I was recently asked to write an algorithm to find the square root of a given integer, with the solution expressed as an integer with the smallest error.
It was unclear to me whether this meant I need to return the integer closest to the actual square root, or return the integer such that, when squared, returned the closest value to the target number. When asking for clarification, I was told these are the same.
This got me thinking however, are these really equivalent?
Clearly, when deciding between two real approximations, minimizing the error of the square roots isn't the same as minimizing the error of the squares. For example, if I had to pick between 1.9 and 2.096 as an approximation of the square root of 4, then 2.096 would be the closest to the actual square root (error of 0.096 compared to 0.1), while 1.9 would provide the smallest error when squared (error of 0.39 compared to 0.393216).
For the integer approximation case, it doesn't appear obvious to me why the solutions to the two cases will always be the same. I've experimentally verified it to be so for the first 200 billion integers. Can anyone provide an intuitive or formal proof of this?