Skip to content

Instantly share code, notes, and snippets.

@ichaos
Created September 14, 2013 15:01
Show Gist options
  • Save ichaos/6562712 to your computer and use it in GitHub Desktop.
Save ichaos/6562712 to your computer and use it in GitHub Desktop.
Check if a integer is perfect square, i.e. its root is also a integer.
bool isPerfectSquare(int x) {
int sqr = Math.sqrt(x);
return x == sqr * sqr;
}
bool isPerfectSquareFaster(int x) {
if (x < 0) return false;
int h = x & 0xf;
if (h == 0 || h == 0x1 || h == 0x4 || h == 0x9) {
return isPerfectSquare(x);
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment