I used this code for the problem ( http://www.codechef.com/problems/PD13/ ).I tried as many test cases I could think of and get correct answer on each one of them but whenever I submit it shows wrong answer.Please advise.Here’s the code.(math.h is included it wont show somehow )

#include

#include<math.h>

long long divsum(long n);

int main()

{

long cases,num,x=0;

scanf("%ld",&cases);

for(long j=0;j<cases;j++)

{

scanf("%ld",&num);

if(num==1||num==0)

{

printf("%d\n",x);

}

else

{

printf("%lld\n",divsum(num));

}

}

}

long long divsum(long n)

{

long k;

long long sum=0;

k=(long)sqrt((float)n);

if(n%2!=0)

{

for(long i=1;i<=k;i+=2)

{

if(n%i==0)

{

sum+=i+n/i;

}

}

}

else

{

for(int i=1;i<=k;i++)

{

if(n%i==0)

{

sum+=i+n/i;

}

}

}

return sum-n;

}

If the number is a perfect square , you will end up adding the square root of it twice .

2 Likes

thanx a lot sir fixed my mistake