my-server
← Wiki

Integer square root

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 ,

For example,

Introductory remark

Let and be non-negative integers.

Algorithms that compute (the decimal representation of) run forever on each input which is not a perfect square.

Algorithms that compute do not run forever. They are nevertheless capable of computing up to any desired accuracy .

Choose any and compute .

For example (setting ):

Compare the results with

It appears that the multiplication of the input by gives an accuracy of decimal digits.

To compute the (entire) decimal representation of , one can execute an infinite number of times, increasing by a factor at each pass.

Assume that in the next program () the procedure is already defined and — for the sake of the argument — that all variables can hold integers of unlimited magnitude.

Then will print the entire decimal representation of .

The conclusion is that algorithms which compute are computationally equivalent to algorithms which compute.

Another derivation of from is given in section Continued fraction of √c based on isqrt below.

Basic algorithms

The integer square root of a non-negative integer can be defined as

For example, because .

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

Algorithm using binary search

Linear search sequentially checks every value until it hits the smallest where .

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

Numerical examples

  • , .
  • Using binary search the computation of converges to in iteration steps via the sequence
  • The computation of converges to in steps via the sequence
Linear search (ascending, starting from ) needs steps.

Algorithm using Newton's method

One way of calculating and is to use Heron's method, which is a special case of Newton's method, to find a solution for the equation , giving the iterative formula

The sequence converges quadratically to as .

Using only integer division

For computing 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 and initial guess . Define the integer sequence:

Proof of convergence

1. Positivity: All terms are positive integers: for all .

2. Monotonicity:

  • If , then ;
so .
Hence the sequence decreases.
  • If , then ;
so .
Hence the sequence increases or stays the same.

3. Boundedness: The sequence is bounded below by 1 and above by , so it is bounded.

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

or .

5. Integer "Fixed-point" Condition: At stabilization or oscillation:

.
This ensures that the sequence is either at or oscillating between the two nearest integers around .

6. Conclusion: The sequence eventually stabilizes at or oscillates between and .

Remark:

  • is a strict fixed point unless is a perfect square.
  • If is a perfect square, the sequence oscillates between and .

Example implementation

Numerical examples

The call <code>isqrt(2000000)</code> converges to in 14 passes through <code>while</code>:

.

One iteration is gained by setting <code>x0</code> to 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 which is the least power of two bigger than . In the example of the integer square root of , , , and the resulting sequence is 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 is based on working from higher digit places to lower, and as each new digit pick the largest that will still yield a square . 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>&lt;&lt;</code> being left shift, and <code>&gt;&gt;</code> being logical right shift, a recursive algorithm to find the integer square root of any natural number is:

Equivalent non-recursive program:

See for an example.

Karatsuba square root algorithm

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). 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.

Because only one recursive call is made per level, the total complexity remains in the number of bits. Each level performs only linear-time arithmetic on half-size numbers.

Comparison with Karatsuba multiplication

Use and history

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

Implementation in Python

The Python program below implements Zimmermann’s algorithm. Given an integer , <code>SqrtRem</code> computes simultaneously its integer square root and the corresponding remainder . The choice of <code>isqrt()</code> is ad libitum.

Example usage

In programming languages

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.

Continued fraction of √c based on isqrt

The computation of the simple continued fraction of can be carried out using only integer operations, with serving as the initial term. The algorithm generates continued fraction expansion in canonical form.

Let be the integer square root of .

If is a perfect square, the continued fraction terminates immediately:

Otherwise, the continued fraction is periodic:

,

where the overline indicates the repeating part.

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

For ,

Since there are only finitely many possible triples , eventually one repeats, and from that point onward the continued fraction becomes periodic.

Implementation in Python

On input , a non-negative integer, the following program computes the simple continued fraction of . The integer square root is computed once. Only integer arithmetic is used. The program outputs , where the second element is the periodic part.

Example usage

Output

See also

Notes

References

External links