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.
Algorithm
Notation
Let [math]\displaystyle{ B }[/math] be the base of the number system you are using, and [math]\displaystyle{ n }[/math] be the degree of the root to be extracted. Let [math]\displaystyle{ x }[/math] be the radicand processed thus far, [math]\displaystyle{ y }[/math] be the root extracted thus far, and [math]\displaystyle{ r }[/math] be the remainder. Let [math]\displaystyle{ \alpha }[/math] be the next [math]\displaystyle{ n }[/math] digits of the radicand, and [math]\displaystyle{ \beta }[/math] be the next digit of the root. Let [math]\displaystyle{ x' }[/math] be the new value of [math]\displaystyle{ x }[/math] for the next iteration, [math]\displaystyle{ y' }[/math] be the new value of [math]\displaystyle{ y }[/math] for the next iteration, and [math]\displaystyle{ r' }[/math] be the new value of [math]\displaystyle{ r }[/math] for the next iteration. These are all integers.
Invariants
At each iteration, the invariant [math]\displaystyle{ y^n + r = x }[/math] will hold. The invariant [math]\displaystyle{ (y+1)^n\gt x }[/math] will hold. Thus [math]\displaystyle{ y }[/math] is the largest integer less than or equal to the [math]\displaystyle{ n }[/math]th root of [math]\displaystyle{ x }[/math], and [math]\displaystyle{ r }[/math] is the remainder.
Initialization
The initial values of [math]\displaystyle{ x, y }[/math], and [math]\displaystyle{ r }[/math] should be 0. The value of [math]\displaystyle{ \alpha }[/math] for the first iteration should be the most significant aligned block of [math]\displaystyle{ n }[/math] digits of the radicand. An aligned block of [math]\displaystyle{ n }[/math] 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 two digits is 01, the next most significant is 23, and the third most significant is 40.
Main loop
On each iteration we shift in [math]\displaystyle{ n }[/math] digits of the radicand, so we have [math]\displaystyle{ x' = B^n x + \alpha }[/math] and we produce one digit of the root, so we have [math]\displaystyle{ y' = B y + \beta }[/math]. The first invariant implies that [math]\displaystyle{ r' = x' - y'^n }[/math]. We want to choose [math]\displaystyle{ \beta }[/math] so that the invariants described above hold. It turns out that there is always exactly one such choice, as will be proved below.
By definition of a digit, [math]\displaystyle{ 0 \leq \beta \lt B }[/math], and by definition of a block of digits, [math]\displaystyle{ 0 \leq \alpha \lt B^n }[/math]
The first invariant says that:
- [math]\displaystyle{ x' = y'^n + r' }[/math]
or
- [math]\displaystyle{ B^n x + \alpha = (B y + \beta)^n + r'. }[/math]
So, pick the largest integer [math]\displaystyle{ \beta }[/math] such that
- [math]\displaystyle{ (B y + \beta)^n \le B^n x + \alpha. }[/math]
Such a [math]\displaystyle{ \beta }[/math] always exists, since [math]\displaystyle{ 0 \leq \beta \lt B }[/math] and if [math]\displaystyle{ \beta = 0 }[/math] then [math]\displaystyle{ B^n y^n \le B^n x + \alpha }[/math], but since [math]\displaystyle{ y^n \le x }[/math], this is always true for [math]\displaystyle{ \beta = 0 }[/math]. Thus, there will always be a [math]\displaystyle{ \beta }[/math] that satisfies the first invariant
Now consider the second invariant. It says:
- [math]\displaystyle{ (y'+1)^n \gt x' }[/math]
or
- [math]\displaystyle{ (B y + \beta + 1)^n\gt B^n x + \alpha. }[/math]
Now, if [math]\displaystyle{ \beta }[/math] is not the largest admissible [math]\displaystyle{ \beta }[/math] for the first invariant as described above, then [math]\displaystyle{ \beta + 1 }[/math] is also admissible, and we have
- [math]\displaystyle{ (B y + \beta + 1)^n \le B^n x + \alpha. }[/math]
This violates the second invariant, so to satisfy both invariants we must pick the largest [math]\displaystyle{ \beta }[/math] allowed by the first invariant. Thus we have proven the existence and uniqueness of [math]\displaystyle{ \beta }[/math].
To summarize, on each iteration:
- Let [math]\displaystyle{ \alpha }[/math] be the next aligned block of digits from the radicand
- Let [math]\displaystyle{ x' = B^n x + \alpha }[/math]
- Let [math]\displaystyle{ \beta }[/math] be the largest [math]\displaystyle{ \beta }[/math] such that [math]\displaystyle{ (B y + \beta)^n \le B^n x + \alpha }[/math]
- Let [math]\displaystyle{ y' = B y + \beta }[/math]
- Let [math]\displaystyle{ r' = x' - y'^n }[/math]
Now, note that [math]\displaystyle{ x = y^n + r }[/math], so the condition
- [math]\displaystyle{ (B y + \beta)^n \le B^n x + \alpha }[/math]
is equivalent to
- [math]\displaystyle{ (B y + \beta)^n - B^n y^n \le B^n r + \alpha }[/math]
and
- [math]\displaystyle{ r' = x' - y'^n = B^n x + \alpha - (B y + \beta)^n }[/math]
is equivalent to
- [math]\displaystyle{ r' = B^n r + \alpha - ((B y + \beta)^n - B^n y ^n). }[/math]
Thus, we do not actually need [math]\displaystyle{ x }[/math], and since [math]\displaystyle{ r = x - y^n }[/math] and [math]\displaystyle{ x\lt (y+1)^n }[/math], [math]\displaystyle{ r\lt (y+1)^n-y^n }[/math] or [math]\displaystyle{ r\lt n y^{n-1}+O(y^{n-2}) }[/math], or [math]\displaystyle{ r\lt n x^{{n-1}\over n} + O(x^{{n-2}\over n}) }[/math], so by using [math]\displaystyle{ r }[/math] instead of [math]\displaystyle{ x }[/math] we save time and space by a factor of 1/[math]\displaystyle{ n }[/math]. Also, the [math]\displaystyle{ B^n y^n }[/math] we subtract in the new test cancels the one in [math]\displaystyle{ (B y + \beta)^n }[/math], so now the highest power of [math]\displaystyle{ y }[/math] we have to evaluate is [math]\displaystyle{ y^{n-1} }[/math] rather than [math]\displaystyle{ y^n }[/math].
Summary
- Initialize [math]\displaystyle{ r }[/math] and [math]\displaystyle{ y }[/math] to 0.
- Repeat until desired precision is obtained:
- Let [math]\displaystyle{ \alpha }[/math] be the next aligned block of digits from the radicand.
- Let [math]\displaystyle{ \beta }[/math] be the largest [math]\displaystyle{ \beta }[/math] such that [math]\displaystyle{ (B y + \beta)^n - B^n y^n \le B^n r + \alpha. }[/math]
- Let [math]\displaystyle{ y' = B y + \beta }[/math].
- Let [math]\displaystyle{ r' = B^n r + \alpha - ((B y + \beta)^n - B^n y^n). }[/math]
- Assign [math]\displaystyle{ y \leftarrow y' }[/math] and [math]\displaystyle{ r \leftarrow r'. }[/math]
- [math]\displaystyle{ y }[/math] is the largest integer such that [math]\displaystyle{ y^n\lt x B^k }[/math], and [math]\displaystyle{ y^n+r=x B^k }[/math], where [math]\displaystyle{ k }[/math] is the number of digits of the radicand after the decimal point that have been consumed (a negative number if the algorithm has not reached the decimal point yet).
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 = 3(10×0)2×1 +3(10×0)×12 +13 — 2 000 1 744 = 3(10×1)2×4 +3(10×1)×42 +43 ————— 256 000 241 984 = 3(10×14)2×4 +3(10×14)×42 +43 ——————— 14 016 000 12 458 888 = 3(10×144)2×2 +3(10×144)×22 +23 —————————— 1 557 112 000 1 247 791 448 = 3(10×1442)2×2 +3(10×1442)×22 +23 ————————————— 309 320 552 000 249 599 823 424 = 3(10×14422)2×4 +3(10×14422)×42 +43 ——————————————— 59 720 728 576
Note that after the first iteration or two the leading term dominates the [math]\displaystyle{ (B y + \beta)^n - B^n y^n }[/math], so we can get an often correct first guess at [math]\displaystyle{ \beta }[/math] by dividing [math]\displaystyle{ B^n r + \alpha }[/math] by [math]\displaystyle{ n B^{n-1} y^{n-1} }[/math].
Performance
On each iteration, the most time-consuming task is to select [math]\displaystyle{ \beta }[/math]. We know that there are [math]\displaystyle{ B }[/math] possible values, so we can find [math]\displaystyle{ \beta }[/math] using [math]\displaystyle{ O(\log(B)) }[/math] comparisons. Each comparison will require evaluating [math]\displaystyle{ (B y +\beta)^n - B^n y^n }[/math]. In the kth iteration, [math]\displaystyle{ y }[/math] has [math]\displaystyle{ k }[/math] digits, and the polynomial can be evaluated with [math]\displaystyle{ 2 n - 4 }[/math] multiplications of up to [math]\displaystyle{ k(n-1) }[/math] digits and [math]\displaystyle{ n - 2 }[/math] additions of up to [math]\displaystyle{ k(n-1) }[/math] digits, once we know the powers of [math]\displaystyle{ y }[/math] and [math]\displaystyle{ \beta }[/math] up through [math]\displaystyle{ n-1 }[/math] for [math]\displaystyle{ y }[/math] and [math]\displaystyle{ n }[/math] for [math]\displaystyle{ \beta }[/math]. [math]\displaystyle{ \beta }[/math] has a restricted range, so we can get the powers of [math]\displaystyle{ \beta }[/math] in constant time. We can get the powers of [math]\displaystyle{ y }[/math] with [math]\displaystyle{ n-2 }[/math] multiplications of up to [math]\displaystyle{ k(n-1) }[/math] digits. Assuming [math]\displaystyle{ n }[/math]-digit multiplication takes time [math]\displaystyle{ O(n^2) }[/math] and addition takes time [math]\displaystyle{ O(n) }[/math], we take time [math]\displaystyle{ O(k^2 n^2) }[/math] for each comparison, or time [math]\displaystyle{ O(k^2 n^2 \log(B)) }[/math] to pick [math]\displaystyle{ \beta }[/math]. The remainder of the algorithm is addition and subtraction that takes time [math]\displaystyle{ O(k) }[/math], so each iteration takes [math]\displaystyle{ O(k^2 n^2 \log(B)) }[/math]. For all [math]\displaystyle{ k }[/math] digits, we need time [math]\displaystyle{ O(k^3 n^2 \log(B)) }[/math].
The only internal storage needed is [math]\displaystyle{ r }[/math], which is [math]\displaystyle{ O(k) }[/math] digits on the kth iteration. That this algorithm does not 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 [math]\displaystyle{ \beta }[/math] by a factor of [math]\displaystyle{ O(\log(B)) }[/math], 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 [math]\displaystyle{ O(\log^2(B)) }[/math]. When the base is larger than the radicand, the algorithm degenerates to binary search, so it follows that this algorithm is not useful for computing roots with a computer, 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 (27 x 7) ---- 11 00 10 29 = 20×17×3+3^2 (343 x 3) ----- 71 00 69 24 = 20×173×2+2^2 (3462 x 2) ----- 1 76 00 0 = 20×1732×0+0^2 (34640 x 0) ------- 1 76 00 00 1 73 20 25 = 20×17320×5+5^2 (346405 x 5) ---------- 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
See also
External links
- Why the square root algorithm works "Home School Math". Also related pages giving examples of the long-division-like pencil and paper method for square roots.
- Reflections on The Square Root of Two "Medium". With an example of a C++ implementation.
Original source: https://en.wikipedia.org/wiki/Shifting nth root algorithm.
Read more |