User:Marc Schroeder/sandbox2

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,


 * $$\mbox{isqrt}( n ) = \lfloor \sqrt n \rfloor.$$

For example, $$\mbox{isqrt}(27) = \lfloor \sqrt{27} \rfloor = \lfloor 5.19615242270663 \ldots \rfloor = 5$$

1
$$\mbox{isqrt}$$ is a function from $$\mathbb{N}_0$$ to $$\mathbb{N}_0$$. It is worthwhile to mention that any algorithm for $$\mbox{isqrt}$$ needs only a slight adjustment to compute the square root of real numbers to any requested degree of accuracy $$k$$.

Explanation

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

The positional notation of $$r$$ in base $$b$$ corresponds to the Cauchy sequence $$\{x_k\}_{k=0}^{\infty}$$, where
 * $$x_k = \frac{\lfloor b^k \times r \rfloor}{b^k}$$.

The positional notation of $$\sqrt{r}$$ in base $$b$$ corresponds to the Cauchy sequence $$\{y_k\}_{k=0}^{\infty}$$, where
 * $$y_k = \frac{\lfloor b^k \sqrt r \rfloor}{b^k}$$.

These sequences are related to each other as follows:
 * $$y_k = \frac{\lfloor b^k \sqrt{x_{2k}} \rfloor}{b^k} = \frac{\lfloor \sqrt{ b^{2k} \times x_{2k}} \rfloor}{b^k} = \frac{\mbox{isqrt}( b^{2k} \times x_{2k} )}{b^k} $$

On input $$x_{2k}, b, k$$ the computation of $$x_k$$ takes three steps:
 * 1) $$tmp \leftarrow b^{2k} \times x_{2k}$$
 * 2) $$tmp \leftarrow \mbox{isqrt}(tmp)$$
 * 3) $$y_k \leftarrow \frac{tmp}{b^k}$$

The  'slight adjustment'  mentioned above consists in
 * step 1: removes the radix point from $$x_{2k}$$
 * step 3: adds the radix point to the result.

Example

Let $$r$$ be the real number $$2$$. The decimal expansion


 * of $$r = 2$$.


 * of $$\sqrt r = \underline{1.414}21356237309504880168872420969807856967187537694 \dots$$.

Possible Cauchy sequences are

or else
 * $$\{x_k\}_{k=0}^{\infty}$$||$$= (2,$$||$$2,$$||$$2,$$||$$2,$$||$$2,$$||$$2,$$||$$\underline{2},$$||$$2,$$||$$2,$$||$$2,$$||$$\dots)$$
 * $$\{y_k\}_{k=0}^{\infty}$$||$$= (1,$$||$$1.4,$$||$$1.41,$$||$$\underline{1.414},$$||$$1.4142,$$||$$1.41421,$$||$$1.414213,$$||$$1.4142135,$$||$$1.41421356,$$||$$1.414213562,$$||$$\dots)$$
 * }
 * }

or else (base = 2)
 * $$\{x_k\}_{k=0}^{\infty}$$||$$= (1,$$||$$1.9,$$||$$1.99,$$||$$1.999,$$||$$1.9999,$$||$$1.99999,$$||$$\underline{1.999999},$$||$$1.9999999,$$||$$1.99999999,$$||$$1.999999999,$$||$$\dots)$$
 * $$\{z_k\}_{k=0}^{\infty}$$||$$= (1,$$||$$1.4,$$||$$1.41,$$||$$\underline{1.414},$$||$$1.4142,$$||$$1.41421,$$||$$1.414213,$$||$$1.4142135,$$||$$1.41421356,$$||$$1.414213562,$$||$$\dots)$$
 * }
 * }


 * $$\{x_k\}_{k=0}^{\infty}$$||$$= (1_2,$$||$$1.0_2,$$||$$1.01_2,$$||$$1.011_2,$$||$$1.0110_2,$$||$$1.01101_2,$$||$$1.011010_2,$$||$$1.0110101_2,$$||$$\underline{1.01101010_2},$$||$$1.011010100_2,$$||$$\dots)$$
 * $$= (1,$$||$$1.0,$$||$$1.25,$$||$$1.375,$$||$$1.375,$$||$$1.40625,$$||$$1.40625,$$||$$1.4140625,$$||$$\underline{1.4140625},$$||$$1.4140625,$$||$$\dots)$$
 * }
 * }

Compute the decimal expansion of $$\sqrt {2}$$ truncated at $$k=3$$.
 * $$y_3 = \mbox{isqrt}(10^{6} \times 2.000000) / 10^{3} = \lfloor \sqrt {2000000} \rfloor / 1000 = 1.414$$.

Compute the decimal expansion of $$\sqrt {2}$$ truncated at $$k=3$$.
 * $$z_3 = \mbox{isqrt}(10^{6} \times 1.999999) / 10^{3} = \lfloor \sqrt {1999999} \rfloor / 1000 = 1.414$$.

The method given by — see below — uses repeated multiplication by $$10^2$$ to compute square roots.[ Jarvis]

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



\begin{array}{lcl} \sqrt{z} & = & \sqrt{x^2+y} = x+\cfrac{y} {2x+\cfrac{y} {2x+\cfrac{y} {2x+\ddots}}} \\ & = & x + \underset{i=1}\overset{\infty}\operatorname{K}\cfrac{y}{2x}. \end{array} $$

As, trivially, $$z = \lfloor \sqrt z \rfloor^2 + (z - \lfloor \sqrt z \rfloor^2)$$, this continued fraction becomes



\begin{array}{lcl} \sqrt{z} & = & \lfloor \sqrt z \rfloor + \cfrac{z - \lfloor \sqrt z \rfloor^2} {2 \lfloor \sqrt z \rfloor + \cfrac{z - \lfloor \sqrt z \rfloor^2} {2 \lfloor \sqrt z \rfloor+\cfrac{z - \lfloor \sqrt z \rfloor^2} {2 \lfloor \sqrt z \rfloor+\ddots}}} \\ & = & \lfloor \sqrt z \rfloor + \underset{i=1}\overset{\infty}\operatorname{K}\cfrac{z - \lfloor \sqrt z \rfloor^2}{2 \lfloor \sqrt z \rfloor}. \end{array} $$

This is another way to approach $$\sqrt z$$ by a combination of applications of $$\lfloor \sqrt n \rfloor$$.

Basic algorithms
The integer square root of a non-negative integer $$y$$ can be defined as
 * $$\lfloor \sqrt y \rfloor = \mu x ((x+1)^2 > y)$$

For example, $$\mbox{isqrt}(27) = \lfloor \sqrt{27} \rfloor = 5$$ because $$6^2 > 27 \text{ and } 5^2 \ngtr 27$$.

Algorithm using linear search
The following C-programs are straightforward implementations.

... using addition
In the program above (linear search, ascending) one can replace multiplication by addition, using the equivalence
 * $$(L+1)^2 = L^2 + 2L + 1 = L^2 + 1 + \sum_{i=1}^L 2$$.

... using subtraction
published a very strange method — using only subtraction — to compute the square root of an integer $$n$$.

The method generates a string of digits that approach more and more closely the decimal digits of $$\sqrt{n}$$.

The algorithm [ Jarvis] can be simplified to

Initial step
 * $$\text{ Let } a = n \text{, and put } b = 1$$.

Repeated steps
 * $$\begin{array}{lll}

(\mathbf{R1}) & \text{ If } a \ge b \text{, subtract } b \text{ from } a \text{, and add } 2 \text{ to } b.\\ (\mathbf{R2}) & \text{ If } a < b \text{, multiply } a \text{ by } 4 \text{, and replace } b \text{ by } 2b - 1. \end{array}$$

In the simplified version the digits of $$b$$ approach more and more closely the binary digits of $$\sqrt{n}$$.

To compute $$\lfloor \sqrt{n} \rfloor$$, rule $$\mathbf{R1}$$ alone does the job.

Numerical example

For example, if one computes $$\operatorname{isqrt}(200)$$ using the method 'by subtraction', one obtains the $$(a,b)$$ sequence $$\begin{align} & (200,1) \rightarrow (199,3) \rightarrow (196,5) \rightarrow (191,7) \rightarrow (184,9) \rightarrow (175,11) \rightarrow (164,13) \rightarrow (151,15) \\ & \rightarrow (136,17) \rightarrow (119,19) \rightarrow (100,21) \rightarrow (79,23) \rightarrow (56,25) \rightarrow (31,27) \rightarrow (4,29). \end{align}$$
 * $$29 / 2 = 14 = \lfloor \sqrt{200} \rfloor$$

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

Algorithm using binary search
Linear search sequentially checks every value until it hits the smallest $$x$$ where $$x^2 > y$$.

A speed-up is achieved by using binary search instead. The following C-program is an implementation.

Numerical example

For example, if one computes $$\mbox{isqrt}( 2000000 )$$ using binary search, one obtains the $$(L,R)$$ sequence
 * $$\begin{align}

& (0,2000001) \rightarrow (0,1000000) \rightarrow (0,500000) \rightarrow (0,250000) \rightarrow (0,125000) \rightarrow (0,62500) \rightarrow (0,31250) \rightarrow (0,15625) \\ & \rightarrow (0,7812) \rightarrow (0,3906) \rightarrow (0,1953) \rightarrow (976,1953) \rightarrow (976,1464) \rightarrow (1220,1464) \rightarrow (1342,1464) \rightarrow (1403,1464) \\ & \rightarrow (1403,1433) \rightarrow (1403,1418) \rightarrow (1410,1418) \rightarrow (1414,1418) \rightarrow (1414,1416) \rightarrow (1414,1415) \end{align}$$

This computation takes 21 iteration steps, whereas linear search (ascending, starting from $$0$$) needs $1,414$ steps.

Algorithm using Newton's method
One way of calculating $$\sqrt{n}$$ and $$\mbox{isqrt}( n )$$ is to use Heron's method, which is a special case of Newton's method, to find a solution for the equation $$x^{2} - n = 0$$, giving the iterative formula


 * $${x}_{k+1} = \frac{1}{2}\left(x_k + \frac{ n }{x_k}\right), \quad k \ge 0, \quad x_0 > 0.$$

The sequence $$\{ x_k \}$$ converges quadratically to $$\sqrt{n}$$ as $$k\to \infty$$.

Stopping criterion
One can prove that $$c=1$$ is the largest possible number for which the stopping criterion
 * $$|x_{k+1} - x_{k}| < c$$

ensures $$\lfloor x_{k+1} \rfloor=\lfloor \sqrt n \rfloor$$ 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
Although $$\sqrt{n}$$ is irrational for many $$n$$, the sequence $$\{ x_k \}$$ contains only rational terms when $$ x_0 $$ is rational. Thus, with this method it is unnecessary to exit the field of rational numbers in order to calculate $$\mbox{isqrt}( n )$$, a fact which has some theoretical advantages.

Using only integer division
For computing $$\lfloor \sqrt n \rfloor$$ 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


 * $${x}_{k+1} = \left\lfloor \frac{1}{2}\left(x_k + \left\lfloor \frac{ n }{x_k} \right\rfloor \right) \right\rfloor, \quad k \ge 0, \quad x_0 > 0, \quad x_0 \in \mathbb{Z}.$$

By using the fact that


 * $$\left\lfloor \frac{1}{2}\left(x_k + \left\lfloor \frac{ n }{x_k} \right\rfloor \right) \right\rfloor = \left\lfloor \frac{1}{2}\left(x_k + \frac{ n }{x_k} \right) \right\rfloor,$$

one can show that this will reach $$\lfloor \sqrt n \rfloor$$ within a finite number of iterations.

In the original version, one has $${x}_{k}\ge \sqrt n$$ for $$k \ge 1$$, and $${x}_{k}> {x}_{k+1}$$ for $${x}_{k}>  \sqrt n$$. So in the integer version, one has $$\lfloor {x}_{k}\rfloor \ge \lfloor\sqrt n\rfloor$$ and $${x}_{k}\ge \lfloor {x}_{k}\rfloor > {x}_{k+1} \ge \lfloor {x}_{k+1}\rfloor $$ until the final solution $$ {x}_{s}$$ is reached. For the final solution $$ {x}_{s}$$, one has $$\lfloor \sqrt n\rfloor\le\lfloor {x}_{s}\rfloor \le \sqrt n$$ and $$\lfloor{x}_{s+1}\rfloor \ge  \lfloor{x}_{s}\rfloor $$, so the stopping criterion is $$\lfloor{x}_{k+1}\rfloor \ge \lfloor{x}_{k}\rfloor $$.

However, $$\lfloor \sqrt n \rfloor$$ is not necessarily a fixed point of the above iterative formula. Indeed, it can be shown that $$\lfloor \sqrt n \rfloor$$ 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 $$\lfloor \sqrt n \rfloor$$ and $$\lfloor \sqrt n \rfloor + 1$$ instead of converging.

Numerical example
For example, if one computes the integer square root of $$2000000$$ using the algorithm above, one obtains the sequence
 * $$\begin{align}

& 1000000 \rightarrow 500001 \rightarrow 250002 \rightarrow 125004 \rightarrow 62509 \rightarrow 31270 \rightarrow 15666 \rightarrow 7896 \\ & \rightarrow 4074 \rightarrow 2282 \rightarrow 1579 \rightarrow 1422 \rightarrow 1414 \rightarrow 1414 \end{align}$$ 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.  in C++20), one should better start at
 * $$x_0 = 2^{\lfloor (\log_2 n) /2 \rfloor+1}$$,

which is the least power of two bigger than $$\sqrt n$$. In the example of the integer square root of $$2000000$$, $$\lfloor \log_2 n \rfloor=20$$, $$x_0=2^{11}=2 048$$, and the resulting sequence is
 * $$ 2048\rightarrow 1512\rightarrow 1417\rightarrow 1414 \rightarrow 1414$$.

In this case only 4 iteration steps are needed.

Digit-by-digit algorithm
The traditional pen-and-paper algorithm for computing the square root $$\sqrt{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 $$\leq 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:

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 for an example.

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.


 * : Common Lisp.
 * : Python.