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).
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Day 19: Interfaces
You are viewing a single comment's thread. Return to all comments →
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
andn / i
at the same step, you're adding the other number that is also a divisor in same step, take for example8
, ifi
is2
, you'll end up addingsum += 2 + 8/2
which traduces tosum += 2 + 4
, both2
&4
are divisors of8
, keep doing that and you'll soon notice that this way thei
variable only needs to go up tosqrt(n)
.