# 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,

${\displaystyle {\mbox{isqrt}}(n)=\lfloor {\sqrt {n}}\rfloor .}$

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

## Algorithm using Newton's method

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

${\displaystyle {x}_{k+1}={\frac {1}{2}}\left(x_{k}+{\frac {n}{x_{k}}}\right),\quad k\geq 0,\quad x_{0}>0.}$

The sequence ${\displaystyle \{x_{k}\}}$ converges quadratically to ${\displaystyle {\sqrt {n}}}$ as ${\displaystyle k\to \infty }$.

### Stopping criterion

One can prove[citation needed] that ${\displaystyle c=1}$ is the largest possible number for which the stopping criterion

${\displaystyle |x_{k+1}-x_{k}|

ensures ${\displaystyle \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.

### Domain of computation

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

### Using only integer division

For computing ${\displaystyle \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

${\displaystyle {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

${\displaystyle \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 ${\displaystyle \lfloor {\sqrt {n}}\rfloor }$ within a finite number of iterations.

In the original version, one has ${\displaystyle {x}_{k}\geq {\sqrt {n}}}$ for ${\displaystyle k\geq 1}$, and ${\displaystyle {x}_{k}>{x}_{k+1}}$ for ${\displaystyle {x}_{k}>{\sqrt {n}}}$. So in the integer version, one has ${\displaystyle \lfloor {x}_{k}\rfloor \geq \lfloor {\sqrt {n}}\rfloor }$ and ${\displaystyle {x}_{k}\geq \lfloor {x}_{k}\rfloor >{x}_{k+1}\geq \lfloor {x}_{k+1}\rfloor }$ until the final solution ${\displaystyle {x}_{s}}$ is reached. For the final solution ${\displaystyle {x}_{s}}$, one has ${\displaystyle \lfloor {\sqrt {n}}\rfloor \leq \lfloor {x}_{s}\rfloor \leq {\sqrt {n}}}$ and ${\displaystyle \lfloor {x}_{s+1}\rfloor \geq \lfloor {x}_{s}\rfloor }$, so the stopping criterion is ${\displaystyle \lfloor {x}_{k+1}\rfloor \geq \lfloor {x}_{k}\rfloor }$.

However, ${\displaystyle \lfloor {\sqrt {n}}\rfloor }$ is not necessarily a fixed point of the above iterative formula. Indeed, it can be shown that ${\displaystyle \lfloor {\sqrt {n}}\rfloor }$ is a fixed point if and only if ${\displaystyle n+1}$ is not a perfect square. If ${\displaystyle n+1}$ is a perfect square, the sequence ends up in a period-two cycle between ${\displaystyle \lfloor {\sqrt {n}}\rfloor }$ and ${\displaystyle \lfloor {\sqrt {n}}\rfloor +1}$ instead of converging.

### Example implementation in C

// Square root of integer
unsigned long int_sqrt ( unsigned long s )
{
unsigned long x0 = s >> 1;				// Initial estimate
// Avoid overflow when s is the maximum representable value

// Sanity check
if ( x0 )
{
unsigned long x1 = ( x0 + s / x0 ) >> 1;	// Update

while ( x1 < x0 )				// This also checks for cycle
{
x0 = x1;
x1 = ( x0 + s / x0 ) >> 1;
}

return x0;
}
else
{
return s;
}
}


### Numerical example

For example, if one computes the integer square root of 2 000 000 using the algorithm above, one obtains the sequence 1 000 000, 500 001, 250 002, 125 004, 62 509, 31 270, 15 666, 7 896, 4 074, 2 282, 1 579, 1 422, 1 414, 1 414. Totally 13 iteration steps are needed. Although Heron's method converges quadratically close to the solution, less than one bit precision per iteration is gained at the beginning. This means that the choice of the initial estimate is critical for the performance of the algorithm.

When a fast computation for the integer part of the binary logarithm or for the bit-length is available (like e.g. std::bit_width in C++20), one should better start at

${\displaystyle x_{0}=2^{\lfloor (\log _{2}n)/2\rfloor +1}}$,

which is the least power of two bigger than ${\displaystyle {\sqrt {n}}}$. In the example of the integer square root of 2 000 000, ${\displaystyle \lfloor \log _{2}n\rfloor =20}$, ${\displaystyle x_{0}=2^{11}=2048}$, and the resulting sequence is 2 048, 1 512, 1 417, 1 414, 1 414. In this case only 4 iteration steps are needed.

## Digit-by-digit algorithm

The traditional pen-and-paper algorithm for computing the square root ${\displaystyle {\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 ${\displaystyle \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:

def integer_sqrt(n: int) -> int:
assert n >= 0, "sqrt works for only non-negative inputs"
if n < 2:
return n

# Recursive call:
small_cand = integer_sqrt(n >> 2) << 1
large_cand = small_cand + 1
if large_cand * large_cand > n:
return small_cand
else:
return large_cand

# equivalently:
def integer_sqrt_iter(n: int) -> int:
assert n >= 0, "sqrt works for only non-negative inputs"
if n < 2:
return n

# shift = ceil(log2(n) * 0.5) * 2 = ceil(ffs(n) * 0.5) * 2
shift = 2
while (n >> shift) != 0:
shift += 2

# Unroll the bit-setting loop.
result = 0
while shift >= 0:
result = result << 1
large_cand = (
result + 1
)  # Same as result ^ 1 (xor), because the last bit is always 0.
if large_cand * large_cand <= n >> shift:
result = large_cand
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. See Methods of computing square roots § Woo abacus for an example.[1]

## In programming languages

Some programming languages dedicate an explicit operation to the integer square root calculation in addition to the general case or can be extended by libraries to this end.