I've been told this code snippet is equivalent to (int)sqrt(n)
int s(int n) {
for (int i = 1, k = 0; n > 0; i += 2) {
if (k + i > n)
return i / 2;
k += i;
}
return 0;
}
And it seem to work, yet I don't understand how it works ?
n
nearINT_MAX
. IAC, for large numbers it takes a long time to run. Faster methods exist. – Talithatalk