User:Datheun/sandbox

Nth Root Calculation
The principal nth root $$\sqrt[n]{A}$$ of a positive real number A, is the positive real solution of the equation


 * $$x^n = A$$

(for integer n there are n distinct complex solutions to this equation if $$A > 0$$, but only one is positive and real).

There is a very fast-converging  nth root algorithm for finding $$\sqrt[n]{A}$$:
 * 1) Make an initial guess $$x_0$$
 * 2) Set $$x_{k+1} = \frac{1}{n} \left[{(n-1)x_k +\frac{A}{x_k^{n-1}}}\right]$$
 * 3) Repeat step 2 until the desired precision is reached.

A special case is the familiar square-root algorithm. By setting n = 2, the iteration rule in step 2 becomes the square root iteration rule:
 * $$x_{k+1} = \frac{1}{2}\left(x_k + \frac{A}{x_k}\right)$$

Several different derivations of this algorithm are possible. One derivation shows it is a special case of Newton's method (also called the Newton-Raphson method) for finding zeros of a function $$f(x)$$ beginning with an initial guess. Although Newton's method is iterative, meaning it approaches the solution through a series of increasingly accurate guesses, it converges very quickly. The rate of convergence is quadratic, meaning roughly that the number of bits of accuracy doubles on each iteration (so improving a guess from 1 bit to 64 bits of precision requires only 6 iterations). For this reason, this algorithm is often used in computers as a very fast method to calculate square roots.

For large n, the nth root algorithm is somewhat less efficient since it requires the computation of $$x_k^{n-1}$$ at each step, but can be efficiently implemented with a good exponentiation algorithm.

Rough estimation
Many of the methods for calculating square roots of a positive real number S require an initial seed value. If the initial value is too far from the actual square root, the calculation will be slowed down. It is therefore useful to have a rough estimate, which may be very inaccurate but easy to calculate. If S ≥ 1, let D be the number of digits to the left of the decimal point. If S < 1, let D be the negative of the number of zeros to the immediate right of the decimal point. Then the rough estimation is this:
 * If D is odd, D = 2n + 1, then use $$ \sqrt{S} \approx 2 \cdot 10^n.$$
 * If D is even, D = 2n + 2, then use $$ \sqrt{S} \approx 6 \cdot 10^n.$$

Two and six are used because they approximate the geometric means of the lowest and highest possible values with the given number of digits: $$\sqrt{\sqrt{1 \cdot 10}} = \sqrt[4]{10} \approx 2 \,$$ and $$\sqrt{\sqrt{10 \cdot 100}} = \sqrt[4]{1000} \approx 6 \,.$$

When working in the binary numeral system (as computers do internally), an alternative method is to use $$2^{\left\lfloor D/2\right\rfloor}$$ (here D is the number of binary digits).