Integer square root (original) (raw)

Greatest integer less than or equal to square root

In number theory, the integer square root (isqrt) of a non-negative integer n is the non-negative integer m which is the greatest integer less than or equal to the square root of n, isqrt ⁡ ( n ) = ⌊ n ⌋ . {\displaystyle \operatorname {isqrt} (n)=\lfloor {\sqrt {n}}\rfloor .} {\displaystyle \operatorname {isqrt} (n)=\lfloor {\sqrt {n}}\rfloor .}

For example, isqrt ⁡ ( 27 ) = ⌊ 27 ⌋ = ⌊ 5.19615242270663... ⌋ = 5. {\displaystyle \operatorname {isqrt} (27)=\lfloor {\sqrt {27}}\rfloor =\lfloor 5.19615242270663...\rfloor =5.} {\displaystyle \operatorname {isqrt} (27)=\lfloor {\sqrt {27}}\rfloor =\lfloor 5.19615242270663...\rfloor =5.}

Let y {\displaystyle y} {\displaystyle y} and k {\displaystyle k} {\displaystyle k} be non-negative integers.

Algorithms that compute (the decimal representation of) y {\displaystyle {\sqrt {y}}} {\displaystyle {\sqrt {y}}} run forever on each input y {\displaystyle y} {\displaystyle y} which is not a perfect square.[note 1]

Algorithms that compute ⌊ y ⌋ {\displaystyle \lfloor {\sqrt {y}}\rfloor } {\displaystyle \lfloor {\sqrt {y}}\rfloor } do not run forever. They are nevertheless capable of computing y {\displaystyle {\sqrt {y}}} {\displaystyle {\sqrt {y}}} up to any desired accuracy k {\displaystyle k} {\displaystyle k}.

Choose any k {\displaystyle k} {\displaystyle k} and compute ⌊ y × 100 k ⌋ {\textstyle \lfloor {\sqrt {y\times 100^{k}}}\rfloor } {\textstyle \lfloor {\sqrt {y\times 100^{k}}}\rfloor }.

For example (setting y = 2 {\displaystyle 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}}} {\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...} {\displaystyle {\sqrt {2}}=1.41421356237309504880168872420969807856967187537694...}

It appears that the multiplication of the input by 100 k {\displaystyle 100^{k}} {\displaystyle 100^{k}} gives an accuracy of k decimal digits.[note 2]

To compute the (entire) decimal representation of y {\displaystyle {\sqrt {y}}} {\displaystyle {\sqrt {y}}}, one can execute isqrt ⁡ ( y ) {\displaystyle \operatorname {isqrt} (y)} {\displaystyle \operatorname {isqrt} (y)} an infinite number of times, increasing y {\displaystyle y} {\displaystyle y} by a factor 100 {\displaystyle 100} {\displaystyle 100} at each pass.

Assume that in the next program ( sqrtForever {\displaystyle \operatorname {sqrtForever} } {\displaystyle \operatorname {sqrtForever} }) the procedure isqrt ⁡ ( y ) {\displaystyle \operatorname {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)} {\displaystyle \operatorname {sqrtForever} (y)} will print the entire decimal representation of y {\displaystyle {\sqrt {y}}} {\displaystyle {\sqrt {y}}}.[note 3]

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

The conclusion is that algorithms which compute isqrt() are computationally equivalent to algorithms which compute sqrt().

Another derivation of y {\displaystyle {\sqrt {y}}} {\displaystyle {\sqrt {y}}} from ⌊ y ⌋ {\displaystyle \lfloor {\sqrt {y}}\rfloor } {\displaystyle \lfloor {\sqrt {y}}\rfloor } is given in section Continued fraction of √c based on isqrt below.

The integer square root of a non-negative integer y {\displaystyle y} {\displaystyle y} can be defined as ⌊ y ⌋ = max { x : x 2 ≤ y < ( x + 1 ) 2 , x ∈ N } {\displaystyle \lfloor {\sqrt {y}}\rfloor =\max\{x:x^{2}\leq y<(x+1)^{2},x\in \mathbb {N} \}} {\displaystyle \lfloor {\sqrt {y}}\rfloor =\max\{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} {\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} {\displaystyle 6^{2}>27{\text{ and }}5^{2}\ngtr 27}.

[edit]

The following Python programs are straightforward implementations.

def isqrt(y: int) -> int: """ Integer square root (linear search, ascending) """ # initial underestimate, L <= isqrt(y)
L = 0 while (L + 1) * (L + 1) <= y: L += 1

return L

def isqrt(y: int) -> int: """ Integer square root (linear search, descending) """ # initial overestimate, isqrt(y) <= R
R = y while (R * R > y): R -= 1

return R

Linear search using addition

[edit]

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.} {\displaystyle (L+1)^{2}=L^{2}+2L+1=L^{2}+1+\sum _{i=1}^{L}2.}

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

[edit]

Linear search sequentially checks every value until it hits the smallest x {\displaystyle x} {\displaystyle x} where x 2 > y {\displaystyle x^{2}>y} {\displaystyle x^{2}>y}.

A speed-up is achieved by using binary search instead.

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

Numerical examples

[ 0 , 131073 ] → [ 0 , 65536 ] → [ 0 , 32768 ] → [ 0 , 16384 ] → [ 0 , 8192 ] → [ 0 , 4096 ] → [ 0 , 2048 ] → [ 0 , 1024 ] → [ 0 , 512 ] → [ 256 , 512 ] → [ 256 , 384 ] → [ 320 , 384 ] → [ 352 , 384 ] → [ 352 , 368 ] → [ 360 , 368 ] → [ 360 , 364 ] → [ 362 , 364 ] → [ 362 , 363 ] {\displaystyle {\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}}} {\displaystyle {\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}}}

[ 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]\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}}} {\displaystyle {\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}}}

Linear search (ascending, starting from 0 {\displaystyle 0} {\displaystyle 0}) needs 1414 steps.

Algorithm using Newton's method

[edit]

One way of calculating n {\displaystyle {\sqrt {n}}} {\displaystyle {\sqrt {n}}} and isqrt ⁡ ( n ) {\displaystyle \operatorname {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} {\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.} {\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}\}} {\displaystyle \{x_{k}\}} converges quadratically to n {\displaystyle {\sqrt {n}}} {\displaystyle {\sqrt {n}}} as k → ∞ {\displaystyle k\to \infty } {\displaystyle k\to \infty }.[1][note 4]

Using only integer division

[edit]

For computing ⌊ n ⌋ {\displaystyle \lfloor {\sqrt {n}}\rfloor } {\displaystyle \lfloor {\sqrt {n}}\rfloor } 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 n > 0 {\displaystyle n>0} {\displaystyle n>0} and initial guess x 0 > 0 {\displaystyle x_{0}>0} {\displaystyle x_{0}>0}. Define the integer sequence:

x k + 1 = ⌊ x k + ⌊ n / x k ⌋ 2 ⌋ , k = 0 , 1 , 2 , … {\displaystyle x_{k+1}=\left\lfloor {\frac {x_{k}+\left\lfloor n/x_{k}\right\rfloor }{2}}\right\rfloor ,\quad k=0,1,2,\dots } {\displaystyle x_{k+1}=\left\lfloor {\frac {x_{k}+\left\lfloor n/x_{k}\right\rfloor }{2}}\right\rfloor ,\quad k=0,1,2,\dots }

Proof of convergence

[edit]

1. _Positivity:_All terms are positive integers: x k > 0 {\displaystyle x_{k}>0} {\displaystyle x_{k}>0} for all k {\displaystyle k} {\displaystyle k}.

2. Monotonicity:

so x k + 1 = ⌊ x k + ⌊ n / x k ⌋ 2 ⌋ < x k + n / x k 2 < x k {\displaystyle 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}} {\displaystyle 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}}.

Hence the sequence decreases.

so x k + 1 ≥ x k + n / x k − 1 2 > x k − 1 {\displaystyle x_{k+1}\geq {\frac {x_{k}+n/x_{k}-1}{2}}>x_{k}-1} {\displaystyle x_{k+1}\geq {\frac {x_{k}+n/x_{k}-1}{2}}>x_{k}-1}.

Hence the sequence increases or stays the same.

3. _Boundedness:_The sequence is bounded below by 1 and above by x 0 {\displaystyle x_{0}} {\displaystyle x_{0}}, so it is bounded.

4. _Stabilization / Oscillation:_A bounded monotone integer sequence either stabilizes or oscillates between two consecutive integers:

x k + 1 = x k {\displaystyle x_{k+1}=x_{k}} {\displaystyle x_{k+1}=x_{k}} or x k + 1 = x k ± 1 {\displaystyle x_{k+1}=x_{k}\pm 1} {\displaystyle x_{k+1}=x_{k}\pm 1}.

5. _Integer "Fixed-point" Condition:_At stabilization or oscillation:

x k + 1 = ⌊ ( x k + ⌊ n / x k ⌋ ) / 2 ⌋ {\displaystyle x_{k+1}=\lfloor (x_{k}+\lfloor n/x_{k}\rfloor )/2\rfloor } {\displaystyle x_{k+1}=\lfloor (x_{k}+\lfloor n/x_{k}\rfloor )/2\rfloor }.

This ensures that the sequence is either at ⌊ n ⌋ {\displaystyle \lfloor {\sqrt {n}}\rfloor } {\displaystyle \lfloor {\sqrt {n}}\rfloor } or oscillating between the two nearest integers around n {\displaystyle {\sqrt {n}}} {\displaystyle {\sqrt {n}}}.

6. _Conclusion:_The sequence eventually stabilizes at ⌊ n ⌋ {\displaystyle \lfloor {\sqrt {n}}\rfloor } {\displaystyle \lfloor {\sqrt {n}}\rfloor } or oscillates between ⌊ n ⌋ {\displaystyle \lfloor {\sqrt {n}}\rfloor } {\displaystyle \lfloor {\sqrt {n}}\rfloor } and ⌈ n ⌉ {\displaystyle \lceil {\sqrt {n}}\rceil } {\displaystyle \lceil {\sqrt {n}}\rceil }.

Remark:

Example implementation

[edit]

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

The call isqrt(2000000) converges to 1414 {\displaystyle 1414} {\displaystyle 1414} in 14 passes through while:

1000000 → 500001 → 250002 → 125004 → 62509 → 31270 → 15666 → 7896 → 4074 → 2282 → 1579 → 1422 → 1414 → 1414 {\displaystyle {\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}}} {\displaystyle {\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}}}.

One iteration is gained by setting x0 to ⌊ n / 2 ⌋ {\displaystyle \lfloor n/2\rfloor } {\displaystyle \lfloor n/2\rfloor } with the call isqrt(2000000, 1000000). 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.[note 5] When a fast computation for the integer part of the binary logarithm or for the bit-length is available (like e.g. n.bit_length() in Python), one should better start at x 0 = 2 ⌊ ( log 2 ⁡ n ) / 2 ⌋ + 1 , {\displaystyle x_{0}=2^{\lfloor (\log _{2}n)/2\rfloor +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}}} {\displaystyle {\sqrt {n}}}. In the example of the integer square root of 2000000, ⌊ log 2 ⁡ n ⌋ = 20 {\displaystyle \lfloor \log _{2}n\rfloor =20} {\displaystyle \lfloor \log _{2}n\rfloor =20}, x 0 = 2 11 = 2048 {\displaystyle 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.} {\displaystyle 2048\rightarrow 1512\rightarrow 1417\rightarrow 1414\rightarrow 1414.} In this case only four iteration steps are needed. This corresponds to the call isqrt(2000000, 2048).

Digit-by-digit algorithm

[edit]

The traditional pen-and-paper algorithm for computing the square root n {\displaystyle {\sqrt {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} {\displaystyle \leq n}. If stopping after the one's place, the result computed will be the integer square root.

Using bitwise operations

[edit]

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 isqrt_recursive(n: int) -> int: assert n >= 0, "n must be a non-negative integer" 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

Equivalent non-recursive program:[2][note 6]

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

# "one" starts at the highest power of four <= x
one = 1
while one <= op:
    one <<= 2
one >>= 2

while one != 0:
    if op >= res + one:
        op -= res + one
        res += 2 * one
    res //= 2
    one //= 4

return res

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.[2]

Karatsuba square root algorithm

[edit]

The Karatsuba square root algorithm applies the same divide-and-conquer principle as the Karatsuba multiplication algorithm to compute integer square roots. The method was formally analyzed by Paul Zimmermann (1999).[3] 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.

Paul Zimmermann (1999) gives the following algorithm.[3]

Algorithm SqrtRem ( n = a 3 b 3 + a 2 b 2 + a 1 b + a 0 ) {\displaystyle {\text{Algorithm }}{\text{SqrtRem}}(n=a_{3}b^{3}+a_{2}b^{2}+a_{1}b+a_{0})} {\displaystyle {\text{Algorithm }}{\text{SqrtRem}}(n=a_{3}b^{3}+a_{2}b^{2}+a_{1}b+a_{0})}

Input: 0 ≤ a i < b , with a 3 ≥ b / 4 {\displaystyle {\text{Input: }}0\leq a_{i}<b,{\text{ with }}a_{3}\geq b/4} {\displaystyle {\text{Input: }}0\leq a_{i}<b,{\text{ with }}a_{3}\geq b/4}

Output: ( s , r ) such that s 2 ≤ n = s 2 + r < ( s + 1 ) 2 {\displaystyle {\text{Output: }}(s,r){\text{ such that }}s^{2}\leq n=s^{2}+r<(s+1)^{2}} {\displaystyle {\text{Output: }}(s,r){\text{ such that }}s^{2}\leq n=s^{2}+r<(s+1)^{2}}

( s ′ , r ′ ) ← SqrtRem ( a 3 b + a 2 ) {\displaystyle \qquad (s',r')\gets {\text{SqrtRem}}(a_{3}b+a_{2})} {\displaystyle \qquad (s',r')\gets {\text{SqrtRem}}(a_{3}b+a_{2})}

( q , u ) ← DivRem ( r ′ b + a 1 , 2 s ′ ) {\displaystyle \qquad (q,u)\gets {\text{DivRem}}(r'b+a_{1},2s')} {\displaystyle \qquad (q,u)\gets {\text{DivRem}}(r'b+a_{1},2s')}

s ← s ′ b + q {\displaystyle \qquad s\gets s'b+q} {\displaystyle \qquad s\gets s'b+q}

r ← u b + a 0 − q 2 {\displaystyle \qquad r\gets ub+a_{0}-q^{2}} {\displaystyle \qquad r\gets ub+a_{0}-q^{2}}

if r < 0 then {\displaystyle \qquad {\text{if }}r<0{\text{ then }}} {\displaystyle \qquad {\text{if }}r<0{\text{ then }}}

r ← r + 2 s − 1 {\displaystyle \qquad \qquad r\gets r+2s-1} {\displaystyle \qquad \qquad r\gets r+2s-1}

s ← s − 1 {\displaystyle \qquad \qquad s\gets s-1} {\displaystyle \qquad \qquad s\gets s-1}

return ( s , r ) {\displaystyle \qquad {\text{return }}(s,r)} {\displaystyle \qquad {\text{return }}(s,r)}

Because only one recursive call is made per level, the total complexity remains O ( n ) {\displaystyle O(n)} {\displaystyle O(n)} in the number of bits. Each level performs only linear-time arithmetic on half-size numbers.

Comparison with Karatsuba multiplication

[edit]

Property Karatsuba multiplication Karatsuba-style square root
Recursive calls per level 3 1
Recurrence T ( n ) = 3 T ( n / 2 ) + O ( n ) {\displaystyle T(n)=3T(n/2)+O(n)} {\displaystyle T(n)=3T(n/2)+O(n)} T ( n ) = T ( n / 2 ) + O ( n ) {\displaystyle T(n)=T(n/2)+O(n)} {\displaystyle T(n)=T(n/2)+O(n)}
Asymptotic complexity O ( n log 2 ⁡ 3 ) ≈ O ( n 1.585 ) {\displaystyle O(n^{\log _{2}3})\!\approx \!O(n^{1.585})} {\displaystyle O(n^{\log _{2}3})\!\approx \!O(n^{1.585})} O ( n ) {\displaystyle O(n)} {\displaystyle O(n)}
Key operation Three partial multiplications and recombination One recursive square root and algebraic correction

The Karatsuba-style square root is mainly used for arbitrary-precision arithmetic on very large integers, where it combines efficiently with Burnikel–Ziegler division [de] and Karatsuba multiplication. It was first analyzed formally by Paul Zimmermann (1999).[3] Earlier practical work includes Martin Guy (1985),[2] and recursive versions appear in Donald Knuth (1998).[4] Modern GMP and MPIR libraries implement similar recursive techniques.

Implementation in Python

[edit]

The Python program below implements Zimmermann’s algorithm. Given an integer n ≥ 0 {\displaystyle n\geq 0} {\displaystyle n\geq 0}, SqrtRem computes simultaneously its integer square root s = ⌊ n ⌋ {\displaystyle s=\lfloor {\sqrt {n}}\rfloor } {\displaystyle s=\lfloor {\sqrt {n}}\rfloor } and the corresponding remainder r = n − s 2 {\displaystyle r=n-s^{2}} {\displaystyle r=n-s^{2}}. The choice of isqrt() is ad libitum.[note 5]

def SqrtRem(n: int, word_bits: int = 32) -> tuple[int, int]: """ Implementation based on Zimmermann's Karatsuba-style integer square root algorithm [Zimmermann, 1999]. It recursively splits the input n into "limbs" of size word_bits and combines partial results to compute the integer square root.

Args:
    n (int): Non-negative integer to compute the square root of.
    word_bits (int, optional): Number of bits per "limb" or chunk
        used when recursively splitting n. Default is 32. Each
        limb represents a fixed-size part of n for the algorithm.

Returns:
    tuple[int, int]: s = integer square root of n, r = remainder (n - s*s).

Notes:
    The limb size controls recursion granularity. Larger word_bits
    reduces recursion depth but increases the size of subproblems;
    smaller word_bits increases recursion depth but works on smaller chunks.

Reference:
    Zimmermann, P. (1999). "Karatsuba Square Root", Research report #3805, Inria.
    Archived: https://inria.hal.science/inria-00072854v1/file/RR-3805.pdf
"""
if n < 0:
    raise ValueError("n must be non-negative")
if n == 0:
    return 0, 0  # trivial case

# Determine number of word-sized limbs (mimics “limb” splitting in Zimmermann)
limblen = (n.bit_length() + word_bits - 1) // word_bits

# Base case: single limb — compute directly
if limblen <= 1:
    s = isqrt(n)  # any isqrt, e.g., math.isqrt or custom
    r = n - s*s
    return s, r

# --- Step 1: Split n into high and low parts ---
half_limbs = limblen // 2
shift = half_limbs * word_bits
hi = n >> shift              # high half, corresponds to a3*b + a2
lo = n & ((1 << shift) - 1)  # low half, corresponds to a1*b + a0

# --- Step 2: Recursive call on the high part ---
s_high, r_high = SqrtRem(hi, word_bits)  # approximate sqrt of high half

# --- Step 3: Recombine to approximate full sqrt ---
quarter = shift // 2
numerator = (r_high << quarter) | (lo >> quarter)   # simulate Zimmermann’s DivRem step
denominator = s_high << 1                           # 2*s' term

q = numerator // denominator if denominator else 0  # integer division
s_candidate = (s_high << quarter) + q               # recombine high and low

# --- Step 4: Verification and correction ---
# Ensure remainder is non-negative and s*s <= n < (s+1)*(s+1)
s = s_candidate
r = n - s*s

while r < 0:                 # overestimate correction
    s -= 1
    r = n - s*s
while (s + 1)*(s + 1) <= n:  # underestimate correction
    s += 1
    r = n - s*s

return s, r

Example usage

for n in [(2**32) + 5, 12345678901234567890, (1 << 1512) - 1]:
    s, r = SqrtRem(n)
    print(f"SqrtRem({n}) = {s}, remainder = {r}")
Computation
SqrtRem(4294967301) = 65536, remainder = 5
SqrtRem(12345678901234567890) = 3513641828, remainder = 5763386306
SqrtRem(143665816004337822710282600285310394341474369045835074863414468709543787931907367746179403311452034095731304066234112071267510472464260955153084575408147254672957261763907982395337943906645864229014250227057207826232751957053220218983971305018634078800548055251973907806245884614087189937340865371691338441989956445051526543084039211962387469415699218979531585795574920384684004258007709014706216763392717018544247025174258411677231986785008489302218244095) = 379032737378102767370356320425415662904513187772631008578870126471203845870697482014374611530431269030880793627229265919475483409207718357286202948008100864063587640630090308972232735749901964068667724412528434753635948938919935, remainder = 758065474756205534740712640850831325809026375545262017157740252942407691741394964028749223060862538061761587254458531838950966818415436714572405896016201728127175281260180617944465471499803928137335448825056869507271897877839870

In programming languages

[edit]

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.

Programming language Example use Version introduced
Chapel BigInteger.sqrt(result, n);[5]BigInteger.sqrtRem(result, remainder, n); Unknown
Common Lisp (isqrt n)[6] Unknown
Crystal Math.isqrt(n)[7] 1.2
Java n.sqrt()[8] (BigInteger only) 9
Julia isqrt(n)[9] 0.3
Maple isqrt(n)[10] Unknown
PARI/GP sqrtint(n)[11] 1.35a[12] (as isqrt) or before
PHP sqrt($num)[13] 4
Python math.isqrt(n)[14] 3.8
Racket (integer-sqrt n)[15](integer-sqrt/remainder n) Unknown
Ruby Integer.sqrt(n)[16] 2.5.0
Rust n.isqrt()[17]n.checked_isqrt()[18] 1.84.0
SageMath isqrt(n)[19] Unknown
Scheme (exact-integer-sqrt n)[20] R6RS
Tcl isqrt($n)[21] 8.5
Zig std.math.sqrt(n)[22] Unknown

Continued fraction of √c based on isqrt

[edit]

The computation of the simple continued fraction of c {\displaystyle {\sqrt {c}}} {\displaystyle {\sqrt {c}}} can be carried out using only integer operations, with isqrt ⁡ ( c ) {\displaystyle \operatorname {isqrt} (c)} {\displaystyle \operatorname {isqrt} (c)} serving as the initial term. The algorithm[23] generates continued fraction expansion in canonical form.

Let a 0 = ⌊ c ⌋ {\displaystyle a_{0}=\lfloor {\sqrt {c}}\rfloor } {\displaystyle a_{0}=\lfloor {\sqrt {c}}\rfloor }be the integer square root of c {\displaystyle c} {\displaystyle c}.

If c {\displaystyle c} {\displaystyle c} is a perfect square, the continued fraction terminates immediately:

c = [ a 0 ] . {\displaystyle {\sqrt {c}}=[a_{0}].} {\displaystyle {\sqrt {c}}=[a_{0}].}

Otherwise, the continued fraction is periodic:

c = [ a 0 ; a 1 , a 2 , … , a m ¯ ] {\displaystyle {\sqrt {c}}=[a_{0};{\overline {a_{1},a_{2},\dots ,a_{m}}}]} {\displaystyle {\sqrt {c}}=[a_{0};{\overline {a_{1},a_{2},\dots ,a_{m}}}]},

where the overline indicates the repeating part.

The continued fraction can be obtained by the following recurrence, which uses only integer arithmetic:

m 0 = 0 , d 0 = 1 , a 0 = ⌊ c ⌋ . {\displaystyle m_{0}=0,\quad d_{0}=1,\quad a_{0}=\lfloor {\sqrt {c}}\rfloor .} {\displaystyle m_{0}=0,\quad d_{0}=1,\quad a_{0}=\lfloor {\sqrt {c}}\rfloor .}

For k ≥ 0 {\displaystyle k\geq 0} {\displaystyle k\geq 0},

m k + 1 = d k a k − m k , d k + 1 = c − m k + 1 2 d k , a k + 1 = ⌊ a 0 + m k + 1 d k + 1 ⌋ . {\displaystyle 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 .} {\displaystyle 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 .}

Since there are only finitely many possible triples ( m k , d k , a k ) {\displaystyle (m_{k},d_{k},a_{k})} {\displaystyle (m_{k},d_{k},a_{k})}, eventually one repeats, and from that point onward the continued fraction becomes periodic.

Implementation in Python

[edit]

On input c {\displaystyle c} {\displaystyle c}, a non-negative integer, the following program computes the simple continued fraction of c {\displaystyle {\sqrt {c}}} {\displaystyle {\sqrt {c}}}. The integer square root ⌊ c ⌋ {\displaystyle \lfloor {\sqrt {c}}\rfloor } {\displaystyle \lfloor {\sqrt {c}}\rfloor } is computed once.[note 5] Only integer arithmetic is used. The program outputs [ a 0 , ( a 1 , a 2 , . . . , a m ) ] {\displaystyle [a0,(a1,a2,...,am)]} {\displaystyle [a0,(a1,a2,...,am)]}, where the second element is the periodic part.

def continued_fraction_sqrt(c: int) -> tuple[int, tuple[int, ...]]: """ Compute the continued fraction of sqrt(c) using integer arithmetic. Returns [a0, (a1, a2, ..., am)] where the second element is the periodic part. For perfect squares, the period is empty. """ a0 = isqrt(c)

# Perfect square: return period empty
if a0 * a0 == c:
    return (a0, ())

m, d, a = 0, 1, a0
period = []
seen = set()

while True:
    m_next = d * a - m
    d_next = (c - m_next * m_next) // d
    a_next = (a0 + m_next) // d_next

    if (m_next, d_next, a_next) in seen:
        break

    seen.add((m_next, d_next, a_next))
    period.append(a_next)
    m, d, a = m_next, d_next, a_next

return (a0, tuple(period))

Example usage

for c in list(range(0, 18)) + [114] + [4097280036]:
    cf = continued_fraction_sqrt(c)
    print(f"sqrt({c}): {cf}")

Output

Input (c) Output (cf) Continued fraction
0 [0] 0 = 0 {\displaystyle {\sqrt {0}}=0} {\displaystyle {\sqrt {0}}=0}
1 [1] 1 = 1 {\displaystyle {\sqrt {1}}=1} {\displaystyle {\sqrt {1}}=1}
2 [1; (2,)] 2 = [ 1 ; 2 ¯ ] {\displaystyle {\sqrt {2}}=[1;{\overline {2}}]} {\displaystyle {\sqrt {2}}=[1;{\overline {2}}]}
3 [1; (1, 2)] 3 = [ 1 ; 1 , 2 ¯ ] {\displaystyle {\sqrt {3}}=[1;{\overline {1,2}}]} {\displaystyle {\sqrt {3}}=[1;{\overline {1,2}}]}
4 [2] 4 = 2 {\displaystyle {\sqrt {4}}=2} {\displaystyle {\sqrt {4}}=2}
5 [2; (4,)] 5 = [ 2 ; 4 ¯ ] {\displaystyle {\sqrt {5}}=[2;{\overline {4}}]} {\displaystyle {\sqrt {5}}=[2;{\overline {4}}]}
6 [2; (2, 4)] 6 = [ 2 ; 2 , 4 ¯ ] {\displaystyle {\sqrt {6}}=[2;{\overline {2,4}}]} {\displaystyle {\sqrt {6}}=[2;{\overline {2,4}}]}
7 [2; (1, 1, 1, 4)] 7 = [ 2 ; 1 , 1 , 1 , 4 ¯ ] {\displaystyle {\sqrt {7}}=[2;{\overline {1,1,1,4}}]} {\displaystyle {\sqrt {7}}=[2;{\overline {1,1,1,4}}]}
8 [2; (1, 4)] 8 = [ 2 ; 1 , 4 ¯ ] {\displaystyle {\sqrt {8}}=[2;{\overline {1,4}}]} {\displaystyle {\sqrt {8}}=[2;{\overline {1,4}}]}
9 [3] 9 = 3 {\displaystyle {\sqrt {9}}=3} {\displaystyle {\sqrt {9}}=3}
10 [3; (6,)] 10 = [ 3 ; 6 ¯ ] {\displaystyle {\sqrt {10}}=[3;{\overline {6}}]} {\displaystyle {\sqrt {10}}=[3;{\overline {6}}]}
11 [3; (3, 6)] 11 = [ 3 ; 3 , 6 ¯ ] {\displaystyle {\sqrt {11}}=[3;{\overline {3,6}}]} {\displaystyle {\sqrt {11}}=[3;{\overline {3,6}}]}
12 [3; (2, 6)] 12 = [ 3 ; 2 , 6 ¯ ] {\displaystyle {\sqrt {12}}=[3;{\overline {2,6}}]} {\displaystyle {\sqrt {12}}=[3;{\overline {2,6}}]}
13 [3; (1, 1, 1, 1, 6)] 13 = [ 3 ; 1 , 1 , 1 , 1 , 6 ¯ ] {\displaystyle {\sqrt {13}}=[3;{\overline {1,1,1,1,6}}]} {\displaystyle {\sqrt {13}}=[3;{\overline {1,1,1,1,6}}]}
14 [3; (1, 2, 1, 6)] 14 = [ 3 ; 1 , 2 , 1 , 6 ) ¯ ] {\displaystyle {\sqrt {14}}=[3;{\overline {1,2,1,6)}}]} {\displaystyle {\sqrt {14}}=[3;{\overline {1,2,1,6)}}]}
15 [3; (1, 6)] 15 = [ 3 ; 1 , 6 ¯ ] {\displaystyle {\sqrt {15}}=[3;{\overline {1,6}}]} {\displaystyle {\sqrt {15}}=[3;{\overline {1,6}}]}
16 [4] 16 = 4 {\displaystyle {\sqrt {16}}=4} {\displaystyle {\sqrt {16}}=4}
17 [4; (8,)] 17 = [ 4 ; 8 ¯ ] {\displaystyle {\sqrt {17}}=[4;{\overline {8}}]} {\displaystyle {\sqrt {17}}=[4;{\overline {8}}]}
114 [10; (1, 2, 10, 2, 1, 20)] 114 = [ 10 ; 1 , 2 , 10 , 2 , 1 , 20 ¯ ] {\displaystyle {\sqrt {114}}=[10;{\overline {1,2,10,2,1,20}}]} {\displaystyle {\sqrt {114}}=[10;{\overline {1,2,10,2,1,20}}]}[note 7]
4097280036 [64009; (1, 1999, 3, 4, 1, 499, 3, 1, 3, 3, 1, 124, ... ..., 3, 1, 3, 499, 1, 4, 3, 1999, 1, 128018)]period: 13,032 terms[note 8]
  1. ^ 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.

  2. ^ It is no surprise that the repeated multiplication by 100 is a feature in Jarvis (2006)

  3. ^ The fractional part of square roots of perfect squares is rendered as 000....

  4. ^ See Heron's method.

  5. ^ a b c Newton's method can be given as follows
    (with the initial guess set to s {\displaystyle s} {\displaystyle s}):
    def isqrt(s: int) -> int:
    """isqrt via Newton/Heron iteration."""
    L, R = 1, s
    while L < R:
    R = L + ((R - L) // 2)
    L = s // R
    return R
    Computations
    [ 1 , 2000000 ] → [ 2 , 1000000 ] → [ 3 , 500001 ] → [ 7 , 250002 ] → [ 15 , 125004 ] → [ 31 , 62509 ] → [ 63 , 31270 ] → [ 127 , 15666 ] → [ 253 , 7896 ] → [ 490 , 4074 ] → [ 876 , 2282 ] → [ 1266 , 1579 ] → [ 1406 , 1422 ] → [ 1414 , 1414 ] {\displaystyle {\begin{aligned}&[1,2000000]\to [2,1000000]\to [3,500001]\\&\to [7,250002]\to [15,125004]\to [31,62509]\\&\to [63,31270]\to [127,15666]\to [253,7896]\\&\to [490,4074]\to [876,2282]\to [1266,1579]\\&\to [1406,1422]\to [1414,1414]\end{aligned}}} {\displaystyle {\begin{aligned}&[1,2000000]\to [2,1000000]\to [3,500001]\\&\to [7,250002]\to [15,125004]\to [31,62509]\\&\to [63,31270]\to [127,15666]\to [253,7896]\\&\to [490,4074]\to [876,2282]\to [1266,1579]\\&\to [1406,1422]\to [1414,1414]\end{aligned}}}
    One sees that the performance gain of Newton's method over binary search is due to the fact that ⌊ s ⌋ {\displaystyle \lfloor {\sqrt {s}}\rfloor } {\displaystyle \lfloor {\sqrt {s}}\rfloor } is approached simultaneously from Left and Right, whereas binary search adjusts only one side at each iteration.

  6. ^ The algorithm is explained in Square_root_algorithms#Binary numeral system (base 2)

  7. ^ See the example in the article Periodic continued fraction.

  8. ^ The continued fraction expansion of 4097280036 {\displaystyle {\sqrt {4097280036}}} {\displaystyle {\sqrt {4097280036}}} has a period of 13,032 terms. While Python is unable to display the entire sequence on screen due to its length, writing the output to a file completes successfully.

  9. ^ Johnson, S. G. (4 February 2015). "Square Roots via Newton's Method" (PDF). MIT Course 18.335: Introduction to Numerical Methods. Retrieved 12 October 2025.

  10. ^ a b c Guy, Martin (1985). "Fast integer square root by Mr. Woo's abacus algorithm". University of Kent at Canterbury (UKC). Archived from the original on 6 March 2012. Retrieved 5 October 2025.

  11. ^ a b c Zimmermann, Paul (1999). "Karatsuba Square Root" (PDF). Research report #3805. Inria (published 24 May 2006). Archived (PDF) from the original on 11 May 2023.

  12. ^ Knuth, Donald E. (1998). The Art of Computer Programming, Volume 2: Seminumerical Algorithms (3rd ed.). Addison–Wesley. ISBN 9780201896848.

  13. ^ "BigInteger - Chapel Documentation 2.1". Chapel Documentation - Chapel Documentation 2.1.

  14. ^ "CLHS: Function SQRT, ISQRT". Common Lisp HyperSpec (TM).

  15. ^ "Math - Crystal 1.13.2". The Crystal Programming Language API docs.

  16. ^ "BigInteger (Java SE 21 & JDK 21)". JDK 21 Documentation.

  17. ^ "Mathematics - The Julia Language". Julia Documentation - The Julia Language.

  18. ^ "iroot- Maple Help". Help - Maplesoft.

  19. ^ "Catalogue of GP/PARI Functions: Arithmetic functions". PARI/GP Development Headquarters.

  20. ^ "Index of /archive/science/math/multiplePrecision/pari/". PSG Digital Resources. Archived from the original on 6 November 2024.

  21. ^ "Mathematical functions". PHP Documentation.

  22. ^ "Mathematical functions". Python Standard Library documentation.

  23. ^ "4.3.2 Generic Numerics". Racket Documentation.

  24. ^ "class Integer - RDoc Documentation". RDoc Documentation.

  25. ^ "i32 - Rust". std - Rust.

  26. ^ "i32 - Rust". std - Rust.

  27. ^ "Elements of the ring ℤ of integers - Standard Commutative Rings". SageMath Documentation.

  28. ^ "Revised7 Report on the Algorithmic Language Scheme". Scheme Standards.

  29. ^ "mathfunc manual page - Tcl Mathematical Functions". Tcl/Tk 8.6 Manual.

  30. ^ "std.math.sqrt.sqrt - Zig Documentation". Home ⚡ Zig Programming Language.

  31. ^ Beceanu, Marius (5 February 2003). "Period of the Continued Fraction of sqrt(n)" (PDF). Theorem 2.3. Archived (PDF) from the original on 21 December 2015. Retrieved 5 October 2025.