User:Marc Schroeder/sandbox3

Deze sandbox hoort bij Methods of computing square roots

Square roots
The identity



\begin{array}{lcl} \sqrt{x} & = & 1 + \sqrt{x} - 1 \\ & = & 1+\frac{(\sqrt{x} - 1)(\sqrt{x} + 1)}{(\sqrt{x} + 1)} \\ & = & 1+\frac{x-1}{1+\sqrt{x}} \end{array} $$

leads via recursion to the generalized continued fraction for any square root:

\begin{array}{lcl} \sqrt{x} & = & 1+\frac{x-1}{1 + \left(1+\frac{x-1}{1+\sqrt{x}}\right )} = 1+\frac{x-1}{2 + \frac{x-1}{1+\sqrt{x}}} \\ & = & 1+ \cfrac{x-1}{2 + \cfrac{x-1}{2 + \cfrac{x-1}{2+{\ddots}}}} \\ & \\ & = & 1+ \cfrac{(x-1)}{2+} \cfrac{(x-1)}{2+} \cfrac{(x-1)}{2+}\cdots \\ & = & 1 + \underset{i=1}\overset{\infty}\operatorname{K}\cfrac{x-1}{2} \end{array} $$

Computation of the convergents

A general continued fraction is an expression of the form


 * $$c = a_0 + b_1 / (a_1 + b_2 / (a_2 + b_3 / (a_3 + b_4 / (a_4 + b_5 / (a_5 + b_6 / (a_6 + \dots))))))$$

whish is in prefix notation:
 * $$c = \mathop{+} a_0 \mathop{/} b_1 \mathop{+} a_1 \mathop{/} b_2 \mathop{+} a_2 \mathop{/} b_3 \mathop{+} a_3 \mathop{/} b_4 \mathop{+} a_4 \mathop{/} b_5 \mathop{+} a_5 \mathop{/} b_6 \mathop{+} a_6 \dots $$

Here:
 * $$c = \mathop{+} 1 \mathop{/} \mathop{-} x \ 1 \mathop{+} 2 \mathop{/} \mathop{-} x \ 1 \mathop{+} 2 \mathop{/} \mathop{-} x \ 1 \mathop{+} 2 \mathop{/} \mathop{-} x \ 1 \mathop{+} 2 \mathop{/} \mathop{-} x \ 1 \mathop{+} 2 \mathop{/} \mathop{-} x \ 1 \mathop{+} 2 \dots $$

or

Quadratic irrationals (numbers of the form $$\frac{a+\sqrt{b}}{c}$$, where a, b and c are integers), and in particular, square roots of integers, have periodic continued fractions. Sometimes what is desired is finding not the numerical value of a square root, but rather its continued fraction expansion, and hence its rational approximation. Let S be the positive number for which we are required to find the square root. Then assuming a to be a number that serves as an initial guess and r to be the remainder term, we can write $$S = a^2 + r. $$ Since we have $$S - a^2 = (\sqrt{S} + a)(\sqrt{S} - a) = r$$, we can express the square root of S as
 * $$ \sqrt{S} = a + \frac{r}{a + \sqrt{S}}. $$

By applying this expression for $$\sqrt{S}$$ to the denominator term of the fraction, we have
 * $$ \sqrt{S} = a + \frac{r}{a + (a + \frac{r}{a + \sqrt{S}})} = a + \frac{r}{2a + \frac{r}{a + \sqrt{S}}}. $$

  Compact notation  The numerator/denominator expansion for continued fractions (see left) is cumbersome to write as well as to embed in text formatting systems. So mathematicians have devised several alternative notations, like

\sqrt{S} = a+ \frac{r}{2a+}\, \frac{r}{2a+}\, \frac{r}{2a+}\cdots $$

When $$r = 1$$ throughout, an even more compact notation is:
 * $$[a; 2a, 2a, 2a, \cdots] $$

For repeating continued fractions (which all square roots of non-perfect squares do), the repetend is represented only once, with an overline to signify a non-terminating repetition of the overlined part:
 * $$[a;\overline{2a}]$$

For √2, the value of $$a$$ is 1, so its representation is:
 * $$[1;\overline{2}]$$

Proceeding this way, we get a generalized continued fraction for the square root as $$ \sqrt{S} = a + \cfrac{r}{2a + \cfrac{r}{2a + \cfrac{r}{2a + \ddots}}}$$

The first step to evaluating such a fraction to obtain a root is to do numerical substitutions for the root of the number desired, and number of denominators selected. For example, in canonical form, $$r$$ is 1 and for √2, $$a$$ is 1, so the numerical continued fraction for 3 denominators is:
 * $$ \sqrt{2} \approx 1 + \cfrac{1}{2 + \cfrac{1}{2 + \cfrac{1}{2}}}$$

Step 2 is to reduce the continued fraction from the bottom up, one denominator at a time, to yield a rational fraction whose numerator and denominator are integers. The reduction proceeds thus (taking the first three denominators):
 * $$ 1 + \cfrac{1}{2 + \cfrac{1}{2 + \cfrac{1}{2}}} = 1 + \cfrac{1}{2 + \cfrac{1}{\frac{5}{2}}}$$
 * $$ = 1 + \cfrac{1}{2 + \cfrac{2}{5}} = 1 + \cfrac{1}{\frac{12}{5}}$$
 * $$ = 1 + \cfrac{5}{12} = \frac{17}{12}$$

Finally (step 3), divide the numerator by the denominator of the rational fraction to obtain the approximate value of the root:
 * $$17 \div 12 = 1.42$$ rounded to three digits of precision.

The actual value of √2 is 1.41 to three significant digits. The relative error is 0.17%, so the rational fraction is good to almost three digits of precision. Taking more denominators gives successively better approximations: four denominators yields the fraction $$\frac{41}{29} = 1.4137$$, good to almost 4 digits of precision, etc.

The following are examples of square roots, their simple continued fractions, and their first terms — called convergents — up to and including denominator 99:

In general, the larger the denominator of a rational fraction, the better the approximation. It can also be shown that truncating a continued fraction yields a rational fraction that is the best approximation to the root of any fraction with denominator less than or equal to the denominator of that fraction — e.g., no fraction with a denominator less than or equal to 70 is as good an approximation to √2 as 99/70.

Continued fraction expansion OLD
Quadratic irrationals (numbers of the form $$\frac{a+\sqrt{b}}{c}$$, where a, b and c are integers), and in particular, square roots of integers, have periodic continued fractions. Sometimes what is desired is finding not the numerical value of a square root, but rather its continued fraction expansion, and hence its rational approximation. Let S be the positive number for which we are required to find the square root. Then assuming a to be a number that serves as an initial guess and r to be the remainder term, we can write $$S = a^2 + r. $$ Since we have $$S - a^2 = (\sqrt{S} + a)(\sqrt{S} - a) = r$$, we can express the square root of S as
 * $$ \sqrt{S} = a + \frac{r}{a + \sqrt{S}}. $$

By applying this expression for $$\sqrt{S}$$ to the denominator term of the fraction, we have
 * $$ \sqrt{S} = a + \frac{r}{a + (a + \frac{r}{a + \sqrt{S}})} = a + \frac{r}{2a + \frac{r}{a + \sqrt{S}}}. $$

  Compact notation  The numerator/denominator expansion for continued fractions (see left) is cumbersome to write as well as to embed in text formatting systems. So mathematicians have devised several alternative notations, like

\sqrt{S} = a+ \frac{r}{2a+}\, \frac{r}{2a+}\, \frac{r}{2a+}\cdots $$

When $$r = 1$$ throughout, an even more compact notation is:
 * $$[a; 2a, 2a, 2a, \cdots] $$

For repeating continued fractions (which all square roots of non-perfect squares do), the repetend is represented only once, with an overline to signify a non-terminating repetition of the overlined part:
 * $$[a;\overline{2a}]$$

For √2, the value of $$a$$ is 1, so its representation is:
 * $$[1;\overline{2}]$$

Proceeding this way, we get a generalized continued fraction for the square root as $$ \sqrt{S} = a + \cfrac{r}{2a + \cfrac{r}{2a + \cfrac{r}{2a + \ddots}}}$$

The first step to evaluating such a fraction to obtain a root is to do numerical substitutions for the root of the number desired, and number of denominators selected. For example, in canonical form, $$r$$ is 1 and for √2, $$a$$ is 1, so the numerical continued fraction for 3 denominators is:
 * $$ \sqrt{2} \approx 1 + \cfrac{1}{2 + \cfrac{1}{2 + \cfrac{1}{2}}}$$

Step 2 is to reduce the continued fraction from the bottom up, one denominator at a time, to yield a rational fraction whose numerator and denominator are integers. The reduction proceeds thus (taking the first three denominators):
 * $$ 1 + \cfrac{1}{2 + \cfrac{1}{2 + \cfrac{1}{2}}} = 1 + \cfrac{1}{2 + \cfrac{1}{\frac{5}{2}}}$$
 * $$ = 1 + \cfrac{1}{2 + \cfrac{2}{5}} = 1 + \cfrac{1}{\frac{12}{5}}$$
 * $$ = 1 + \cfrac{5}{12} = \frac{17}{12}$$

Finally (step 3), divide the numerator by the denominator of the rational fraction to obtain the approximate value of the root:
 * $$17 \div 12 = 1.42$$ rounded to three digits of precision.

The actual value of √2 is 1.41 to three significant digits. The relative error is 0.17%, so the rational fraction is good to almost three digits of precision. Taking more denominators gives successively better approximations: four denominators yields the fraction $$\frac{41}{29} = 1.4137$$, good to almost 4 digits of precision, etc.

The following are examples of square roots, their simple continued fractions, and their first terms — called convergents — up to and including denominator 99:

In general, the larger the denominator of a rational fraction, the better the approximation. It can also be shown that truncating a continued fraction yields a rational fraction that is the best approximation to the root of any fraction with denominator less than or equal to the denominator of that fraction — e.g., no fraction with a denominator less than or equal to 70 is as good an approximation to √2 as 99/70.