Skip to content

Continued Fractions

Definition: Continued Fraction

A continued fraction is has this shape:

a0+1a1+1a2+1a3+.

The series (ai)i are the expansion coefficients.

These are useful for approximating irrational numbers by a rational one. One can just cut off the continued fraction at any point and simplify the fraction.

Expansion algorithm

The algorithm to expand a number works like the following. We will take 3 as an example. As it is approximately 1.732, we know 1<3<2. We then subtract the full integer like so:

3=1+(31).

We now know that 31<1 and hence the inverse will be larger than one. We write the above more complicated, like so:

3=1+1131.

The 131 is 1.366, hence we can split off another 1.

3=1+11+1311.

We take 1311, which is smaller than 1, and simplify the fraction first. We first multiply the fraction with 3+1 such that this gives 312=2.

1311=3+121.

This expression evaluates to 0.366, so let us write it again as a double inverse.

1311=113+121.

We simplify the innermost fraction again:

3+121=312.

Such that we have

1311=11312.

Then we can resolve one level of fractions again:

1311=1231.

We can then split off a 2 because the following sub-expression evaluates to 2.732:

231=2+2312.

We then need to simplify the latter part:

2312=23+122=31.

Let us collect the terms that we have so far:

3=1+11+12+(31).

31 is an expression that we have already encountered. The pattern repeats itself and is periodic. The next step would bring us here:

3=1+11+12+11+1311.

We're only interested in the coefficients. There is a non-periodic part of (1) and a periodic part of (1,2). Project Euler suggests the notation [1;(1,2)] for the prefix and the periodic part.

Convergents

The continued fractions are infinitely deep and hence equal the irrational number. When we cut off the expansion at some point, we can get a rational approximation. Let us again take 3 and cut off the expansion at (1,1,2,1). The continued fraction then takes this finite form:

31+11+12+11+12.

We need to simplify this fraction. We start on the inside and simplify:

1+12=32.

Then we need to invert this and simplify with the next coefficient:

2+23=83.

Invert again and simplify with the next coefficient:

1+38=118.

And again:

1+811=1911=1.72.

That is a good approximation to 31.73205.

The algorithm here is just taking a list of coefficients and simplifying the fractions in reverse.