Integer square root

Last updated

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,

Contents

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

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 k decimal digits. [note 2]

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

// Print sqrt(y), without haltingvoidsqrtForever(unsignedinty){unsignedintresult=isqrt(y);printf("%d.",result);// print result, followed by a decimal pointwhile(true)// repeat forever ...{y=y*100;// theoretical example: overflow is ignoredresult=isqrt(y);printf("%d",result%10);// print last digit of result}}

The conclusion is that algorithms which compute isqrt() are computationally equivalent to algorithms which compute sqrt(). [1]

Basic algorithms

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

For example, because .

The following C programs are straightforward implementations.

// Integer square root// (using linear search, ascending)unsignedintisqrt(unsignedinty){// initial underestimate, L <= isqrt(y)unsignedintL=0;while((L+1)*(L+1)<=y)L=L+1;returnL;}
// Integer square root// (using linear search, descending)unsignedintisqrt(unsignedinty){// initial overestimate, isqrt(y) <= RunsignedintR=y;while(R*R>y)R=R-1;returnR;}

Linear search using addition

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

// Integer square root// (linear search, ascending) using additionunsignedintisqrt(unsignedinty){unsignedintL=0;unsignedinta=1;unsignedintd=3;while(a<=y){a=a+d;// (a + 1) ^ 2d=d+2;L=L+1;}returnL;}

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)unsignedintisqrt(unsignedinty){unsignedintL=0;unsignedintM;unsignedintR=y+1;while(L!=R-1){M=(L+R)/2;if(M*M<=y)L=M;elseR=M;}returnL;}

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

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

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 1 should be used to protect against round-off errors.

Domain of computation

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

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

// Square root of integerunsignedintint_sqrt(unsignedints){// Zero yields zero// One yields oneif(s<=1)returns;// Initial estimate (must be too high)unsignedintx0=s/2;// Updateunsignedintx1=(x0+s/x0)/2;while(x1<x0)// Bound check{x0=x1;x1=(x0+s/x0)/2;}returnx0;}

Numerical example

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

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

which is the least power of two bigger than . In the example of the integer square root of 2000000, , , and the resulting sequence is

In this case only four iteration steps are needed.

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 * being multiplication, << being left shift, and >> being logical right shift, a recursive algorithm to find the integer square root of any natural number is:

definteger_sqrt(n:int)->int:assertn>=0,"sqrt works for only non-negative inputs"ifn<2:returnn# Recursive call:small_cand=integer_sqrt(n>>2)<<1large_cand=small_cand+1iflarge_cand*large_cand>n:returnsmall_candelse:returnlarge_cand# equivalently:definteger_sqrt_iter(n:int)->int:assertn>=0,"sqrt works for only non-negative inputs"ifn<2:returnn# Find the shift amount. See also [[find first set]],# shift = ceil(log2(n) * 0.5) * 2 = ceil(ffs(n) * 0.5) * 2shift=2while(n>>shift)!=0:shift+=2# Unroll the bit-setting loop.result=0whileshift>=0:result=result<<1large_cand=(result+1)# Same as result ^ 1 (xor), because the last bit is always 0.iflarge_cand*large_cand<=n>>shift:result=large_candshift-=2returnresult

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 Methods of computing square roots § Binary numeral system (base 2) for an example. [2]

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.

See also

Notes

  1. The square roots of the perfect squares (e.g., 0, 1, 4, 9, 16) are integers. In all other cases, the square roots of positive integers are irrational numbers.
  2. It is no surprise that the repeated multiplication by 100 is a feature in Jarvis (2006)
  3. The fractional part of square roots of perfect squares is rendered as 000....

Related Research Articles

<span class="mw-page-title-main">Binomial distribution</span> Probability distribution

In probability theory and statistics, the binomial distribution with parameters n and p is the discrete probability distribution of the number of successes in a sequence of n independent experiments, each asking a yes–no question, and each with its own Boolean-valued outcome: success or failure. A single success/failure experiment is also called a Bernoulli trial or Bernoulli experiment, and a sequence of outcomes is called a Bernoulli process; for a single trial, i.e., n = 1, the binomial distribution is a Bernoulli distribution. The binomial distribution is the basis for the popular binomial test of statistical significance.

<span class="mw-page-title-main">Binary search algorithm</span> Search algorithm finding the position of a target value within a sorted array

In computer science, binary search, also known as half-interval search, logarithmic search, or binary chop, is a search algorithm that finds the position of a target value within a sorted array. Binary search compares the target value to the middle element of the array. If they are not equal, the half in which the target cannot lie is eliminated and the search continues on the remaining half, again taking the middle element to compare to the target value, and repeating this until the target value is found. If the search ends with the remaining half being empty, the target is not in the array.

<span class="mw-page-title-main">Cumulative distribution function</span> Probability that random variable X is less than or equal to x

In probability theory and statistics, the cumulative distribution function (CDF) of a real-valued random variable , or just distribution function of , evaluated at , is the probability that will take a value less than or equal to .

In mathematics and computer programming, exponentiating by squaring is a general method for fast computation of large positive integer powers of a number, or more generally of an element of a semigroup, like a polynomial or a square matrix. Some variants are commonly referred to as square-and-multiply algorithms or binary exponentiation. These can be of quite general use, for example in modular arithmetic or powering of matrices. For semigroups for which additive notation is commonly used, like elliptic curves used in cryptography, this method is also referred to as double-and-add.

<span class="mw-page-title-main">Floor and ceiling functions</span> Nearest integers from a number

In mathematics and computer science, the floor function is the function that takes as input a real number x, and gives as output the greatest integer less than or equal to x, denoted x or floor(x). Similarly, the ceiling function maps x to the smallest integer greater than or equal to x, denoted x or ceil(x).

<span class="mw-page-title-main">Rounding</span> Replacing a number with a simpler value

Rounding means replacing a number with an approximate value that has a shorter, simpler, or more explicit representation. For example, replacing $23.4476 with $23.45, the fraction 312/937 with 1/3, or the expression 2 with 1.414.

Integration is the basic operation in integral calculus. While differentiation has straightforward rules by which the derivative of a complicated function can be found by differentiating its simpler component functions, integration does not, so tables of known integrals are often useful. This page lists some of the most common antiderivatives.

In computer programming, a bitwise operation operates on a bit string, a bit array or a binary numeral at the level of its individual bits. It is a fast and simple action, basic to the higher-level arithmetic operations and directly supported by the processor. Most bitwise operations are presented as two-operand instructions where the result replaces one of the input operands.

<span class="mw-page-title-main">Prime-counting function</span> Function representing the number of primes less than or equal to a given number

In mathematics, the prime-counting function is the function counting the number of prime numbers less than or equal to some real number x. It is denoted by π(x) (unrelated to the number π).

<span class="mw-page-title-main">Mertens function</span> Summatory function of the Möbius function

In number theory, the Mertens function is defined for all positive integers n as

The fractional part or decimal part of a non‐negative real number is the excess beyond that number's integer part. The latter is defined as the largest integer not greater than x, called floor of x or . Then, the fractional part can be formulated as a difference:

In mathematics, a pairing function is a process to uniquely encode two natural numbers into a single natural number.

In computing, the modulo operation returns the remainder or signed remainder of a division, after one number is divided by another.

Methods of computing square roots are algorithms for approximating the non-negative square root of a positive real number . Since all square roots of natural numbers, other than of perfect squares, are irrational, square roots can usually only be computed to some finite precision: these methods typically construct a series of increasingly accurate approximations.

In number theory, the law of quadratic reciprocity, like the Pythagorean theorem, has lent itself to an unusually large number of proofs. Several hundred proofs of the law of quadratic reciprocity have been published.

Shanks's square forms factorization is a method for integer factorization devised by Daniel Shanks as an improvement on Fermat's factorization method.

The digital root of a natural number in a given radix is the value obtained by an iterative process of summing digits, on each iteration using the result from the previous iteration to compute a digit sum. The process continues until a single-digit number is reached. For example, in base 10, the digital root of the number 12345 is 6 because the sum of the digits in the number is 1 + 2 + 3 + 4 + 5 = 15, then the addition process is repeated again for the resulting number 15, so that the sum of 1 + 5 equals 6, which is the digital root of that number. In base 10, this is equivalent to taking the remainder upon division by 9, which allows it to be used as a divisibility rule.

In mathematics, an infinite periodic continued fraction is a continued fraction that can be placed in the form

In computer science, multiply-with-carry (MWC) is a method invented by George Marsaglia for generating sequences of random integers based on an initial set from two to many thousands of randomly chosen seed values. The main advantages of the MWC method are that it invokes simple computer integer arithmetic and leads to very fast generation of sequences of random numbers with immense periods, ranging from around to .

<span class="mw-page-title-main">Conway–Maxwell–Poisson distribution</span> Probability distribution

In probability theory and statistics, the Conway–Maxwell–Poisson distribution is a discrete probability distribution named after Richard W. Conway, William L. Maxwell, and Siméon Denis Poisson that generalizes the Poisson distribution by adding a parameter to model overdispersion and underdispersion. It is a member of the exponential family, has the Poisson distribution and geometric distribution as special cases and the Bernoulli distribution as a limiting case.

References

  1. see 'Methods of computing square roots'.
  2. Woo, C (June 1985). "Square root by abacus algorithm (archived)". Archived from the original on 2012-03-06.
  3. LispWorks Ltd (1996). "CLHS: Function SQRT, ISQRT". www.lispworks.com.
  4. Python Software Foundation (2001). "Mathematical functions". Python Standard Library documentation.  (since version 3.8).