Divisor Summation PD13

Problem link : PD13
My code : http://ideone.com/kqh8S2
Tried a lot but still can’t figure out why it’s giving wrong output.

My opinion is, that sharing variable for different loop is very bad habit.

I changed just this without algorithm modification and it seems to work fine now - http://ideone.com/CDZ3l0

I think, that

for (z - 1; z > 0; --z)

is the problem, modify it to

for (; z > 0; --z)

and set

z = y - 1


[Here’s][1] my new code. I am still getting TLE status so I checked the accepted solutions and found [this one][2] . Even he is iterating over the same range, but only my code is getting TLE. Someone please explain this and also tell me the complexities of the two codes and how are they different.

[1]: http://ideone.com/JwdwNz
[2]: http://www.codechef.com/viewplaintext/2869792

In ur code u r printing ‘sum-n’ but in the question you have to print summation of all divisors that is ‘sum’ u r calculating in ur code…and also the number itself is not counted as a divisor so u hv to print ‘sum-1’…

@betlista Thanks a lot. “sharing variable for different loop is very bad habit” I do not understand what you re trying to say.

@gautam94, I think @betlista was trying to say that for each loop you should use a different variable instead of using the same variable for multiple loops…

1 Like

@junior94 Thanks but I had already figured that out some time ago. Could you please look at my post below this and try answering it.