You are viewing a single comment's thread. Return to all comments →
how is this correct for a number like 20? 10 is larger than sqrt(20), am confused here
You're right, if we were talking prime numbers, sqrt(n) would make sense, but not for divisors.
[Edit]: Let me correct myself, if you add i and n / i at the same step, you're adding the other number that is also a divisor in same step, take for example 8, if i is 2, you'll end up adding sum += 2 + 8/2 which traduces to sum += 2 + 4, both 2 & 4 are divisors of 8, keep doing that and you'll soon notice that this way the i variable only needs to go up to sqrt(n).
n / i
sum += 2 + 8/2
sum += 2 + 4
yes i got it thank you very much
If n=1, then this would return 2, no? even though it should return 1.