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⌋. For example, isqrt(27)=⌊27⌋=⌊5.19615242270663...⌋=5.
Introductory remark Let y and k be non-negative integers. Algorithms that compute (the decimal representation of) y run forever on each input y which is not a perfect square. Algorithms that compute ⌊y⌋ do not run forever, but are nevertheless capable of computing y up to any desired accuracy k. Choose any k and compute ⌊y×100k⌋. For example (setting y=2): k=0:⌊2×1000⌋=⌊2⌋=1k=1:⌊2×1001⌋=⌊200⌋=14k=2:⌊2×1002⌋=⌊20000⌋=141k=3:⌊2×1003⌋=⌊2000000⌋=1414⋮k=8:⌊2×1008⌋=⌊20000000000000000⌋=141421356⋮ Compare the results with 2=1.41421356237309504880168872420969807856967187537694... It appears that the multiplication of the input by 100k gives an accuracy of k decimal digits. To compute the (entire) decimal representation of y, one can execute isqrt(y) an infinite number of times, increasing y by a factor 100 at each pass. Assume that in the next program (sqrtForever) the procedure isqrt(y) is already defined and — for the sake of the argument — that all variables can hold integers of unlimited magnitude. Then sqrtForever(y) will print the entire decimal representation of y.
The conclusion is that algorithms which compute isqrt() are computationally equivalent to algorithms which compute sqrt(). Another derivation of y from ⌊y⌋ is given in section Continued fraction of √c based on isqrt below.
Basic algorithms The integer square root of a non-negative integer y can be defined as ⌊y⌋=max{x:x2≤y<(x+1)2,x∈ℕ} For example, isqrt(27)=⌊27⌋=5 because 52≤27 and 27<62. Since isqrt is a computable function, its graph is Diophantine by the MRDP theorem. This is easily made explicit: for non-negative integers x and y, x=isqrt(y) holds if and only if there are non-negative integers a and b with y=x2+aanda+b=2x, since the first equation expresses x2≤y and, given the first, the second is equivalent to y<(x+1)2. The two equations can be combined into the single Diophantine equation (y−x2−a)2+(a+b−2x)2=0. Here a=y−x2 is the remainder, as returned by SqrtRem below. All unknowns are bounded by 2y, so a solution can always be found by finite search; the ascending linear search below is such a search.
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 (L+1)2=L2+2L+1=L2+1+∑i=1L2.
Algorithm using binary search Linear search sequentially checks every value until it hits the smallest x where x2>y. A speed-up is achieved by using binary search instead.
Numerical examples
isqrt(0)=0, isqrt(1)=1. Using binary search the computation of isqrt(131072) converges to 362 in 17 iteration steps via the [L,R] sequence [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] The computation of isqrt(2000000) converges to 1414 in 21 steps via the [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] Linear search (ascending, starting from 0) needs 1414 steps.
Algorithm using Newton's method One way of calculating n and isqrt(n) is to use Heron's method, which is a special case of Newton's method, to find a solution for the equation x2−n=0, giving the iterative formula xk+1=12(xk+nxk),k≥0,x0>0. The sequence {xk} converges quadratically to n as k→∞.
Using only integer division For computing ⌊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 unnecessary. Let n>0 and initial guess x0>0. Define the integer sequence:
xk+1=⌊xk+⌊n/xk⌋2⌋,k=0,1,2,…
Proof of convergence By definition, the sequence xk consists entirely of positive integers. We must prove it eventually reaches r=⌊n⌋. In fact, we might as well assume x0≥r. Indeed, given any xk>0 we have
r2xk−(2r−xk)=(r−xk)2xk≥0 Thus, ⌊r2/xk⌋≥2r−xk because 2r−xk is an integer, so (noting n≥r2)
⌊n/xk⌋≥⌊r2/xk⌋≥2r−xk. Therefore,
xk+1≥⌊xk+2r−xk2⌋=r. That is, no matter the initial x0>0, the next value x1 is at least r. We have the following monotonicity property:
If xk≥r+1>n, then n/xk<xk, and so (using ⌊n/xk⌋≤n/xk) xk+1=⌊xk+⌊n/xk⌋2⌋≤xk+n/xk2<xk. That is, the sequence decreases. Therefore, the sequence eventually reaches r: if xk>r, it must strictly decrease but never go below r by the first argument. Moreover, if xk=r, a similar calculation to the above shows r≤xk+1≤r+1, so the sequence either stabilizes at r or oscillates between r and r+1. The oscillating case occurs precisely when n=(r+1)2−1, i.e. when n+1 is a perfect square, in which case r+1=⌈n⌉.
Example implementation
Numerical examples The call isqrt(2000000) converges to 1414 in 14 passes through while:
1000000→500001→250002→125004→62509→31270→15666→7896→4074→2282→1579→1422→1414→1414. One iteration is gained by setting x0 to ⌊n/2⌋ 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. 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 x0=2⌊(log2n)/2⌋+1, which is the least power of two bigger than n. In the example of the integer square root of 2000000, ⌊log2n⌋=20, x0=211=2048, and the resulting sequence is 2048→1512→1417→1414→1414. In this case only four iteration steps are needed. This corresponds to the call isqrt(2000000, 2048).
Generalization to Nth roots Newton's method may be applied to compute ⌊nN⌋ in a manner entirely analogous to the N=2 case. Using integer division, the sequence becomes
… excerpt ends here. Continue reading the full article.