Number

A fraction that never closes

Euclid's algorithm throws away everything except the number of squares it peeled at each step. Those counts are a second name for the number it started from — one that terminates exactly when the ratio is a ratio.

Worth reading first: The oldest algorithm, drawn as a tiling.

Euclid’s algorithm takes a rectangle and peels the largest square off it, over and over, until nothing is left. Drawn as a tiling it computes a greatest common divisor. What this essay is about is the part of that process the greatest common divisor throws away: how many squares came off at each step.

Euclid's algorithm on a 34 by 13 rectangleThe rectangle is tiled by peeling off the largest square that fits, again and again, until nothing is left.131385322 × 131 × 81 × 51 × 31 × 22 × 1gcd(34, 13) = 1
Fig. 1 A 3434 by 1313 rectangle peeled into squares. Two squares of side 1313, then one of side 88, then one of 55, then one of 33, then one of 22, then two of side 11. The generator checks that the squares tile the rectangle exactly and that the last one has side gcd(34,13)\gcd(34, 13).

The counts are 2,1,1,1,1,22, 1, 1, 1, 1, 2. Everything else about the picture — the sizes, the positions, the colours — can be reconstructed from those six numbers and the starting rectangle’s shape, because each count says how many times the current short side goes into the current long one, and that is the only decision the algorithm ever makes.

Read as a sequence, the counts are a second name for the ratio 34/1334/13. Written out, that name looks like this:

3413=2+11+11+11+11+12\frac{34}{13} = 2 + \cfrac{1}{1 + \cfrac{1}{1 + \cfrac{1}{1 + \cfrac{1}{1 + \cfrac{1}{2}}}}}

The tower

The nesting is the whole notation, and it is easier to look at than to typeset.

34/13 as a continued fractionThe nested fraction, one quotient per step, descending to the right.=34/132 +11 +11 +11 +11 +12[2; 1, 1, 1, 1, 2] — and it stops, because the ratio is a ratio
Fig. 2 The same six numbers, arranged as the fraction they name. Each quotient is followed by a bar, and under the bar sits the rest of the expansion. The generator rebuilds the original ratio from the tower and refuses to draw one that does not come back to where it started.

The reason the tower is the natural way to write the algorithm’s output is that each step of Euclid’s algorithm is a step of the tower. Peeling qq squares off an aa by bb rectangle leaves a bb by rr rectangle with a=qb+ra = qb + r; dividing through by bb gives

ab=q+rb=q+1b/r,\frac{a}{b} = q + \frac{r}{b} = q + \cfrac{1}{\,b/r\,},

and b/rb/r is the next rectangle’s ratio. The recursion is the algorithm and the algorithm is the recursion. Nothing has been added; the fraction is the peeling, transcribed.

Written in a line the expansion is [2;1,1,1,1,2][2; 1, 1, 1, 1, 2], with a semicolon after the whole-number part because that one is not like the others — it can be zero or negative, and the rest cannot.

One wrinkle is worth disposing of at once, because it is the only ambiguity in the notation. The last quotient can always be split: [2;1,1,1,1,2][2; 1, 1, 1, 1, 2] and [2;1,1,1,1,1,1][2; 1, 1, 1, 1, 1, 1] are the same number, since a final 22 is 1+1/11 + 1/1. Every rational therefore has exactly two expansions, one ending in a quotient above one and one ending in a quotient of one, and picking either convention makes the representation unique. This is the continued-fraction version of 0.999=10.999\ldots = 1, and it has the same cause: a positional system with a degenerate case at the end.

Apart from that, the representation is completely rigid. Decimal expansions are not — they depend on a choice of base, and a number that looks tidy in base ten can look arbitrary in base seven. A continued fraction is built from the number’s own arithmetic and uses no base at all, which is why it can detect properties, like being the root of a quadratic, that no decimal expansion can see.

Terminating is the same as being a ratio

Euclid’s algorithm on whole numbers always stops. Each remainder is strictly smaller than the last and they are all non-negative, so the sequence of remainders is a strictly decreasing sequence of whole numbers, which cannot go on forever. That is the standard termination argument and it is the reason a rational number has a finite continued fraction.

Turn it around and the statement becomes interesting. If the peeling never stops, the sides had no common measure at all — because a common measure would have to divide every remainder, and the remainders are getting smaller than any fixed length.

Square-peeling on a 1 by √2 rectangleThe same construction as Euclid's algorithm, run on a rectangle whose sides have no common measure. It never terminates.1 square2 squares2 squares2 squares2 squares2 squares2 squares2 squares…and so on, forever1 : √2
Fig. 3 A rectangle whose sides are 11 and 2\sqrt{2}. One square comes off, then two, then two, then two — and the strip left over is always similar to the strip before it, so the process cannot end. The generator checks the counts against the continued fraction of 2\sqrt{2}, computed from the number rather than from the drawing.

2\sqrt{2} has expansion [1;2,2,2,][1; 2, 2, 2, \ldots], repeating forever. The proof that it repeats is a single observation: after the first square is removed, the leftover strip has the same proportions as a strip that appeared earlier, so the algorithm is in a state it has been in before and must do the same thing again.

That self-similarity is the whole of it. The peeling never terminates, so 2\sqrt{2} is not a ratio of whole numbers — which is the irrationality proof in a form Euclid’s contemporaries would have recognised, since to them incommensurability was exactly this: two lengths with no common unit.

The one that repeats fastest

Every quotient in an expansion measures how much of the current rectangle the current square accounts for. A large quotient means the square nearly fills the rectangle and very little is left over; a quotient of one means the square barely gets started and almost everything is left.

Square-peeling on a 1 by φ rectangleThe same construction as Euclid's algorithm, run on a rectangle whose sides have no common measure. It never terminates.1 square1 square1 square1 square1 square1 square1 square1 square1 square…and so on, forever1 : φ
Fig. 4 The golden rectangle. Every quotient is one: exactly one square comes off at every step, and what is left is a smaller golden rectangle. This is the slowest possible peeling — the largest square never accounts for more than it has to.

The golden ratio’s expansion is [1;1,1,1,][1; 1, 1, 1, \ldots], all ones, which is the extreme case in a precise sense: no expansion can have smaller quotients, because a quotient of zero would mean the short side is longer than the long one.

φ as a continued fractionThe nested fraction, one quotient per step, descending to the right.=φ1 +11 +11 +11 +11 +11[1; 1, 1, 1, 1, 1, …] — and it does not stop
Fig. 5 The tower for φ\varphi. Every level is the same, which is another way of writing φ=1+1/φ\varphi = 1 + 1/\varphi — the defining equation, read off the picture rather than solved for.

That self-reference is the same fact as the self-similarity of the rectangle, and it is why the golden ratio keeps appearing in places that have nothing to do with each other. A shape that reproduces itself after one operation and a number that reproduces itself after one operation are the same object described in two languages, which is the connection this collection has already met from the geometric side.

The convergents

Stopping the tower early gives a fraction. Stopping at each level in turn gives a sequence of them, and the sequence is the reason continued fractions are worth having.

The convergents of φEach convergent marked on a number line, alternating above and below the value it approaches.φ = 1.6180342/13/25/38/513/821/1334/217 convergents, each one closer than the last and on the other sidethe last of them, 34/21, is wrong by 1.0e-3
Fig. 6 The convergents of φ\varphi, marked on a number line. Each one is closer than the last, and each one sits on the opposite side of the value from its predecessor — the generator checks both, off the same list the marks are drawn from.

For φ\varphi the convergents are 1/11/1, 2/12/1, 3/23/2, 5/35/3, 8/58/5, 13/813/8: consecutive Fibonacci numbers, which is the second place that sequence turns up in this picture and the first place it is inevitable. The recurrence that builds convergents from quotients is

pk=akpk1+pk2,qk=akqk1+qk2,p_k = a_k p_{k-1} + p_{k-2}, \qquad q_k = a_k q_{k-1} + q_{k-2},

and when every aka_k is one, that is the Fibonacci recurrence with no room for anything else to happen.

Two properties make the convergents useful rather than merely available. They alternate around the value, so any two consecutive ones bracket it. And neighbouring convergents satisfy

pkqk1pk1qk=±1,p_k q_{k-1} - p_{k-1} q_k = \pm 1,

a determinant of one, which says the two fractions are as close together as two fractions with those denominators can possibly be. Everything about how well continued fractions approximate follows from that single identity, and it is checked in every figure on this page that draws a convergent.

How good the approximations are

The determinant identity gives a bound immediately: xpk/qk<1/(qkqk+1)|x - p_k/q_k| < 1/(q_k q_{k+1}), and since qk+1>qkq_{k+1} > q_k that is better than 1/qk21/q_k^2. A fraction that accurate is not something a search would find; the denominators that work are rare.

How good each convergent is, for its sizeOne over q squared times the error, against the denominator q, on logarithmic axes.√5φ√2eπdenominator q, logarithmichigher is a better approximation for the size of the denominatorevery other constant spikes far above the √5 line; the golden ratio closes on it and stays
Fig. 7 For each convergent, one over q2q^2 times the error — higher means a better approximation for the size of the denominator. Every irrational has convergents above the 5\sqrt5 line, which is Hurwitz’s theorem. π\pi spikes two orders of magnitude above it; φ\varphi closes onto the line and stays there.

π\pi’s spike is 355/113355/113, accurate to seven decimal places with a three-digit denominator, and it is a direct consequence of the quotient 292292 appearing early in π\pi’s expansion [3;7,15,1,292,][3; 7, 15, 1, 292, \ldots]. A large quotient means the previous convergent was already very good — the square nearly filled the rectangle — so a big number in the expansion is a announcement that an unusually accurate fraction has just gone past.

φ\varphi, with all quotients equal to one, never announces anything. Its convergents are as bad as convergents are allowed to be, and this is the precise sense in which the golden ratio is the hardest number to approximate by fractions. The pigeonhole argument gives a floor that every irrational must beat infinitely often; φ\varphi beats it and does no better, forever.

What the picture cannot show

The peeling figures stop at nine steps because a drawing has to stop somewhere, and where they stop is where the interesting behaviour of most numbers begins.

For 2\sqrt{2} and φ\varphi the picture is honest, because the expansions repeat and nine steps show the whole pattern. Lagrange proved in 1770 that this happens exactly for the quadratic irrationals — the roots of quadratics with whole-number coefficients — so a repeating expansion is a complete characterisation of one kind of number, which is a rare thing to have.

For everything else the drawing shows a beginning and nothing more. ee has expansion [2;1,2,1,1,4,1,1,6,][2; 1, 2, 1, 1, 4, 1, 1, 6, \ldots], a pattern that continues but is not periodic. π\pi has no known pattern at all, and whether its quotients are bounded is an open question: nobody can rule out a quotient of a billion appearing somewhere, which would mean an astonishingly good rational approximation to π\pi that no one has found.

So the figures show a mechanism working and cannot show what it produces in the long run. That is the standing limitation of everything in this field, and it is sharper here than elsewhere: the algorithm is completely deterministic, entirely elementary, and its output for the most famous constant in mathematics is not understood.

What is known is statistical, and it has a strange shape. Gauss observed, and Kuzmin proved in 1928, that for almost every real number the quotients follow a fixed distribution: a quotient equals kk with probability log2(1+1/k(k+2))\log_2(1 + 1/k(k+2)), so about 41%41\% of quotients are 11, about 17%17\% are 22, and large quotients are rare but not rare enough for their average to exist. The geometric mean of the quotients converges to Khinchin’s constant, 2.6852.685\ldots, for almost every number — and it is not known whether π\pi, ee or 2\sqrt2 is one of them. The theorem covers everything except every number anybody can name, which is a pattern that also afflicts random walks and normal numbers: the typical case is completely understood and no specific case is.

The calendar, and a gear train

The reason continued fractions were developed is not the reason they are taught. They were developed because somebody needed to approximate an awkward ratio with a fraction whose denominator was small enough to build.

The year is about 365.2422365.2422 days long. The fractional part, expanded, is [0;4,7,1,3,5,][0; 4, 7, 1, 3, 5, \ldots], and its convergents are 14\tfrac14, 729\tfrac{7}{29}, 833\tfrac{8}{33}, 31128\tfrac{31}{128}.

31/128 as a continued fractionThe nested fraction, one quotient per step, descending to the right.=31/1280 +14 +17 +11 +13[0; 4, 7, 1, 3] — and it stops, because the ratio is a ratio
Fig. 8 The tower for 31128\tfrac{31}{128}, the fourth convergent of the year’s fractional part and the same expansion truncated: [0;4,7,1,3][0; 4, 7, 1, 3]. The first quotient is 44 — hence a leap year every four years — and the second is 77, which says that rule is already good enough that seven repetitions of it pass before the error is worth correcting.

The Julian calendar takes the first convergent, 14\tfrac14: one leap year in four. That overshoots by about eleven minutes a year, which is three days in four centuries, and by 1582 the accumulated error had moved the equinox by ten days. The Gregorian reform uses 97400\tfrac{97}{400} — leap years except centuries, except centuries divisible by four hundred — which is not a convergent but is very close to 31128\tfrac{31}{128}, and is accurate to about one day in three thousand years.

The interesting question is why the reform chose four hundred rather than the better and simpler 128128. The answer is that a rule has to be usable by people who are not doing arithmetic, and dividing by four hundred is easy in a decimal-and-century calendar while dividing by 128128 is not. The mathematics offers the optimal denominators; the choice among them is made on other grounds entirely.

Huygens faced the same problem with no such escape. Building a mechanical planetarium in 1680, he needed gears whose tooth counts were in the ratio of Saturn’s period to the Earth’s — 77,708,43177{,}708{,}431 to 2,640,8582{,}640{,}858 — and no workshop cuts a gear with seventy-seven million teeth. He expanded the ratio as a continued fraction and took a convergent: 206/7206/7. The error is under a thousandth of a percent, and the gears are small enough to make. This is the earliest clear use of the convergents as the answer to “what is the best small approximation”, and it comes from a workshop rather than from a proof.

What the algorithm costs

Every quotient equal to one is also the worst case for the algorithm’s running time, and the two facts are the same fact.

Peeling one square per step removes as little as possible, so the number of steps is as large as it can be for the size of the numbers involved. The rectangles where this happens are the ones with consecutive Fibonacci numbers as sides — 3434 and 2121, then 2121 and 1313, and so on down. That is Lamé’s theorem, from 1844: the number of steps Euclid’s algorithm takes is at most about logφ\log_\varphi of the smaller number, with equality exactly for Fibonacci pairs.

Lamé’s result is often described as the first analysis of an algorithm’s worst case, some hundred and thirty years before that became a subject. What makes it work is that the worst case is not found by searching — it is read off the observation that small quotients mean slow progress, and the sequence with the smallest possible quotients is forced.

The practical consequence is a good one. Even the worst case is logarithmic, so Euclid’s algorithm on two numbers of a thousand digits takes a few thousand steps rather than anything alarming, and it remains the method actually used.

It also explains a piece of engineering folklore. A hash table whose stride is chosen as a fraction of its size performs best when that fraction is hard to approximate — because a stride close to a simple ratio revisits a few residues quickly, and a stride close to φ\varphi spreads out for as long as possible before repeating. The property that makes the golden ratio the slowest case for Euclid’s algorithm makes it the best case for anything wanting to avoid a pattern, and the two statements are the same statement with the sign flipped.

Where the ladder goes next

The convergents raise a question this essay has only gestured at: they are the best approximations, and “best” needs a definition and a proof. That is how close a fraction can get, which reaches the same fractions from a completely different direction — a pigeonhole argument with no algorithm in it anywhere.

The determinant of one that makes the convergents work is also the rule that generates every fraction exactly once, in a tree whose left-and-right paths turn out to be continued fraction expansions in disguise. And the non-terminating peel, taken as an argument rather than an illustration, is the classical proof that 2\sqrt{2} is irrational — with the arithmetic that this essay left implicit made exact and drawn as a shrinking square.

What links here

Computed from the collection, not written here: the essays that point at this one.

Reads more easily once this is understood

Essays that name this one as worth reading first.

Shares its objects with

Essays that name at least two of the same things, and that neither author linked.

Named objects

A dashed tag is an object no other essay names yet.

Continued fraction convergentContinued fractionsEuclidean algorithmFibonacciGreatest common divisorGolden ratioIncommensurabilityPeriodicityRational approximationTermination