In number theory, the integer square root (isqrt) of a non-negative integer is the non-negative integer which is the greatest integer less than or equal to the square root of ,
<math display="block">\operatorname{isqrt}(n) = \lfloor \sqrt n \rfloor.</math>
For example, <math>\operatorname{isqrt}(27) = \lfloor \sqrt{27} \rfloor = \lfloor 5.19615242270663 ... \rfloor = 5.</math>
Introductory remark
Let <math>y</math> and <math>k</math> be non-negative integers.
Algorithms that compute (the decimal representation of) <math>\sqrt y</math> run forever on each input <math>y</math> which is not a perfect square.
Algorithms that compute <math>\lfloor \sqrt y \rfloor</math> do not run forever. They are nevertheless capable of computing <math>\sqrt y</math> up to any desired accuracy <math>k</math>.
Choose any <math>k</math> and compute <math display="inline">\lfloor \sqrt {y \times 100^k} \rfloor</math>.
For example (setting <math>y = 2</math>):
<math display="block">\begin{align}
& k = 0: \lfloor \sqrt {2 \times 100^{0 \rfloor = \lfloor \sqrt {2} \rfloor = 1 \\
& k = 1: \lfloor \sqrt {2 \times 100^{1 \rfloor = \lfloor \sqrt {200} \rfloor = 14 \\
& k = 2: \lfloor \sqrt {2 \times 100^{2 \rfloor = \lfloor \sqrt {20000} \rfloor = 141 \\
& k = 3: \lfloor \sqrt {2 \times 100^{3 \rfloor = \lfloor \sqrt {2000000} \rfloor = 1414 \\
& \vdots \\
& k = 8: \lfloor \sqrt {2 \times 100^{8 \rfloor = \lfloor \sqrt {20000000000000000} \rfloor = 141421356 \\
& \vdots \\
\end{align}</math>
Compare the results with <math>\sqrt {2} = 1.41421356237309504880168872420969807856967187537694 ...</math>
It appears that the multiplication of the input by <math>100^k</math> gives an accuracy of decimal digits.
To compute the (entire) decimal representation of <math>\sqrt y</math>, one can execute <math>\operatorname{isqrt}(y)</math> an infinite number of times, increasing <math>y</math> by a factor <math>100</math> at each pass.
Assume that in the next program (<math>\operatorname{sqrtForever}</math>) the procedure <math>\operatorname{isqrt}(y)</math> is already defined and — for the sake of the argument — that all variables can hold integers of unlimited magnitude.
Then <math>\operatorname{sqrtForever}(y)</math> will print the entire decimal representation of <math>\sqrt y</math>.
:<syntaxhighlight lang="python">
import math # assume isqrt computation as given here
def sqrtForever(y: int):
""" Print sqrt(y), without halting """
result = math.isqrt(y)
print(str(result) + ".", end="") # print result, followed by a decimal point
while True: # repeat forever ...
y *= 100 # theoretical example: overflow is ignored
result = math.isqrt(y)
print(str(result % 10), end="") # print last digit of result
</syntaxhighlight>
The conclusion is that algorithms which compute are computationally equivalent to algorithms which compute .
Another derivation of <math>\sqrt y</math> from <math>\lfloor \sqrt y \rfloor</math> is given in section Continued fraction of √c based on isqrt below.
Basic algorithms
The integer square root of a non-negative integer <math>y</math> can be defined as
<math display="block">\lfloor \sqrt y \rfloor = \max \{ x : x^2 \leq y <(x+1)^2, x \in \mathbb{N} \}</math>
For example, <math>\operatorname{isqrt}(27) = \lfloor \sqrt{27} \rfloor = 5</math> because <math>6^2 > 27 \text{ and } 5^2 \ngtr 27</math>.
Algorithm using linear search
The following Python programs are straightforward implementations.
:
Linear search using addition
In the program above (linear search, ascending) one can replace multiplication by addition, using the equivalence
<math display="block">(L+1)^2 = L^2 + 2L + 1 = L^2 + 1 + \sum_{i=1}^L 2.</math>
:<syntaxhighlight lang="python">
def isqrt(y: int) -> int:
"""
Integer square root
(linear search, ascending) using addition
"""
L = 0
a = 1
d = 3
while a <= y:
a = a + d
d = d + 2
L = L + 1
return L
</syntaxhighlight>
Algorithm using binary search
Linear search sequentially checks every value until it hits the smallest <math>x</math> where <math>x^2 > y</math>.
A speed-up is achieved by using binary search instead.
:<syntaxhighlight lang="python">
def isqrt(y: int) -> int:
""" Integer square root (binary search) """
L = 0 # lower bound of the square root
R = y + 1 # upper bound of the square root
while (L != R - 1):
M = (L + R) // 2 # midpoint to test
if (M * M <= y):
L = M
else:
R = M
return L
</syntaxhighlight>
Numerical examples
- <math>\operatorname{isqrt}(0) = 0</math>, <math>\operatorname{isqrt}(1) = 1</math>.
- Using binary search the computation of <math>\operatorname{isqrt}(131072)</math> converges to <math>362</math> in <math>17</math> iteration steps via the <math>[L,R]</math> sequence
:<math>\begin{aligned} &[0,131073] \to [0,65536] \to [0,32768] \to [0,16384] \to [0,8192] \to [0,4096] \rightarrow [0,2048] \to [0,1024] \to [0,512] \\& \to [256,512] \to [256,384] \to [320,384] \to [352,384] \to [352,368] \to [360,368] \to [360,364] \to [362,364] \to [362,363] \end{aligned}</math>
- The computation of <math>\operatorname{isqrt}(2000000)</math> converges to <math>1414</math> in <math>21</math> steps via the <math>[L,R]</math> sequence
:<math>\begin{aligned} & [0,2000001] \to [0,1000000] \to [0,500000] \to [0,250000] \to [0,125000] \to [0,62500] \to [0,31250] \to [0,15625] \\& \to [0,7812] \to [0,3906] \to [0,1953] \to [976,1953] \to [976,1464] \to [1220,1464] \to [1342,1464] \to [1403,1464] \\& \to [1403,1433] \to [1403,1418] \to [1410,1418] \to [1414,1418] \to [1414,1416] \to [1414,1415] \end{aligned}</math>
: Linear search (ascending, starting from <math>0</math>) needs steps.
Algorithm using Newton's method
One way of calculating <math>\sqrt{n}</math> and <math>\operatorname{isqrt}(n)</math> is to use Heron's method, which is a special case of Newton's method, to find a solution for the equation <math>x^2 - n = 0</math>, giving the iterative formula
<math display="block">x_{k+1} = \frac{1}{2}\!\left(x_k + \frac{n}{x_k}\right), \quad k \ge 0, \quad x_0 > 0.</math>
The sequence <math>\{x_k\}</math> converges quadratically to <math>\sqrt{n}</math> as <math>k\to\infty</math>.
Using only integer division
For computing <math>\lfloor \sqrt n \rfloor</math> 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 unnecessary.
Let <math>n > 0</math> and initial guess <math>x_0 > 0</math>. Define the integer sequence:
:<math>x_{k+1} = \left\lfloor \frac{x_k + \left\lfloor n / x_k \right\rfloor}{2} \right\rfloor, \quad k = 0, 1, 2, \dots</math>
Proof of convergence
1. Positivity:
All terms are positive integers: <math>x_k > 0</math> for all <math>k</math>.
2. Monotonicity:
- If <math>x_k > \sqrt{n}</math>, then <math>\lfloor n / x_k \rfloor \le n / x_k</math>;
: so <math>x_{k+1} = \left\lfloor \frac{x_k + \lfloor n / x_k \rfloor}{2} \right\rfloor < \frac{x_k + n / x_k}{2} < x_k</math>.
:Hence the sequence decreases.
- If <math>x_k < \sqrt{n}</math>, then <math>\lfloor n / x_k \rfloor \ge n / x_k - 1</math>;
:so <math>x_{k+1} \ge \frac{x_k + n / x_k - 1}{2} > x_k - 1</math>.
:Hence the sequence increases or stays the same.
3. Boundedness:
The sequence is bounded below by 1 and above by <math>x_0</math>, so it is bounded.
4. Stabilization / Oscillation:
A bounded monotone integer sequence either stabilizes or oscillates between two consecutive integers:
: <math>x_{k+1} = x_k</math> or <math>x_{k+1} = x_k \pm 1</math>.
5. Integer "Fixed-point" Condition:
At stabilization or oscillation:
: <math>x_{k+1} = \lfloor (x_k + \lfloor n / x_k \rfloor)/2 \rfloor</math>.
:This ensures that the sequence is either at <math>\lfloor \sqrt n \rfloor</math> or oscillating between the two nearest integers around <math>\sqrt n</math>.
6. Conclusion:
The sequence eventually stabilizes at <math>\lfloor \sqrt n \rfloor</math> or oscillates between <math>\lfloor \sqrt n \rfloor</math> and <math>\lceil \sqrt n \rceil</math>.
Remark:
- <math>\lfloor \sqrt n \rfloor</math> is a strict fixed point unless <math>n + 1</math> is a perfect square.
- If <math>n + 1</math> is a perfect square, the sequence oscillates between <math>\lfloor \sqrt n \rfloor</math> and <math>\lceil \sqrt n \rceil</math>.
Example implementation
:<syntaxhighlight lang="python">
def isqrt(n: int, x0: int = 1) -> int:
"""
isqrt via Newton-Heron iteration with specified initial guess.
Uses 2-cycle oscillation detection.
Preconditions:
n >= 0 # isqrt(0) = 0
x0 > 0, defaults to 1 # initial guess
Output:
isqrt(n)
"""
assert n >= 0 and x0 > 0, "Invalid input"
- isqrt(0) = 0; isqrt(1) = 1
if n < 2: return n
prev2 = -1 # x_{i-2}
prev1 = x0 # x_{i-1}
while True:
x1 = (prev1 + n // prev1) // 2
- Case 1: converged (steady value)
if x1 == prev1:
return x1
- Case 2: oscillation (2-cycle)
if x1 == prev2 and x1 != prev1:
- We’re flipping between prev1 and prev2
- Choose the smaller one (the true integer sqrt)
return min(prev1, x1)
- Move forward
prev2, prev1 = prev1, x1
</syntaxhighlight>
Numerical examples
The call <code>isqrt(2000000)</code> converges to <math>1414</math> in 14 passes through <code>while</code>:
:<math>\begin{aligned} & 1000000 \to 500001 \to 250002 \to 125004 \to 62509 \to 31270 \to 15666 \to 7896 \to 4074 \to 2282 \\& \to 1579 \to 1422 \to 1414 \rightarrow 1414\end{aligned}</math>.
One iteration is gained by setting <code>x0</code> to <math>\lfloor n/2 \rfloor</math> with the call <code>isqrt(2000000, 1000000)</code>. 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. <code>n.bit_length()</code> in Python), one should better start at <math display="block">x_0 = 2^{\lfloor (\log_2 n) / 2 \rfloor + 1},</math> which is the least power of two bigger than <math>\sqrt n</math>. In the example of the integer square root of , <math>\lfloor \log_2 n \rfloor = 20</math>, <math>x_0 = 2^{11} = 2048</math>, and the resulting sequence is
<math display="block">2048 \rightarrow 1512 \rightarrow 1417 \rightarrow 1414 \rightarrow 1414.</math> In this case only four iteration steps are needed. This corresponds to the call <code>isqrt(2000000, 2048)</code>.
Digit-by-digit algorithm
The traditional pen-and-paper algorithm for computing the square root <math>\sqrt{n}</math> is based on working from higher digit places to lower, and as each new digit pick the largest that will still yield a square <math>\leq n</math>. 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 <code>*</code> being multiplication, <code><<</code> being left shift, and <code>>></code> being logical right shift, a recursive algorithm to find the integer square root of any natural number is:
:<syntaxhighlight lang="python">
def isqrt_recursive(n: int) -> int:
assert n >= 0, "n must be a non-negative integer"
if n < 2: return n
- Recursive call:
small_cand = isqrt_recursive(n >> 2) << 1 # same as 2 * isqrt(n // 4)
large_cand = small_cand + 1
if large_cand * large_cand > n: return small_cand
else: return large_cand
</syntaxhighlight>
Equivalent non-recursive program:
:<syntaxhighlight lang="python">
def isqrt_iterative(x: int) -> int:
""" Guy, Martin (1985). "Fast integer square root by Mr. Woo's abacus algorithm" """
assert x >= 0, "x must be a non-negative integer"
op = x; res = 0
- note: i << 1 is 2 * i, i << 2 is 4 * i,
- i >> 1 is i // 2, and i >> 2 is i // 4...
- "one" starts at the highest power of four <= x
one = 1
while one <= op: one <<= 2
one >>= 2
while one != 0:
dltasqr = res + one
if op >= dltasqr:
op -= dltasqr
res += one << 1
res >>= 1
one >>= 2
return res
</syntaxhighlight>
See for an example. It recursively splits the input number into high and low halves, computes the square root of the higher half, and then determines the lower half algebraically.
Algorithm
Paul Zimmermann (1999) gives the following algorithm. Modern GMP and MPIR libraries implement similar recursive techniques.
Implementation in Python
The Python program below implements Zimmermann’s algorithm. Given an integer <math>n \ge 0</math>, <code>SqrtRem</code> computes simultaneously its integer square root <math>s = \lfloor \sqrt{n} \rfloor</math> and the corresponding remainder <math>r = n - s^2</math>. The choice of <code>isqrt()</code> is ad libitum.<br><code>BigInteger.sqrtRem(result, remainder, n);</code> || Unknown
|-
| Common Lisp || <code>(isqrt n)</code> || Unknown
|-
| Crystal || <code>Math.isqrt(n)</code> || 1.2
|-
| Java || <code>n.sqrt()</code> (<code>BigInteger</code> only) || 9
|-
| Julia || <code>isqrt(n)</code> || 0.3
|-
| Maple || <code>isqrt(n)</code> || Unknown
|-
| PARI/GP || <code>sqrtint(n)</code> || 1.35a (as <code>isqrt</code>) or before
|-
| PHP || <code>sqrt($num)</code> || 4
|-
| Python || <code>math.isqrt(n)</code> || 3.8
|-
| Racket || <code>(integer-sqrt n)</code><br><code>(integer-sqrt/remainder n)</code> || Unknown
|-
| Ruby || <code>Integer.sqrt(n)</code> || 2.5.0
|-
| Rust || <code>n.isqrt()</code><br><code>n.checked_isqrt()</code> || 1.84.0
|-
| SageMath || <code>isqrt(n)</code> || Unknown
|-
| Scheme || <code>(exact-integer-sqrt n)</code> || R<sup>6</sup>RS
|-
| Tcl || <code>isqrt($n)</code> || 8.5
|-
| Zig || <code>std.math.sqrt(n)</code> || Unknown
|}
Continued fraction of √c based on isqrt
The computation of the simple continued fraction of <math>\sqrt{c}</math> can be carried out using only integer operations, with <math>\operatorname{isqrt}(c)</math> serving as the initial term. The algorithm generates continued fraction expansion in canonical form.
Let <math>a_0 = \lfloor \sqrt{c} \rfloor</math>
be the integer square root of <math>c</math>.
If <math>c</math> is a perfect square, the continued fraction terminates immediately:
:<math>\sqrt{c} = [a_0].</math>
Otherwise, the continued fraction is periodic:
:<math>\sqrt{c} = [a_0; \overline{a_1, a_2, \dots, a_m}]</math>,
where the overline indicates the repeating part.
The continued fraction can be obtained by the following recurrence, which uses only integer arithmetic:
:<math>m_{0} = 0, \quad d_{0} = 1, \quad a_{0} = \lfloor \sqrt{c} \rfloor.</math>
:For <math>k \geq 0</math>,
:<math>
m_{k+1} = d_{k} a_{k} - m_{k}, \quad
d_{k+1} = \frac{c - m_{k+1}^{2{d_{k, \quad
a_{k+1} = \left\lfloor \frac{a_{0} + m_{k+1{d_{k+1 \right\rfloor.
</math>
Since there are only finitely many possible triples <math>(m_{k}, d_{k}, a_{k})</math>, eventually one repeats, and from that point onward the continued fraction becomes periodic.
Implementation in Python
On input <math>c</math>, a non-negative integer, the following program computes the simple continued fraction of <math>\sqrt{c}</math>. The integer square root <math>\lfloor \sqrt{c} \rfloor</math> is computed once.
|-
| 4097280036 || colspan="2" | <div style="width:400px; white-space:normal;">[64009; (1, 1999, 3, 4, 1, 499, 3, 1, 3, 3, 1, 124, ...<br/>..., 3, 1, 3, 499, 1, 4, 3, 1999, 1, 128018)]<br/>period: 13,032 terms</div>
|}
See also
- Square root algorithms
- Periodic continued fraction
