Background: the integer square root function is defined as $\DeclareMathOperator{\isqrt}{isqrt}\isqrt{\left(x\right)}=\lfloor\sqrt{x}\rfloor$. There are methods to compute the integer square root using only integers (see previous link), which makes it useful for approximating the real square root on computers with limited processing power.
I'm interested in calculating what could be called the "integer hypotenuse" function: $\DeclareMathOperator{\ihypot}{ihypot}\ihypot{\left(x,y\right)}=\lfloor\sqrt{x^2 + y^2}\rfloor$.* This can be calculated using the same methods as the integer square root, but are there "better" approaches that somehow exploit the form of the argument (the sum of two squares) while still only requiring integers for computation?
I realize that is somewhat vague, so here's an example: One of the ways to find the integer square root is using binary search. Binary search requires an upper and lower bound. Since
$$ 0 \le \lfloor\sqrt{a}\rfloor \le a$$
$0$ and $a$ are often (always?) used as the bounds. We could naively do the same thing with the integer hypotenuse:
$$ 0 \le \lfloor\sqrt{a^2 + b^2}\rfloor \le a^2 + b^2$$
but we can find tighter bounds by realizing that the argument is the sum of squares and using the triangle inequality:
$$ \max\left(\left|a\right|,\left|b\right|\right) \le \lfloor\sqrt{a^2 + b^2}\rfloor \le \left|a\right| + \left|b\right|$$
Using those bounds would mean fewer iterations would be required to find the solution using binary search, so that's an example of a "better" approach that exploits the form of the argument.
I can't rigorously define "better", but I'm using it in the sense of "requiring less computational resources (including time and memory)" to get the answer. Things that might be better include:
- Approaches that require fewer iterations (like the example above).
- Not having to explicitly calculate $a^2 + b^2$. (Calculating $a^2 + b^2$ could lead to integer overflow issues on a computer.)
- Not having to divide by anything other than a power of 2. (Arbitrary division is expensive on a computer.)
Here's an example of what the second bullet might look like --- except that it doesn't work:
Heron's method for calculating the square root makes use of the inequality of arithmetic and geometric means as follows:
$$\sqrt{a} = \sqrt{x \frac{a}{x}} \leq \frac{1}{2}\left(x + \frac{a}{x}\right)$$
Then you can make an initial guess at the solution $x_0$, iterate $x_{n+1} = \frac{1}{2}\left(x_n + \frac{a}{x_n}\right)$, and $x_n$ will approach $\sqrt{a}$.
I was tempted to do the following:
$$\sqrt{a^2+b^2} = \sqrt{\left(a+bi\right)\left(a-bi\right)} \leq \frac{1}{2}\left(\left(a+bi\right)x + \frac{a-bi}{x}\right)$$
and then iterate in the same way as above. This looks promising and avoids ever having to calculate $a^2$ or $b^2$. However the inequality of arithmetic and geometric means only works for real numbers, so the above line is wrong. (There is a generalization to complex numbers --- eq 1.1 here, but it added enough complication that it didn't look promising to me.)
Also, just to be explicit: I'm not interested in approximations or methods that don't use integers for intermediary steps.
* I realize that this name might sound weird: The hypotenuse is the side of a triangle --- not a function! The motivation is that programming languages typically have separate built-in functions for calculating the square root and the length of the hypotenuse (e.g. math.sqrt
and math.hypot
in Python). Python even has an integer square root function: math.isqrt
. I'm interested in an integer version of the hypotenuse function --- hence the name.