We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.

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).

## Day 19: Interfaces

You are viewing a single comment's thread. Return to all comments →

sqrt(n) will reduce it more.

just add i and n/i(if a pure int)

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)`

.yes i got it thank you very much

If n=1, then this would return 2, no? even though it should return 1.