Skip to content

Instantly share code, notes, and snippets.

@NodoFox
Created January 16, 2017 10:54
Show Gist options
  • Save NodoFox/88c56bc34040b48b146c6ab4d510b551 to your computer and use it in GitHub Desktop.
Save NodoFox/88c56bc34040b48b146c6ab4d510b551 to your computer and use it in GitHub Desktop.
public boolean isPerfectSquare(int num) {
long low = 0, high = num;
while(low <= high) {
long mid = low + (high - low)/2;
//int product = mid * mid;
// if(mid != 0 && product/mid != mid) {
// high = mid - 1; // integer overflow;
// continue;
// }
if(mid * mid == num) {
return true;
}
if(mid * mid > num) {
high = mid - 1;
} else {
low = mid + 1;
}
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment