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. This works fine for numbers like 256, 808201 , etc But fails for numbers like 999966000289. I cannot figure out why? Answer As mentioned in the comments, the problem is that the intermediate mid*mid