Jump to content

User:Marc Schroeder/sandbox2

From Wikipedia, the free encyclopedia

Deze sandbox hoort bij Integer square root


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,

For example,

Introductory remarks

[edit]

1

[edit]

is a function from to . It is worthwhile to mention that any algorithm for needs only a slight adjustment to compute the square root of real numbers to any requested degree of accuracy .

Explanation [1]

Let be a — not necessarily computable — non-negative real number.

The positional notation of in base [2] corresponds to the Cauchy sequence , where

.

The positional notation of in base [3] corresponds to the Cauchy sequence , where

.

These sequences are related to each other as follows:

On input the computation of takes three steps:

The 'slight adjustment' mentioned above consists in

step 1: removes the radix point from
step 3: adds the radix point to the result.

Example

Let be the real number . The decimal [4] expansion

of .
of .

Possible Cauchy sequences are

or else

or else (base = 2)

Compute the decimal [5] expansion of truncated at .

.

Compute the decimal [6] expansion of truncated at .

.

The method given by Jarvis (2005) — see below — uses repeated multiplication by to compute square roots.[Jarvis]

2

[edit]

Let be a non-negative integer. From the formula derived there one can construct the following generalized continued fraction:

As, trivially, , this continued fraction becomes

This is another way to approach by a combination of applications of .

Basic algorithms

[edit]

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

For example, because .

[edit]

The following C-programs are straightforward implementations.

// Integer square root
// (using linear search, ascending)
unsigned int isqrt( unsigned int y )
{
	// initial underestimate, L <= isqrt(y)
	unsigned int L = 0;

	while( (L + 1) * (L + 1) <= y )
		L = L + 1;

	return L;
}
// Integer square root
// (using linear search, descending)
unsigned int isqrt( unsigned int y )
{
	// initial overestimate, isqrt(y) <= R
	unsigned int R = y;

	while( R * R > y )
		R = R - 1;

	return R;
}

Linear search ...

[edit]

... using addition

[edit]

In the program above (linear search, ascending) one can replace multiplication by addition, using the equivalence

.
// 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;
}

... using subtraction

[edit]

Jarvis (2005) published a very strange method — using only subtraction — to compute the square root of an integer .[7]

The method generates a string of digits that approach more and more closely the decimal digits of .

The algorithm [Jarvis] can be simplified to

Initial step

.

Repeated steps

In the simplified version the digits of approach more and more closely the binary digits of .

To compute , rule alone does the job.

// Integer square root (using subtraction)
unsigned int isqrt( unsigned int n )
{
	unsigned int a = n;
	unsigned int b = 1;

	while (a >= b)
	{
		a = a - b;
		b = b + 2;
	}

	return b / 2;
}

Numerical example

For example, if one computes using the method 'by subtraction', one obtains the sequence

This computation takes 14 iteration steps, the same as linear search (ascending, starting from ).

[edit]

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

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 using binary search, one obtains the sequence

This computation takes 21 iteration steps, whereas linear search (ascending, starting from ) needs 1414 steps.

Algorithm using Newton's method

[edit]

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 .

Stopping criterion

[edit]

One can prove[citation needed] 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.

Domain of computation

[edit]

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.

Using only integer division

[edit]

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.

In the original version, one has for , and for . So in the integer version, one has and until the final solution is reached. For the final solution , one has and , so the stopping criterion is .

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.

Example implementation in C

[edit]
// Square root of integer
unsigned int int_sqrt ( unsigned int s )
{
	unsigned int x0 = s / 2;			// Initial estimate
	                        			// Avoid overflow when s is the maximum representable value

	// Sanity check
	if ( x0 != 0 )
	{
		unsigned int x1 = ( x0 + s / x0 ) / 2;	// Update
		
		while ( x1 < x0 )				// This also checks for cycle
		{
			x0 = x1;
			x1 = ( x0 + s / x0 ) / 2;
		}
		
		return x0;
	}
	else
	{
		return s;
	}
}

Numerical example

[edit]

For example, if one computes the integer square root of using the algorithm above, one obtains the sequence

Totally 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

,

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 4 iteration steps are needed.

Digit-by-digit algorithm

[edit]

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

[edit]

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 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 § Binary numeral system (base 2) for an example.[8]

In programming languages

[edit]

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.

See also

[edit]

Notes

[edit]
[Jarvis] - The method given in Jarvis (2005) generates the terms of the Cauchy sequence of the decimal representation of , but does not output the radix point.
Initial step
.
.
Repeated steps
Then the digits of approach more and more closely the digits of the expansion (in r-ary positional notation) of .
Define the number of applications of rule .
Then converges to .
Setting (instead of ) has as effect that the digits of the Cauchy sequence of the binary expansion are generated (instead of the sequence of the decimal expansion).
  1. ^ numbers without a subscript are assumed to be represented in base 10.
  2. ^ .
  3. ^ .
  4. ^ means:
  5. ^ means:
  6. ^ means:
  7. ^ Jarvis (2005, p. 2): "In fact, the method does not only work for positiver integers. The method works for all positive numbers, even decimals such as , and even ."
  8. ^ Woo 1985.
  9. ^ LispWorks Ltd 1996.
  10. ^ Python Software Foundation 2001.
[edit]
  • Jarvis, Ashley Frazer (2005). "Square roots by subtraction" (PDF). Mathematical Spectrum. 37: 119–122.

Category:Number theoretic algorithms Category:Number theory Category:Root-finding algorithms