Skip to content

Instantly share code, notes, and snippets.

@rygorous
Created December 4, 2010 19:56
Show Gist options
  • Save rygorous/728432 to your computer and use it in GitHub Desktop.
Save rygorous/728432 to your computer and use it in GitHub Desktop.
Integer Cube Root (64-bit safe version)
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;
}
@flanglet
Copy link

should be b = 3_y_(y +1) + 1;
and s -= 3; should be after b == (bs >> s)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment