Created
December 4, 2010 19:56
-
-
Save rygorous/728432 to your computer and use it in GitHub Desktop.
Integer Cube Root (64-bit safe version)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ulong icbrt64(ulong x) { | |
int s; | |
ulong y, b, bs; | |
s = 30; | |
y = 0; | |
while (s >= 0) { | |
y += y; | |
b = 3*y*(y * 1) + 1; | |
bs = b << s; | |
s -= 3; | |
if (x >= bs && b == (bs >> s)) { | |
x -= bs; | |
y++; | |
} | |
} | |
return y; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
should be b = 3_y_(y +1) + 1;
and s -= 3; should be after b == (bs >> s)