Let y {\displaystyle y} and k {\displaystyle k} be non-negative integers.
Algorithms that compute (the decimal representation of) y {\displaystyle {\sqrt {y}}} run forever on each input y {\displaystyle y} which is not a perfect square.1
Algorithms that compute ⌊ y ⌋ {\displaystyle \lfloor {\sqrt {y}}\rfloor } do not run forever. They are nevertheless capable of computing y {\displaystyle {\sqrt {y}}} up to any desired accuracy k {\displaystyle k} .
Choose any k {\displaystyle k} and compute ⌊ y × 100 k ⌋ {\textstyle \lfloor {\sqrt {y\times 100^{k}}}\rfloor } .
For example (setting y = 2 {\displaystyle y=2} ): k = 0 : ⌊ 2 × 100 0 ⌋ = ⌊ 2 ⌋ = 1 k = 1 : ⌊ 2 × 100 1 ⌋ = ⌊ 200 ⌋ = 14 k = 2 : ⌊ 2 × 100 2 ⌋ = ⌊ 20000 ⌋ = 141 k = 3 : ⌊ 2 × 100 3 ⌋ = ⌊ 2000000 ⌋ = 1414 ⋮ k = 8 : ⌊ 2 × 100 8 ⌋ = ⌊ 20000000000000000 ⌋ = 141421356 ⋮ {\displaystyle {\begin{aligned}&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{aligned}}}
Compare the results with 2 = 1.41421356237309504880168872420969807856967187537694... {\displaystyle {\sqrt {2}}=1.41421356237309504880168872420969807856967187537694...}
It appears that the multiplication of the input by 100 k {\displaystyle 100^{k}} gives an accuracy of k decimal digits.2
To compute the (entire) decimal representation of y {\displaystyle {\sqrt {y}}} , one can execute isqrt ( y ) {\displaystyle \operatorname {isqrt} (y)} an infinite number of times, increasing y {\displaystyle y} by a factor 100 {\displaystyle 100} at each pass.
Assume that in the next program ( sqrtForever {\displaystyle \operatorname {sqrtForever} } ) the procedure isqrt ( y ) {\displaystyle \operatorname {isqrt} (y)} is already defined and — for the sake of the argument — that all variables can hold integers of unlimited magnitude.
Then sqrtForever ( y ) {\displaystyle \operatorname {sqrtForever} (y)} will print the entire decimal representation of y {\displaystyle {\sqrt {y}}} .3
The conclusion is that algorithms which compute isqrt() are computationally equivalent to algorithms which compute sqrt().
The integer square root of a non-negative integer y {\displaystyle y} can be defined as ⌊ y ⌋ = x : x 2 ≤ y < ( x + 1 ) 2 , x ∈ N {\displaystyle \lfloor {\sqrt {y}}\rfloor =x:x^{2}\leq y<(x+1)^{2},x\in \mathbb {N} }
For example, isqrt ( 27 ) = ⌊ 27 ⌋ = 5 {\displaystyle \operatorname {isqrt} (27)=\lfloor {\sqrt {27}}\rfloor =5} because 6 2 > 27 and 5 2 ≯ 27 {\displaystyle 6^{2}>27{\text{ and }}5^{2}\ngtr 27} .
The following C programs are straightforward implementations.
In the program above (linear search, ascending) one can replace multiplication by addition, using the equivalence ( L + 1 ) 2 = L 2 + 2 L + 1 = L 2 + 1 + ∑ i = 1 L 2. {\displaystyle (L+1)^{2}=L^{2}+2L+1=L^{2}+1+\sum _{i=1}^{L}2.}
Linear search sequentially checks every value until it hits the smallest x {\displaystyle x} where x 2 > y {\displaystyle x^{2}>y} .
A speed-up is achieved by using binary search instead. The following C-program is an implementation.
Numerical example
For example, if one computes isqrt ( 2000000 ) {\displaystyle \operatorname {isqrt} (2000000)} using binary search, one obtains the [ L , R ] {\displaystyle [L,R]} sequence [ 0 , 2000001 ] → [ 0 , 1000000 ] → [ 0 , 500000 ] → [ 0 , 250000 ] → [ 0 , 125000 ] → [ 0 , 62500 ] → [ 0 , 31250 ] → [ 0 , 15625 ] → [ 0 , 7812 ] → [ 0 , 3906 ] → [ 0 , 1953 ] → [ 976 , 1953 ] → [ 976 , 1464 ] → [ 1220 , 1464 ] → [ 1342 , 1464 ] → [ 1403 , 1464 ] → [ 1403 , 1433 ] → [ 1403 , 1418 ] → [ 1410 , 1418 ] → [ 1414 , 1418 ] → [ 1414 , 1416 ] → [ 1414 , 1415 ] {\displaystyle {\begin{aligned}&[0,2000001]\rightarrow [0,1000000]\rightarrow [0,500000]\rightarrow [0,250000]\rightarrow [0,125000]\rightarrow [0,62500]\rightarrow [0,31250]\rightarrow [0,15625]\\&\rightarrow [0,7812]\rightarrow [0,3906]\rightarrow [0,1953]\rightarrow [976,1953]\rightarrow [976,1464]\rightarrow [1220,1464]\rightarrow [1342,1464]\rightarrow [1403,1464]\\&\rightarrow [1403,1433]\rightarrow [1403,1418]\rightarrow [1410,1418]\rightarrow [1414,1418]\rightarrow [1414,1416]\rightarrow [1414,1415]\end{aligned}}}
This computation takes 21 iteration steps, whereas linear search (ascending, starting from 0 {\displaystyle 0} ) needs 1414 steps.
One way of calculating n {\displaystyle {\sqrt {n}}} and isqrt ( n ) {\displaystyle \operatorname {isqrt} (n)} is to use Heron's method, which is a special case of Newton's method, to find a solution for the equation x 2 − n = 0 {\displaystyle x^{2}-n=0} , giving the iterative formula x k + 1 = 1 2 ( x k + n x k ) , k ≥ 0 , x 0 > 0. {\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 { x k } {\displaystyle \{x_{k}\}} converges quadratically to n {\displaystyle {\sqrt {n}}} as k → ∞ {\displaystyle k\to \infty } .
One can prove that c = 1 {\displaystyle c=1} is the largest possible number for which the stopping criterion | x k + 1 − x k | < c {\displaystyle |x_{k+1}-x_{k}|<c} ensures ⌊ x k + 1 ⌋ = ⌊ n ⌋ {\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 1 should be used to protect against round-off errors.
Although n {\displaystyle {\sqrt {n}}} is irrational for many n {\displaystyle n} , the sequence { x k } {\displaystyle \{x_{k}\}} contains only rational terms when x 0 {\displaystyle x_{0}} is rational. Thus, with this method it is unnecessary to exit the field of rational numbers in order to calculate isqrt ( n ) {\displaystyle \operatorname {isqrt} (n)} , a fact which has some theoretical advantages.
For computing ⌊ n ⌋ {\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 x k + 1 = ⌊ 1 2 ( x k + ⌊ n x k ⌋ ) ⌋ , k ≥ 0 , x 0 > 0 , x 0 ∈ Z . {\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 ⌊ 1 2 ( x k + ⌊ n x k ⌋ ) ⌋ = ⌊ 1 2 ( x k + n x k ) ⌋ , {\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 ⌊ n ⌋ {\displaystyle \lfloor {\sqrt {n}}\rfloor } within a finite number of iterations.
In the original version, one has x k ≥ n {\displaystyle x_{k}\geq {\sqrt {n}}} for k ≥ 1 {\displaystyle k\geq 1} , and x k > x k + 1 {\displaystyle x_{k}>x_{k+1}} for x k > n {\displaystyle x_{k}>{\sqrt {n}}} . So in the integer version, one has ⌊ x k ⌋ ≥ ⌊ n ⌋ {\displaystyle \lfloor x_{k}\rfloor \geq \lfloor {\sqrt {n}}\rfloor } and x k ≥ ⌊ x k ⌋ > x k + 1 ≥ ⌊ x k + 1 ⌋ {\displaystyle x_{k}\geq \lfloor x_{k}\rfloor >x_{k+1}\geq \lfloor x_{k+1}\rfloor } until the final solution x s {\displaystyle x_{s}} is reached. For the final solution x s {\displaystyle x_{s}} , one has ⌊ n ⌋ ≤ ⌊ x s ⌋ ≤ n {\displaystyle \lfloor {\sqrt {n}}\rfloor \leq \lfloor x_{s}\rfloor \leq {\sqrt {n}}} and ⌊ x s + 1 ⌋ ≥ ⌊ x s ⌋ {\displaystyle \lfloor x_{s+1}\rfloor \geq \lfloor x_{s}\rfloor } , so the stopping criterion is ⌊ x k + 1 ⌋ ≥ ⌊ x k ⌋ {\displaystyle \lfloor x_{k+1}\rfloor \geq \lfloor x_{k}\rfloor } .
However, ⌊ n ⌋ {\displaystyle \lfloor {\sqrt {n}}\rfloor } is not necessarily a fixed point of the above iterative formula. Indeed, it can be shown that ⌊ n ⌋ {\displaystyle \lfloor {\sqrt {n}}\rfloor } is a fixed point if and only if n + 1 {\displaystyle n+1} is not a perfect square. If n + 1 {\displaystyle n+1} is a perfect square, the sequence ends up in a period-two cycle between ⌊ n ⌋ {\displaystyle \lfloor {\sqrt {n}}\rfloor } and ⌊ n ⌋ + 1 {\displaystyle \lfloor {\sqrt {n}}\rfloor +1} instead of converging.
For example, if one computes the integer square root of 2000000 using the algorithm above, one obtains the sequence 1000000 → 500001 → 250002 → 125004 → 62509 → 31270 → 15666 → 7896 → 4074 → 2282 → 1579 → 1422 → 1414 → 1414 {\displaystyle {\begin{aligned}&1000000\rightarrow 500001\rightarrow 250002\rightarrow 125004\rightarrow 62509\rightarrow 31270\rightarrow 15666\rightarrow 7896\\&\rightarrow 4074\rightarrow 2282\rightarrow 1579\rightarrow 1422\rightarrow 1414\rightarrow 1414\end{aligned}}} In total 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 x 0 = 2 ⌊ ( log 2 n ) / 2 ⌋ + 1 , {\displaystyle x_{0}=2^{\lfloor (\log _{2}n)/2\rfloor +1},} which is the least power of two bigger than n {\displaystyle {\sqrt {n}}} . In the example of the integer square root of 2000000, ⌊ log 2 n ⌋ = 20 {\displaystyle \lfloor \log _{2}n\rfloor =20} , x 0 = 2 11 = 2048 {\displaystyle x_{0}=2^{11}=2048} , and the resulting sequence is 2048 → 1512 → 1417 → 1414 → 1414. {\displaystyle 2048\rightarrow 1512\rightarrow 1417\rightarrow 1414\rightarrow 1414.} In this case only four iteration steps are needed.
The traditional pen-and-paper algorithm for computing the square root n {\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 ≤ n {\displaystyle \leq n} . If stopping after the one's place, the result computed will be the integer square root.
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:
Traditional pen-and-paper presentations of the digit-by-digit algorithm include various optimizations not present in the code above, in particular the trick of pre-subtracting the square of the previous digits which makes a general multiplication step unnecessary. See Methods of computing square roots § Binary numeral system (base 2) for an example.4
The Karatsuba square root algorithm is a combination of two functions: a public function, which returns the integer square root of the input, and a recursive private function, which does the majority of the work.
The public function normalizes the actual input, passes the normalized input to the private function, denormalizes the result of the private function, and returns that.
The private function takes a normalized input, divides the input bits in half, passes the most-significant half of the input recursively to the private function, and performs some integer operations on the output of that recursive call and the least-significant half of the input to get the normalized output, which it returns.
For big-integers of "50 to 1,000,000 digits", Burnikel-Ziegler Karatsuba division and Karatsuba multiplication are recommended by the algorithm's creator.5
An example algorithm for 64-bit unsigned integers is below. The algorithm:
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.
The square roots of the perfect squares (e.g., 0, 1, 4, 9, 16) are integers. In all other cases, the square roots of positive integers are irrational numbers. /wiki/Square_root#As_decimal_expansions ↩
It is no surprise that the repeated multiplication by 100 is a feature in Jarvis (2006) - Jarvis, Ashley Frazer (2006). "Square roots by subtraction" (PDF). Mathematical Spectrum. 37: 119–122. http://www.afjarvis.staff.shef.ac.uk/maths/jarvisspec02.pdf ↩
The fractional part of square roots of perfect squares is rendered as 000.... ↩
Woo, C (June 1985). "Square root by abacus algorithm (archived)". Archived from the original on 2012-03-06. https://web.archive.org/web/20120306040058/http://medialab.freaknet.org/martin/src/sqrt/sqrt.c ↩
Zimmermann, Paul (1999). "Karatsuba Square Root" (PDF). Research report #3805. Inria (published 2006-05-24). Archived from the original (PDF) on 2023-05-11. https://web.archive.org/web/20230511212802/https://inria.hal.science/inria-00072854v1/file/RR-3805.pdf ↩
"BigInteger - Chapel Documentation 2.1". Chapel Documentation - Chapel Documentation 2.1. https://chapel-lang.org/docs/modules/standard/BigInteger.html#BigInteger.sqrt ↩
"CLHS: Function SQRT, ISQRT". Common Lisp HyperSpec (TM). http://www.lispworks.com/documentation/lw50/CLHS/Body/f_sqrt_.htm ↩
"Math - Crystal 1.13.2". The Crystal Programming Language API docs. https://crystal-lang.org/api/1.13.2/Math.html#isqrt%28value%3AInt%3A%3APrimitive%29-instance-method ↩
"BigInteger (Java SE 21 & JDK 21)". JDK 21 Documentation. https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/math/BigInteger.html#sqrt() ↩
"Mathematics - The Julia Language". Julia Documentation - The Julia Language. https://docs.julialang.org/en/v1/base/math/#Base.isqrt ↩
"iroot- Maple Help". Help - Maplesoft. https://www.maplesoft.com/support/help/maple/view.aspx?path=isqrt ↩
"Catalogue of GP/PARI Functions: Arithmetic functions". PARI/GP Development Headquarters. https://pari.math.u-bordeaux.fr/dochtml/html-stable/Arithmetic_functions.html#sqrtint ↩
"Index of /archive/science/math/multiplePrecision/pari/". PSG Digital Resources. Archived from the original on 2024-11-06. https://web.archive.org/web/20241106111659/http://library.snls.org.sz/archive/science/math/multiplePrecision/pari/ ↩
"Mathematical functions". Python Standard Library documentation. https://docs.python.org/3/library/math.html#math.isqrt ↩
"4.3.2 Generic Numerics". Racket Documentation. https://docs.racket-lang.org/reference/generic-numbers.html#%28def._%28%28quote._~23~25kernel%29._integer-sqrt%29%29 ↩
"class Integer - RDoc Documentation". RDoc Documentation. https://ruby-doc.org/3.2.2/Integer.html#sqrt-method ↩
"i32 - Rust". std - Rust. https://doc.rust-lang.org/std/primitive.i32.html#method.isqrt ↩
"i32 - Rust". std - Rust. https://doc.rust-lang.org/std/primitive.i32.html#method.checked_isqrt ↩
"Elements of the ring ℤ of integers - Standard Commutative Rings". SageMath Documentation. https://doc.sagemath.org/html/en/reference/rings_standard/sage/rings/integer.html#sage.rings.integer.Integer.isqrt ↩
"Revised7 Report on the Algorithmic Language Scheme". Scheme Standards. https://standards.scheme.org/corrected-r7rs/r7rs-Z-H-8.html#TAG:__tex2page_index_470 ↩
"mathfunc manual page - Tcl Mathematical Functions". Tcl/Tk 8.6 Manual. https://www.tcl.tk/man/tcl/TclCmd/mathfunc.htm#M22 ↩
"std.math.sqrt.sqrt - Zig Documentation". Home ⚡ Zig Programming Language. https://ziglang.org/documentation/master/std/#std.math.sqrt.sqrt ↩