# Integer square root

In number theory, the integer square root (isqrt) of a positive integer n is the positive integer m which is the greatest integer less than or equal to the square root of n,

${\mbox{isqrt}}(n)=\lfloor {\sqrt {n}}\rfloor .$ For example, ${\mbox{isqrt}}(27)=5$ because $5\cdot 5=25\leq 27$ and $6\cdot 6=36>27$ .

## Algorithm using Newton's method

One way of calculating ${\sqrt {n}}$ and ${\mbox{isqrt}}(n)$ is to use Newton's method to find a solution for the equation $x^{2}-n=0$ , giving the iterative formula

${x}_{k+1}={\frac {1}{2}}\left(x_{k}+{\frac {n}{x_{k}}}\right),\quad k\geq 0,\quad x_{0}>0.$ The sequence $\{x_{k}\}$ converges quadratically to ${\sqrt {n}}$ as $k\to \infty$ . It can be proven that if $x_{0}=n$ is chosen as the initial guess, one can stop as soon as

$|x_{k+1}-x_{k}|<1$ to ensure that $\lfloor x_{k+1}\rfloor =\lfloor {\sqrt {n}}\rfloor .$ ### Using only integer division

For computing $\lfloor {\sqrt {n}}\rfloor$ for very large integers n, one can use the quotient of Euclidean division for both of the division operations. This has the advantage of only using integers for each intermediate value, thus making the use of floating point representations of large numbers unnecessary. It is equivalent to using the iterative formula

${x}_{k+1}=\left\lfloor {\frac {1}{2}}\left(x_{k}+\left\lfloor {\frac {n}{x_{k}}}\right\rfloor \right)\right\rfloor ,\quad k\geq 0,\quad x_{0}>0,\quad x_{0}\in \mathbb {Z} .$ By using the fact that

$\left\lfloor {\frac {1}{2}}\left(x_{k}+\left\lfloor {\frac {n}{x_{k}}}\right\rfloor \right)\right\rfloor =\left\lfloor {\frac {1}{2}}\left(x_{k}+{\frac {n}{x_{k}}}\right)\right\rfloor ,$ one can show that this will reach $\lfloor {\sqrt {n}}\rfloor$ within a finite number of iterations.

However, $\lfloor {\sqrt {n}}\rfloor$ is not necessarily a fixed point of the above iterative formula. Indeed, it can be shown that $\lfloor {\sqrt {n}}\rfloor$ is a fixed point if and only if $n+1$ is not a perfect square. If $n+1$ is a perfect square, the sequence ends up in a period-two cycle between $\lfloor {\sqrt {n}}\rfloor$ and $\lfloor {\sqrt {n}}\rfloor +1$ instead of converging. For termination, it suffices to check that either the number has converged or it has increased by exactly one from the previous step, in which case the new result is discarded.

### Domain of computation

Although ${\sqrt {n}}$ is irrational for many $n$ , the sequence $\{x_{k}\}$ contains only rational terms when $x_{0}$ is rational. Thus, with this method it is unnecessary to exit the field of rational numbers in order to calculate ${\mbox{isqrt}}(n)$ , a fact which has some theoretical advantages.

### Stopping criterion

One can prove that $c=1$ is the largest possible number for which the stopping criterion

$|x_{k+1}-x_{k}| ensures $\lfloor x_{k+1}\rfloor =\lfloor {\sqrt {n}}\rfloor$ in the algorithm above.

In implementations which use number formats that cannot represent all rational numbers exactly (for example, floating point), a stopping constant less than one should be used to protect against roundoff errors.

## Digit-by-digit algorithm

The traditional pen-and-paper algorithm for computing the square root ${\sqrt {n}}$ is based on working from higher digit places to lower, and as each new digit pick the largest that will still yield a square $\leq n$ . If stopping after the one's place, the result computed will be the integer square root.

### Using bitwise operations

If working in base 2, the choice of digit is simplified to that between 0 (the "small candidate") and 1 (the "large candidate"), and digit manipulations can be expressed in terms of binary shift operations. With * being multiplication, << being left shift, and >> being logical right shift, a recursive algorithm to find the integer square root of any natural number is:

function integerSqrt(n):
if n < 0:
error "integerSqrt works for only nonnegative inputs"
else if n < 2:
return n
else:
# Recursive call:
smallCandidate = integerSqrt(n >> 2) << 1
largeCandidate = smallCandidate + 1
if largeCandidate*largeCandidate > n:
return smallCandidate
else:
return largeCandidate

function integerSqrt(n):
if n < 0:
error "integerSqrt works for only nonnegative inputs"

# Find greatest shift.
shift = 2
nShifted = n >> shift
# We check for nShifted being n, since some implementations
# perform shift operations modulo the word size.
while nShifted ≠ 0 and nShifted ≠ n:
shift = shift + 2
nShifted = n >> shift
shift = shift - 2

# Find digits of result.
result = 0
while shift ≥ 0:
result = result << 1
candidateResult = result + 1
if candidateResult*candidateResult ≤ n >> shift:
result = candidateResult
shift = shift - 2

return result

Traditional pen-and-paper presentations of the digit-by-digit algorithm include various optimisations not present in the code above, in particular the trick of presubtracting the square of the previous digits which makes a general multiplication step unnecessary.