# Shifting nth-root algorithm

The shifting nth-root algorithm is an algorithm for extracting the nth root of a positive real number which proceeds iteratively by shifting in n digits of the radicand, starting with the most significant, and produces one digit of the root on each iteration, in a manner similar to long division.

 Contents

## Algorithm

### Notation

Let B be the base of the number system you are using, and n be the degree of the root to be extracted. Let x be the radicand processed thus far, y be the root extracted thus far, and r be the remainder. Let α be the next n digits of the radicand, and β be the next digit of the root. Let x' be the new value of x for the next iteration, y' be the new value of y for the next iteration, and r' be the new value of r for the next iteration. These are all integers.

### Invariants

At each iteration, the invariant [itex]y^n + r = x[itex] will hold. The invariant [itex](y+1)^n>x[itex] will hold. Thus y is the largest integer less than the nth root of x, and r is the remainder.

### Initialization

The initial values of x, y, and r should be 0. The value of α for the first iteration should be the most significant aligned block of n digits of the radicand. An aligned block of n digits means a block of digits aligned so that the decimal point falls between blocks. For example, in 123.4 the most significant aligned block of 2 digits is 01, the next most significant is 23, and the third most significant is 40.

### Main loop

On each iteration we shift in n digits of the radicand, so we have [itex]x' = B^n x + \alpha[itex] and we produce 1 digit of the root, so we have [itex]y' = B y + \beta [itex]. We want to choose β and r' so that the invariants described above hold. It turns out that there is always exactly one such choice, as will be proved below.

The first invariant says that:

[itex]x' = y'^n + r'[itex]

or

[itex]B^n x + \alpha = (B y + \beta)^n + r'[itex]

So, pick the largest integer β such that

[itex](B y + \beta)^n \le B^n x + \alpha[itex]

and let

[itex]r' = B^n x + \alpha - (B y + \beta)^n[itex]

Such a β always exists, since if [itex]\beta = 0[itex] then the condition is [itex]B^n y^n \le B^n x + \alpha[itex], but [itex]y^n \le x[itex], so this is always true. Also, β must be less than B, since if [itex]\beta = B[itex] then we would have

[itex](B(y+1))^n \le B^n x + \alpha[itex]

but the second invariant implies that

[itex]B^n x < B^n (y+1)^n[itex]

and since [itex]B^n x[itex] and [itex]B^n (y+1)^n[itex] are both multiples of [itex]B^n[itex] the difference between them must be at least [itex]B^n[itex], and then we have

[itex]B^n x + B^n \le B^n (y+1)^n[itex]
[itex]B^n x + B^n \le B^n x + \alpha[itex]
[itex]B^n \le \alpha[itex]

but [itex]0 \le \alpha < B^n[itex] by definition of α, so this can't be true, and [itex](B y + \beta)^n[itex] is a monotonically increasing function of β, so it can't be true for larger β either, so we conclude that there exists an integer γ with [itex]\gammar' with [itex]r' \ge 0[itex] exists such that the first invariant holds if and only if [itex]0 \le \beta \le \gamma[itex].

Now consider the second invariant. It says:

[itex](y'+1)^n>x'[itex]

or

[itex](B y + \beta + 1)^n>B^n x + \alpha[itex]

Now, if β is not the largest admissible β for the first invariant as described above, then [itex]\beta + 1[itex] is also admissible, and we have

[itex](B y + \beta + 1)^n \le B^n x + \alpha[itex]

This violates the second invariant, so to satisfy both invariants we must pick the largest β allowed by the first invariant. Thus we have proven the existence and uniqueness of β and r'.

To summarize, on each iteration:

1. Let α be the next aligned block of digits from the radicand
2. Let [itex]x' = B^n x + \alpha[itex]
3. Let β be the largest β such that [itex](B y + \beta)^n \le B^n x + \alpha[itex]
4. Let [itex]y' = B y + \beta[itex]
5. Let [itex]r' = x' - y'^n[itex]

Now, note that [itex]x = y^n + r[itex], so the condition

[itex](B y + \beta)^n \le B^n x + \alpha[itex]

is equivalent to

[itex](B y + \beta)^n - B^n y^n \le B^n r + \alpha[itex]

and

[itex]r' = x' - y'^n = B^n x + \alpha - (B y + \beta)^n[itex]

is equivalent to

[itex]r' = B^n r + \alpha - ((B y + \beta)^n - B^n y ^n)[itex]

Thus, we don't actually need [itex]x[itex], and since [itex]r = x - y^n[itex] and [itex]x<(y+1)^n[itex], [itex]r<(y+1)^n-y^n[itex] or [itex]rn. Also, the [itex]B^n y^n[itex] we subtract in the new test cancels the one in [itex](B y + \beta)^n[itex], so now the highest power of y we have to evaluate is [itex]y^{n-1}[itex] rather than [itex]y^n[itex].

The final algorithm is:

1. Initialize r and y to 0
2. Repeat until desired precision is obtained:
1. Let α be the next aligned block of digits from the radicand
2. Let β be the largest β such that [itex](B y + \beta)^n - B^n y^n \le B^n r + \alpha[itex]
3. Let [itex]y' = B y + \beta[itex]
4. Let [itex]r' = B^n r + \alpha - ((B y + \beta)^n - B^n y^n)[itex]
5. Assign [itex]x \leftarrow x'[itex] and [itex]r \leftarrow r'[itex]
3. [itex]y[itex] is the largest integer such that [itex]y^n

### Paper and pencil nth roots

As noted above, this algorithm is similar to long division, and it lends itself to the same notation:

     1.  4   4   2   2   4
----------------------
3/ 3.000 000 000 000 000
/\/  1 = 300*(0^2)*1+30*0*(1^2)+1^3
-
2 000
1 744 = 300*(1^2)*4+30*1*(4^2)+4^3
-----
256 000
241 984 = 300*(14^2)*4+30*14*(4^2)+4^3
-------
14 016 000
12 458 888 = 300*(144^2)*2+30*144*(2^2)+2^3
----------
1 557 112 000
1 247 791 448 = 300*(1442^2)*2+30*1442*(2^2)+2^3
-------------
309 320 552 000
249 599 823 424 = 300*(14422^2)*4+30*14422*(4^2)+4^3
---------------
59 720 728 576


Note that after the first iteration or two the leading term dominates the [itex](B y + \beta)^n - B^n y^n[itex], so we can get an often correct first guess at β by dividing [itex]B r + \alpha[itex] by [itex]n B^{n-1} y^{n-1}[itex].

### Performance

On each iteration, the most time consuming task is to select β. We know that there are B possible values, so we can find β using [itex]O(\log(B))[itex] comparisons. Each comparison will require evaluating [itex](B y +\beta)^n - B^n y^n[itex]. In the kth iteration, y has k digits, and the polynomial can be evaluated with [itex]2 n - 4[itex] multiplications of up to [itex]k(n-1)[itex] digits and [itex]n - 2[itex] additions of up to [itex]k(n-1)[itex] digits, once we know the powers of y and β up through [itex]n-1[itex] for y and n for β. β has a restricted range, so we can get the powers of β in constant time. We can get the powers of y with [itex]n-2[itex] multiplications of up to [itex]k(n-1)[itex] digits. Assuming n-digit multiplication takes time [itex]O(n^2)[itex] and addition takes time [itex]O(n)[itex], we take time [itex]O(k^2 n^2)[itex] for each comparison, or time [itex]O(k^2 n^2 \log(B))[itex] to pick β. The remainder of the algorithm is addition and subtraction that takes time [itex]O(k)[itex], so each iteration takes [itex]O(k^2 n^2 \log(B))[itex]. For all k digits, we need time [itex]O(k^3 n^2 \log(B))[itex].

The only internal storage needed is r, which is [itex]O(k)[itex] digits on the kth iteration. That this algorithm doesn't have bounded memory usage puts an upper bound on the number of digits which can be computed mentally, unlike the more elementary algorithms of arithmetic. Unfortunately, any bounded memory state machine with periodic inputs can only produce periodic outputs, so there are no such algorithms which can compute irrational numbers from rational ones, and thus no bounded memory root extraction algorithms.

Note that increasing the base increases the time needed to pick β by a factor of [itex]O(log(B))[itex], but decreases the number of digits needed to achieve a given precision by the same factor, and since the algorithm is cubic time in the number of digits, increasing the base gives an overall speedup of [itex]O(\log^2(B))[itex]. When the base is larger than the radicand, the algorithm degenerates to binary search, so it follows that this algorithm is completely useless, as it is always outperformed by much simpler binary search, and has the same memory complexity.

## Examples

### Square root of 2 in binary

      1. 0  1  1  0  1
------------------
/ 10.00 00 00 00 00     1
/\/   1                  + 1
-----               ----
1 00                100
0               +  0
--------            -----
1 00 00             1001
10 01            +   1
-----------         ------
1 11 00          10101
1 01 01         +    1
----------      -------
1 11 00       101100
0      +     0
----------   --------
1 11 00 00    1011001
1 01 10 01          1
----------
1 01 11 remainder


### Square root of 3

     1. 7  3  2  0  5
----------------------
/ 3.00 00 00 00 00
/\/  1 = 20*0*1+1^2
-
2 00
1 89 = 20*1*7+7^2
----
11 00
10 29 = 20*17*3+3^2
-----
71 00
69 24 = 20*173*2+2^2
-----
1 76 00
0 = 20*1732*0+0^2
-------
1 76 00 00
1 73 20 25 = 20*17320*5+5^2
----------
2 79 75


### Cube root of 5

     1.  7   0   9   9   7
----------------------
3/ 5.000 000 000 000 000
/\/  1 = 300*(0^2)*1+30*0*(1^2)+1^3
-
4 000
3 913 = 300*(1^2)*7+30*1*(7^2)+7^3
-----
87 000
0 = 300*(17^2)*0+30*17*(0^2)+0^3
-------
87 000 000
78 443 829 = 300*(170^2)*9+30*170*(9^2)+9^3
----------
8 556 171 000
7 889 992 299 = 300*(1709^2)*9+30*1709*(9^2)+9^3
-------------
666 178 701 000
614 014 317 973 = 300*(17099^2)*7+30*17099*(7^2)+7^3
---------------
52 164 383 027


### Fourth root of 7

     1.   6    2    6    5    7
---------------------------
4/ 7.0000 0000 0000 0000 0000
/\/  1 = 4000*(0^3)*1+600*(0^2)*(1^2)+40*0*(1^3)+1^4
-
6 0000
5 5536 = 4000*(1^3)*6+600*(1^2)*(6^2)+40*1*(6^3)+6^4
------
4464 0000
3338 7536 = 4000*(16^3)*2+600*(16^2)*(2^2)+40*16*(2^3)+2^4
---------
1125 2464 0000
1026 0494 3376 = 4000*(162^3)*6+600*(162^2)*(6^2)+40*162*(6^3)+6^4
--------------
99 1969 6624 0000
86 0185 1379 0625 = 4000*(1626^3)*5+600*(1626^2)*(5^2)+
-----------------   40*1626*(5^3)+5^4
13 1784 5244 9375 0000
12 0489 2414 6927 3201 = 4000*(16265^3)*7+600*(16265^2)*(7^2)+
----------------------   40*16265*(7^3)+7^4
1 1295 2830 2447 6799


• Art and Cultures
• Countries of the World (http://www.academickids.com/encyclopedia/index.php/Countries)
• Space and Astronomy