Harman Patil (Editor)

Integer square root

Updated on
Edit
Like
Comment
Share on FacebookTweet on TwitterShare on LinkedInShare on Reddit

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,

Contents

isqrt ( n ) = n .

For example, isqrt ( 27 ) = 5 because 5 5 = 25 27 and 6 6 = 36 > 27 .

Algorithm

One way of calculating n and isqrt ( n ) is to use Newton's method to find a solution for the equation x 2 n = 0 , giving the iterative formula

x k + 1 = 1 2 ( x k + n x k ) , k 0 , x 0 > 0.

The sequence { x k } converges quadratically to n as k . It can be proven that if x 0 = n is chosen as the initial guess, one can stop as soon as

| x k + 1 x k | < 1

to ensure that x k + 1 = n .

Using only integer division

For computing n 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 .

By using the fact that

1 2 ( x k + n x k ) = 1 2 ( x k + n x k ) ,

one can show that this will reach n within a finite number of iterations.

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

Using bitwise 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:

function integerSqrt(n): if n < 0: error "integerSqrt works for only nonnegative inputs" else if n < 2: return n else: smallCandidate = integerSqrt(n >> 2) << 1 largeCandidate = smallCandidate + 1 if largeCandidate*largeCandidate > n: return smallCandidate else: return largeCandidate

Or, iteratively instead of recursively:

function integerSqrt(n): if n < 0: error "integerSqrt works for only nonnegative inputs" # Find greatest shift. shift = 2 nShifted = n >> shift # We check for nShifted being n, since some implementations of logical right shifting shift modulo the word size. while nShifted ≠ 0 and nShifted ≠ n: shift = shift + 2 nShifted = n >> shift shift = shift - 2 # Find digits of result. result = 0 while shift ≥ 0: result = result << 1 candidateResult = result + 1 if candidateResult*candidateResult ≤ n >> shift: result = candidateResult shift = shift - 2 return result

Domain of computation

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

Stopping criterion

One can prove that c = 1 is the largest possible number for which the stopping criterion

| x k + 1 x k | < c  

ensures x k + 1 = n 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.

References

Integer square root Wikipedia