I was told that this piece of code 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 seems to work, but I donβt understand how it works?
c math sqrt
Howard gray
source share