Thursday, 19 September 2013

what is wrong in my program? although it runs all right

what is wrong in my program? although it runs all right

i want to make a following program: Given a natural number n (1 <= n <=
500000), that outputs the summation of all its proper divisors. e.g.
number 20 has 5 proper divisors: 1, 2, 4, 5, 10, and the divisor summation
is: 1 + 2 + 4 + 5 + 10 = 22.
Input An integer stating the number of test cases (equal to about 200000),
and that many lines follow, each containing one integer between 1 and
500000 inclusive.
Output One integer each line: the divisor summation of the integer given
respectively.
Example
Sample Input:
3
2
10
20
Sample Output:
1
8
22
i have made following code:
#include <stdio.h>
#include <math.h>
int main()
{
long int R,i,q,a,b,k,sum[200000],a1,a2,s,ar[200000];
//printf("enter range");
scanf("%ld",&R);
//sum[0]=1;
for(i=0;i<R;i++)
{
// printf("enter number");
scanf("%ld",&ar[i]);
}
for(q=0;q<R;q++)
{
sum[q]=1;
s=sqrt(ar[q]);
if(ar[q]==1)
{
sum[i]=0;
}
for(b=2;b<=s;b++)
{
if(ar[q]%b==0)
{
a1=b; //factor
a2=ar[q]/b; //it will also be a factor
sum[q]=sum[q]+a1+a2;
if(a1==a2) sum[q]=sum[q]-a2;
if(a1>=a2) break;
}
}
}
if(q==R)
{ for(k=0;k<R;k++)
printf("%ld\n",sum[k]);
}
return 0;
}
i think i have made the code right and its also working fine, but when i
submit the code here (at submit button on top) i get a response as :
"wrong answer", please help anybody, i can't figure it out, why is my
answer wrong?

No comments:

Post a Comment