Jump to content

User:Marc Schroeder/sandbox3

From Wikipedia, the free encyclopedia

Deze sandbox hoort bij Methods of computing square roots


Continued fraction expansion

[edit]

Square roots

[edit]

The identity

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

Computation of the convergents

A general continued fraction is an expression of the form

whish is in prefix [2] notation:

Here:

typedef struct
{
	double p;	// numerator
	double q;	// denominator
} Frac;

or

typedef struct
{
	unsigned long long int p;	// numerator
	unsigned long long int q;	// denominator
} Frac;
Frac sqrt_continued_fraction( Frac x, int k )
{
	Frac current_tail, next_tail;

	for( int i = k ; i > 0 ; i-- )
	{
		// set current_tail to x-1
		current_tail.p = x.p - x.q;
		current_tail.q = x.q;

		if( i == k )
		{
			// last tail: divide (x-1) by 2
			current_tail.q = current_tail.q * 2;
		}
		else
		{
			// 0 < i <= k
			current_tail.p = current_tail.p * next_tail.q;
			current_tail.q = current_tail.q * ((2 * next_tail.q) + next_tail.p);
		} // i > 1

		next_tail = current_tail;
	} // for

	// add first term (= 1) to current_tail
	current_tail.p = current_tail.p + current_tail.q;
	return current_tail;
}


Quadratic irrationals (numbers of the form , 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 Since we have , we can express the square root of S as

By applying this expression for to the denominator term of the fraction, we have

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 [3]

When throughout, an even more compact notation is:[4]

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:[5]

For 2, the value of is 1, so its representation is:

Proceeding this way, we get a generalized continued fraction for the square root as

The first step to evaluating such a fraction [6] 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, is 1 and for 2, is 1, so the numerical continued fraction for 3 denominators is:

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):

Finally (step 3), divide the numerator by the denominator of the rational fraction to obtain the approximate value of the root:

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 , 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:

S ~decimal continued fraction convergents
2 1.41421
3 1.73205
5 2.23607
6 2.44949
10 3.16228
1.77245
1.64872
1.27202

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

[edit]

Quadratic irrationals (numbers of the form , 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 Since we have , we can express the square root of S as

By applying this expression for to the denominator term of the fraction, we have

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 [7]

When throughout, an even more compact notation is:[8]

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:[9]

For 2, the value of is 1, so its representation is:

Proceeding this way, we get a generalized continued fraction for the square root as

The first step to evaluating such a fraction [6] 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, is 1 and for 2, is 1, so the numerical continued fraction for 3 denominators is:

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):

Finally (step 3), divide the numerator by the denominator of the rational fraction to obtain the approximate value of the root:

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 , 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:

S ~decimal continued fraction convergents
2 1.41421
3 1.73205
5 2.23607
6 2.44949
10 3.16228
1.77245
1.64872
1.27202

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.