Integer square root

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,

For example, because and .

Algorithm using Newton's methodEdit

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


The sequence   converges quadratically to   as  . It can be proven that if   is chosen as the initial guess, one can stop as soon as


to ensure that  

Using only integer divisionEdit

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


By using the fact that


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

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

Domain of computationEdit

Although   is irrational for many  , the sequence   contains only rational terms when   is rational. Thus, with this method it is unnecessary to exit the field of rational numbers in order to calculate  , a fact which has some theoretical advantages.

Stopping criterionEdit

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


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

Example implementation in CEdit

static inline unsigned ceil_log2(unsigned long n) {
  return sizeof(unsigned long) * CHAR_BIT - __builtin_clzl(n) - 1;

unsigned long newt_sqrt(unsigned long s) {
  if (s < 4) return (s + 1) / 2;

  // Binary estimate.
  unsigned n = (ceil_log2(s) + 1) / 2;
  unsigned long r = (1UL << (n - 1)) + (s >> (n + 1));
  unsigned long r_o = 0;
  // Iterate.
  for (i = 0; i < 3 && abs(r - r_o) > 1; i++) {
    r_o = r;
    r = (r + s / r) >> 1;  // division can be slow.
  return r;

Digit-by-digit algorithmEdit

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 operationsEdit

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 integer_sqrt(n: int) -> int:
    assert n >= 0, "sqrt works for only non-negative inputs"
    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
        return large_cand

# equivalently:
def integer_sqrt_iter(n: int) -> int:
    assert n >= 0, "sqrt works for only non-negative inputs"
    if n < 2:
        return n

    # Find the shift amount. See also [[find first set]],
    # shift = ceil(log2(n) * 0.5) * 2 = ceil(ffs(n) * 0.5) * 2
    shift = 2
    while (n >> shift) != 0:
        shift += 2

    # Unroll the bit-setting loop.
    result = 0
    while shift > 0:
        result = result << 1
        large_cand = (
            result + 1
        )  # Same as result ^ 1 (xor), because the last bit is always 0.
        if large_cand * large_cand <= n >> shift:
            result = large_cand
        shift -= 2

    return result

Traditional pen-and-paper presentations of the digit-by-digit algorithm include various optimisations not present in the code above, in particular the trick of presubtracting the square of the previous digits which makes a general multiplication step unnecessary. See Methods of computing square roots § Woo abacus for an example.[1]

See alsoEdit


External linksEdit