Integer square root

From testwiki
Jump to navigation Jump to search

Template:Short description In number theory, the integer square root (isqrt) of a non-negative integer Template:Mvar is the non-negative integer Template:Mvar which is the greatest integer less than or equal to the square root of Template:Mvar, 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.[note 1]

Algorithms that compute y do not run forever. They 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=1414k=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 Template:Mvar decimal digits.[note 2]

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.[note 3]

// Print sqrt(y), without halting
void sqrtForever(unsigned int y)
{
	unsigned int result = isqrt(y);
	printf("%d.", result);	// print result, followed by a decimal point

	while (true)	// repeat forever ...
	{
		y = y * 100;	// theoretical example: overflow is ignored
		result = isqrt(y);
		printf("%d", result % 10);	// print last digit of result
	}
}

The conclusion is that algorithms which compute Template:Code are computationally equivalent to [[Methods of computing square roots|algorithms which compute Template:Code]].

Basic algorithms

The integer square root of a non-negative integer y can be defined as y=x:x2y<(x+1)2,x

For example, isqrt(27)=27=5 because 62>27 and 5227.

The following C programs are straightforward implementations.

Template:Flex columns

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.

// Integer square root
// (linear search, ascending) using addition
unsigned int isqrt(unsigned int y)
{
	unsigned int L = 0;
	unsigned int a = 1;
	unsigned int d = 3;

	while (a <= y)
	{
		a = a + d;	// (a + 1) ^ 2
		d = d + 2;
		L = L + 1;
	}

	return L;
}

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. The following C-program is an implementation.

// Integer square root (using binary search)
unsigned int isqrt(unsigned int y)
{
	unsigned int L = 0;
	unsigned int M;
	unsigned int R = y + 1;

    while (L != R - 1)
    {
        M = (L + R) / 2;

		if (M * M <= y)
			L = M;
		else
			R = M;
	}

    return L;
}

Numerical example

For example, if one computes isqrt(2000000) using binary search, one obtains 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]

This computation takes 21 iteration steps, whereas linear search (ascending, starting from 0) needs Template:Val 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 x2n=0, giving the iterative formula xk+1=12(xk+nxk),k0,x0>0.

The sequence {xk} converges quadratically to n as k.

Stopping criterion

One can proveTemplate:Citation needed that c=1 is the largest possible number for which the stopping criterion |xk+1xk|<c ensures xk+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 1 should be used to protect against round-off errors.

Domain of computation

Although n is irrational for many n, the sequence {xk} contains only rational terms when x0 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.

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 xk+1=12(xk+nxk),k0,x0>0,x0.

By using the fact that 12(xk+nxk)=12(xk+nxk),

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

In the original version, one has xkn for k1, and xk>xk+1 for xk>n. So in the integer version, one has xkn and xkxk>xk+1xk+1 until the final solution xs is reached. For the final solution xs, one has nxsn and xs+1xs, so the stopping criterion is xk+1xk.

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.

Example implementation in C

// Square root of integer
unsigned int int_sqrt(unsigned int s)
{
	// Zero yields zero
    // One yields one
	if (s <= 1) 
		return s;

    // Initial estimate (must be too high)
	unsigned int x0 = s / 2;

	// Update
	unsigned int x1 = (x0 + s / x0) / 2;

	while (x1 < x0)	// Bound check
	{
		x0 = x1;
		x1 = (x0 + s / x0) / 2;
	}		
	return x0;
}

Numerical example

For example, if one computes the integer square root of Template:Math using the algorithm above, one obtains the sequence 10000005000012500021250046250931270156667896407422821579142214141414 In total 13 iteration steps are needed. 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. std::bit_width in C++20), 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 Template:Math, log2n=20, x0=211=2048, and the resulting sequence is 20481512141714141414. In this case only four iteration steps are needed.

Digit-by-digit algorithm

The traditional pen-and-paper algorithm for computing the square root 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. 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 * 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
    else:
        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 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 Template:Section link for an example.[1]

Karatsuba square root algorithm

The Karatsuba square root algorithm is a combination of two functions: a public function, which returns the integer square root of the input, and a recursive private function, which does the majority of the work.

The public function normalizes the actual input, passes the normalized input to the private function, denormalizes the result of the private function, and returns that.

The private function takes a normalized input, divides the input bits in half, passes the most-significant half of the input recursively to the private function, and performs some integer operations on the output of that recursive call and the least-significant half of the input to get the normalized output, which it returns.

For big-integers of "50 to 1,000,000 digits", Burnikel-Ziegler Karatsuba division and Karatsuba multiplication are recommended by the algorithm's creator.[2]

An example algorithm for 64-bit unsigned integers is below. The algorithm:

  1. Normalizes the input inside Template:Mono.
  2. Calls Template:Mono, which requires a normalized input.
  3. Calls Template:Mono with the most-significant half of the normalized input's bits, which will already be normalized as the most-significant bits remain the same.
  4. Continues on recursively until there's an algorithm that's faster when the number of bits is small enough.
  5. Template:Mono then takes the returned integer square root and remainder to produce the correct results for the given normalized Template:Mono.
  6. Template:Mono then denormalizes the result.
/// Performs a Karatsuba square root on a `u64`.
pub fn u64_isqrt(mut n: u64) -> u64 {
    if n <= u32::MAX as u64 {
        // If `n` fits in a `u32`, let the `u32` function handle it.
        return u32_isqrt(n as u32) as u64;
    } else {
        // The normalization shift satisfies the Karatsuba square root
        // algorithm precondition "a₃ ≥ b/4" where a₃ is the most
        // significant quarter of `n`'s bits and b is the number of
        // values that can be represented by that quarter of the bits.
        //
        // b/4 would then be all 0s except the second most significant
        // bit (010...0) in binary. Since a₃ must be at least b/4, a₃'s
        // most significant bit or its neighbor must be a 1. Since a₃'s
        // most significant bits are `n`'s most significant bits, the
        // same applies to `n`.
        //
        // The reason to shift by an even number of bits is because an
        // even number of bits produces the square root shifted to the
        // left by half of the normalization shift:
        //
        // sqrt(n << (2 * p))
        // sqrt(2.pow(2 * p) * n)
        // sqrt(2.pow(2 * p)) * sqrt(n)
        // 2.pow(p) * sqrt(n)
        // sqrt(n) << p
        //
        // Shifting by an odd number of bits leaves an ugly sqrt(2)
        // multiplied in.
        const EVEN_MAKING_BITMASK: u32 = !1;
        let normalization_shift = n.leading_zeros() & EVEN_MAKING_BITMASK;
        n <<= normalization_shift;

        let (s, _) = u64_normalized_isqrt_rem(n);

        let denormalization_shift = normalization_shift / 2;
        return s >> denormalization_shift;
    }
}

/// Performs a Karatsuba square root on a normalized `u64`, returning the square
/// root and remainder.
fn u64_normalized_isqrt_rem(n: u64) -> (u64, u64) {
    const HALF_BITS: u32 = u64::BITS >> 1;
    const QUARTER_BITS: u32 = u64::BITS >> 2;
    const LOWER_HALF_1_BITS: u64 = (1 << HALF_BITS) - 1;

    debug_assert!(
        n.leading_zeros() <= 1,
        "Input is not normalized: {n} has {} leading zero bits, instead of 0 or 1.",
        n.leading_zeros()
    );

    let hi = (n >> HALF_BITS) as u32;
    let lo = n & LOWER_HALF_1_BITS;

    let (s_prime, r_prime) = u32_normalized_isqrt_rem(hi);

    let numerator = ((r_prime as u64) << QUARTER_BITS) | (lo >> QUARTER_BITS);
    let denominator = (s_prime as u64) << 1;

    let q = numerator / denominator;
    let u = numerator % denominator;

    let mut s = (s_prime << QUARTER_BITS) as u64 + q;
    let mut r = (u << QUARTER_BITS) | (lo & ((1 << QUARTER_BITS) - 1));
    let q_squared = q * q;
    if r < q_squared {
        r += 2 * s - 1;
        s -= 1;
    }
    r -= q_squared;

    return (s, r);
}

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.

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

See also

Notes

Template:Reflist

References

Template:Reflist

Template:Refbegin

Template:Refend

Template:Number theoretic algorithms


Cite error: <ref> tags exist for a group named "note", but no corresponding <references group="note"/> tag was found