Continued Fractions

Write a real number as a decimal and you get an endless, seemingly random string of digits for anything irrational — \pi = 3.14159265\ldots gives no hint at all about why \tfrac{22}{7} is such a good approximation, or why \tfrac{355}{113} is a spectacularly better one. Decimals hide the structure. A continued fraction — a nested tower of "whole number plus one over something" — reveals it. Every real number has one, it falls straight out of the Euclidean algorithm, and it turns out to hand you the single best possible fraction for any amount of "digits" you're willing to write down.

The idea is old. Indian mathematicians, including Āryabhaṭa around 500 CE, used continued-fraction style methods to solve indeterminate equations centuries before Europe caught on; European mathematicians such as Rafael Bombelli and Pietro Cataldi rediscovered the technique in the 1500s while wrestling with square roots, and it was Euler in the 1700s who turned it into the systematic theory — complete with the notation [a_0; a_1, a_2, \dots] — that's still used today.

The form

Every continued fraction is a stack of "integer part plus a reciprocal":

a_0 + \cfrac{1}{a_1 + \cfrac{1}{a_2 + \cfrac{1}{a_3 + \cdots}}} = [a_0; a_1, a_2, a_3, \dots].

You build it by peeling off the whole-number part, flipping what's left over, and repeating: x = a_0 + \{x\}, then look at 1/\{x\} the same way, and so on. For a rational number this process runs out of steam and terminates — and, remarkably, the partial quotients a_i it produces are exactly the quotients you'd see running Euclid's algorithm on the numerator and denominator.

Worked example: the continued fraction of \tfrac{47}{17}

Run the Euclidean algorithm on 47 and 17, but keep the quotients instead of throwing them away:

\begin{aligned} 47 &= 2 \cdot 17 + 13 \\ 17 &= 1 \cdot 13 + 4 \\ 13 &= 3 \cdot 4 + 1 \\ 4 &= 4 \cdot 1 + 0 \end{aligned}

The quotients, in order, are 2, 1, 3, 4 — and that is the continued fraction:

\frac{47}{17} = [2; 1, 3, 4] = 2 + \cfrac{1}{1 + \cfrac{1}{3 + \cfrac{1}{4}}}.

Check it by collapsing from the bottom up: 3 + \tfrac14 = \tfrac{13}{4}, so 1 + \tfrac{4}{13} = \tfrac{17}{13}, so 2 + \tfrac{13}{17} = \tfrac{47}{17} — exactly the fraction we started from. The algorithm terminates because the remainders keep shrinking and must eventually hit 0, which is precisely why every rational number's continued fraction is finite.

The best approximations

Truncating a continued fraction gives its convergents — and these are, in a precise sense, the best possible rational approximations for their size of denominator: no other fraction with an equal or smaller denominator gets closer. The convergents of \pi = [3; 7, 15, 1, 292, \dots] are

3, \quad \frac{22}{7}, \quad \frac{333}{106}, \quad \frac{355}{113}, \dots

Compare that with just truncating the decimal. To match \tfrac{355}{113}'s accuracy (six correct decimal places) with a plain decimal, you'd need a fraction with a six-digit denominator, like \tfrac{3141593}{1000000}. The continued fraction gets there with a denominator of just 113 — over four orders of magnitude smaller, for the same precision. The huge next term, 292, is exactly why \tfrac{355}{113} punches so far above its weight: a big partial quotient means the next convergent has to travel a long way before it can improve on the current one, so the current one lingers as an unusually good approximation.

Worked example: building convergents with a recurrence

You don't have to collapse the nested fraction from the bottom every time — there's a fast recurrence that builds each convergent p_k/q_k straight from the partial quotients, working forwards:

p_k = a_k p_{k-1} + p_{k-2}, \qquad q_k = a_k q_{k-1} + q_{k-2},

started from p_{-1}=1, p_0=a_0 and q_{-1}=0, q_0=1. Run it on \pi = [3; 7, 15, 1, \dots]:

\begin{aligned} p_0/q_0 &= 3/1 \\ p_1/q_1 &= (7\cdot 3 + 1)/(7\cdot 1 + 0) = 22/7 \\ p_2/q_2 &= (15\cdot 22 + 3)/(15\cdot 7 + 1) = 333/106 \\ p_3/q_3 &= (1\cdot 333 + 22)/(1\cdot 106 + 7) = 355/113 \end{aligned}

Four short multiplications reproduce every convergent we quoted above, with no fractions to collapse and no risk of arithmetic slips from working bottom-up through a deeply nested expression. This recurrence is exactly how computers generate convergents in practice.

Worked example: the repeating pattern in \sqrt{2}

Irrational numbers give continued fractions that never terminate. Let x = \sqrt{2} \approx 1.41421\ldots. Peel off the whole part: a_0 = 1, leaving a fractional part x - 1. Flip it and rationalise the denominator:

\frac{1}{\sqrt2 - 1} = \frac{\sqrt2+1}{(\sqrt2-1)(\sqrt2+1)} = \frac{\sqrt2+1}{1} = \sqrt2 + 1.

So the next value is \sqrt2 + 1 \approx 2.41421\ldots, whose whole part is a_1 = 2, leaving fractional part \sqrt2 - 1 — the exact same quantity we started flipping. The process has looped back on itself, so every quotient from here on is 2, forever:

\sqrt{2} = [1; \overline{2}] = 1 + \cfrac{1}{2 + \cfrac{1}{2 + \cfrac{1}{2 + \cdots}}}.

This eventual repetition is not special to \sqrt2. A theorem of Lagrange guarantees it for every quadratic irrational, e.g. \sqrt{7} = [2; \overline{1, 1, 1, 4}]. That periodicity is not a curiosity — it is the engine that solves Pell's equation, the next page.

The number that's hardest to approximate

Big partial quotients make a convergent unusually accurate — so what happens if every partial quotient is as small as it can possibly be, forever? Let \varphi be the positive solution of \varphi = 1 + \tfrac{1}{\varphi} (the golden ratio, \varphi \approx 1.618\ldots). Peeling off its whole part and flipping, exactly as with \sqrt2, sends you straight back to \varphi itself:

\varphi = [1; \overline{1}] = 1 + \cfrac{1}{1 + \cfrac{1}{1 + \cfrac{1}{1 + \cdots}}}.

Every partial quotient is the smallest one allowed, 1, so no convergent ever gets the "large next term" boost that made \tfrac{355}{113} so sharp for \pi. That makes \varphi the worst irrational number to approximate by a simple fraction — sometimes called the "most irrational" number — and its convergents are ratios of consecutive Fibonacci numbers: 1, \tfrac21, \tfrac32, \tfrac53, \tfrac85, \tfrac{13}{8}, \dots, closing in on \varphi more slowly than the convergents of almost any other irrational number.

This isn't just a curiosity about one number — it's the extreme case of a general theorem (Hurwitz, 1891): every irrational number has infinitely many convergents p/q satisfying \left|x - \tfrac{p}{q}\right| < \tfrac{1}{\sqrt5\,q^2}, and \sqrt5 is the best constant possible in general — precisely because \varphi (and numbers built from it) squeeze that bound as tight as it can go. The "worst-approximable" number sets the limit for every other irrational.

It is easy to muddle up which numbers terminate and which run forever. The rule is exactly the mirror image of decimals, and it's easy to get backwards:

A number can also have two equivalent finite representations — [2; 3] = [2; 2, 1], since 3 = 2 + \tfrac11 — but that ambiguity only ever shows up for terminating (rational) expansions, never for infinite ones.

Around 480 CE, the Chinese mathematician and astronomer Zu Chongzhi computed \pi to seven decimal places — a record that stood for roughly nine hundred years — and along the way singled out \tfrac{355}{113} as an outstandingly good simple fraction for it, over a millennium before continued fractions were formalised in Europe. We now know exactly why it's so good: it's a convergent sitting right before that freakishly large partial quotient 292 in \pi's continued fraction, and large partial quotients are precisely the signal of an unusually accurate convergent.

The same trick runs mechanical clocks and orreries. A clockmaker who needs one gear to turn at, say, \tfrac{365.2422}{1} times the rate of another (to track the solar year against a daily wheel) can't cut a gear with a fractional number of teeth. Truncating the continued fraction of that ratio hands them the best possible whole-number tooth pair for gears of a given practical size — which is exactly how 18th-century astronomical clocks and planetary orreries, with no calculators and no computers, managed to track the sky for centuries with barely any drift.

The same convergent-hunting even tunes a piano. A "perfect fifth" should ring out at a frequency ratio of exactly 3:2, but a keyboard needs to split every octave into equal semitones, and equal semitones can only ever approximate that ratio. The number you actually need to approximate, \log_2(3/2), has continued-fraction convergents that include \tfrac{7}{12} remarkably early — which is exactly why a standard keyboard divides the octave into 12 equal semitones and calls 7 of them a "fifth." It's the same best-small-denominator logic as \tfrac{355}{113}, just tuning ears instead of gears.