'Function to check for perfect square not working for large number
I came across an algorithm to tell if a given number is perfect square or not in O(logN) time.
Here is the implementation(JAVA) of the idea.
public boolean isPerfectSquare(long x) {
if (x <= 1)
return true;
long low = 1;
long high = x;
long mid = 0;
while (low <= high) {
mid = low + (high - low) / 2l;
if (mid * mid == x)
return true;
else if (mid * mid < x)
low = mid + 1;
else
high = mid - 1;
}
return false;
}
This works fine for numbers like 256, 808201 , etc
But fails for numbers like 999966000289.
I cannot figure out why?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
