Number Theory

Continued Fractions and the Best Rational Approximations

Build continued fractions by hand and see why they deliver the best simple fractions for π, √2, calendars and musical tuning.

Key takeaways

  • A simple continued fraction is built by repeatedly taking the whole-number part and flipping what is left over.
  • Rational numbers give continued fractions that stop, and irrational numbers give ones that go on forever.
  • The cut-off values, called convergents, are closer to the target than any other fraction with a denominator no larger than theirs.
  • A large term in the expansion signals that the convergent just before it is unusually accurate.
  • Eventually repeating continued fractions belong exactly to irrational solutions of quadratic equations with integer coefficients.

A continued fraction writes a number as a whole number plus 1 divided by another whole number plus 1 divided by another, and so on. Cut the expression off at any level and you get an ordinary fraction. Those cut-off fractions are the most efficient approximations to the number that exist, in a precise sense.

That is why 22/7 and 355/113 are such good stand-ins for π. They are not lucky guesses. They fall out of a short, mechanical procedure that you can carry out with a basic calculator.

The procedure with a rational number

Take 415/93. The whole-number part is 4, leaving 43/93. Flip the leftover to get 93/43, and repeat.

415/93 = 4 + 43/93
93/43  = 2 + 7/43
43/7   = 6 + 1/7
7/1    = 7 + 0          (nothing left, so stop)

The whole-number parts, 4, 2, 6 and 7, are called the terms. We write 415/93 = [4; 2, 6, 7], with a semicolon after the first term. Spelled out, this is 4 + 1/(2 + 1/(6 + 1/7)).

If those division lines look familiar, they should. They are precisely the steps of the Euclidean algorithm applied to 415 and 93. Since that algorithm always finishes, every rational number has a continued fraction that ends.

Irrational numbers never stop

For an irrational number, the leftover is never zero. Try √2 ≈ 1.41421. The whole part is 1. The leftover is √2 − 1, and its reciprocal is √2 + 1, because (√2 − 1)(√2 + 1) = 1.

Now √2 + 1 has whole part 2, and the leftover is again √2 − 1. The process has returned to where it was, so √2 = [1; 2, 2, 2, 2, ...] forever.

The golden ratio φ = (1 + √5)/2 is simpler still: φ = [1; 1, 1, 1, ...], since φ = 1 + 1/φ. Its convergents are ratios of neighboring Fibonacci numbers: 1, 2, 3/2, 5/3, 8/5, 13/8.

A theorem associated with Euler and Lagrange says that the terms eventually repeat exactly when the number is an irrational root of a quadratic equation with integer coefficients. The number e has a different, striking pattern: [2; 1, 2, 1, 1, 4, 1, 1, 6, 1, 1, 8, ...]. The simple continued fraction of π shows no known pattern at all.

Computing the convergents quickly

The convergent p/q after a given term can be built from the two before it. Multiply the new term by the previous numerator and add the numerator before that. Do the same for denominators.

For π = [3; 7, 15, 1, 292, ...], start with 3/1 and 22/7.

Term Numerator Denominator Convergent value
3 3 1 3.000000000
7 22 7 3.142857143
15 15 × 22 + 3 = 333 15 × 7 + 1 = 106 3.141509434
1 1 × 333 + 22 = 355 1 × 106 + 7 = 113 3.141592920
292 292 × 355 + 333 = 103993 292 × 113 + 106 = 33102 3.141592653

The values alternate: below π, above, below, above, below. Each one is closer than the last.

Neighboring convergents also satisfy a tidy identity: cross-multiplying and subtracting always gives +1 or −1. For example, 22 × 106 − 333 × 7 = 2332 − 2331 = 1. One consequence is that every convergent is automatically in lowest terms.

The continued fraction loop
Flip the leftover Take its reciprocal and start again Check for zero A zero leftover means the number was rational Take the whole part Record it as the next term Subtract it Keep only the fractional leftover 1 2 3 4 Take the whole part Record it as the next term 1 Subtract it Keep only the fractional leftover 2 Check for zero A zero leftover means the number was rational 3 Flip the leftover Take its reciprocal and start again 4

Rebuilding a number from its terms

Going the other way, from terms to a fraction, you can always work from the bottom up. Take [2; 3, 1, 4].

start at the end:      4
next level:            1 + 1/4 = 5/4
next level:            3 + 4/5 = 19/5
top level:             2 + 5/19 = 43/19

So [2; 3, 1, 4] = 43/19. The build-up rule from the previous section gives the same answer: the successive convergents are 2/1, 7/3, 9/4 and 43/19.

Bottom-up evaluation is simple but must be redone if you add a term. The top-down recurrence keeps its work, which is why it is preferred when you do not know in advance how far you will go.

To start the recurrence from nothing, use the pair of artificial values numerator 1, denominator 0 before the first term. With them, the second convergent is computed by the same rule as every later one: for π, 7 × 3 + 1 = 22 and 7 × 1 + 0 = 7.

What "best approximation" means

Any number can be approximated well if you allow a huge denominator. The fair question is how close you can get for a given size of denominator.

Convergents win that contest: no fraction with a denominator up to q gets closer to the target than the convergent p/q (the very first, whole-number convergent can be an exception). So no fraction with denominator 113 or smaller beats 355/113 for π.

The error also has a firm ceiling. The distance from x to the convergent pₙ/qₙ is less than 1/(qₙ × qₙ₊₁), where qₙ₊₁ is the next denominator. Since the next denominator is at least as large, the error is below 1/qₙ².

A partial converse, due to Legendre, is just as useful: if a fraction p/q is within 1/(2q²) of x, then p/q must be one of the convergents. Very good approximations cannot hide anywhere else.

Some fractions that are not convergents are still the closest option up to their own denominator. These are intermediate fractions, formed by using only part of the next term.

Why 292 matters

The next denominator is roughly the next term times the current one. A large next term therefore forces the current convergent to be extremely close.

In π's expansion, the term after 355/113 is 292. That makes 355/113 accurate to about 0.00000027, which is six correct decimal places from a three-digit denominator.

At the other extreme, the golden ratio uses the smallest possible term, 1, at every level. Its convergents close in as slowly as any irrational number's can. This is one reason it is often called the hardest number to approximate, a theme taken further in Diophantine approximation.

Calendars and leap years

The solar year that governs the seasons is about 365.2422 days. A calendar has to approximate the extra 0.2422 with leap days.

Expanding 0.2422 gives [0; 4, 7, 1, ...], with convergents 1/4, 7/29 and 8/33. The first is one leap day every four years, the Julian rule. The fraction 8/33 would mean eight leap days every 33 years, and it is very accurate.

The Gregorian calendar uses 97 leap days in 400 years, or 0.2425. It is not a convergent, but its rule about century years is easy to remember, which counts for a lot in civil life.

Twelve notes in an octave

A pure musical fifth multiplies frequency by 3/2, and an octave multiplies it by 2. To divide the octave into q equal steps with a good fifth, you need p/q close to the base-2 logarithm of 3/2, about 0.58496.

The convergents of that number include 1/2, 3/5, 7/12, 24/41 and 31/53. The fraction 7/12 says: use 12 equal steps and call 7 of them a fifth. That is the standard piano tuning. Systems with 41 or 53 steps give purer fifths at the cost of many more keys.

Gear designers face the same problem. When a ratio needs two gears with manageable tooth counts, convergents are the natural candidates.

Pell's equation and further uses

The convergents of √2 are 1, 3/2, 7/5, 17/12, 41/29 and 99/70. Check what p² − 2q² gives: for 3/2 it is 1, for 7/5 it is −1, for 17/12 it is 1 again. The convergents solve Pell's equation, and for stubborn values such as x² − 61y² = 1 this method finds solutions with many digits that trial and error would never reach.

Continued fractions also help recognize numbers. If a computed decimal produces a gigantic term early in its expansion, it is probably very close to the simple fraction obtained by stopping just before that term.

Solving a repeating expansion backwards

Suppose you are handed the endless expansion [3; 3, 3, 3, ...] and asked which number it is. Call it x. Everything after the first term is a copy of the whole, so

x = 3 + 1/x
x² = 3x + 1                   (multiply by x, which is positive)
x² − 3x − 1 = 0
x = (3 + √13)/2 ≈ 3.3028      (take the positive root)

The convergents are 3, 10/3, 33/10 and 109/33, with values 3, 3.3333, 3.3000 and 3.3030. They close in from alternating sides, just as the theory promises.

The same substitution works for any repeating block. A longer block leads to more algebra but still ends in a quadratic equation. That is the easier half of the theorem about repeating expansions. The harder half, that every quadratic irrational eventually repeats, needs an argument showing that only finitely many different leftovers can ever occur.

For numbers that satisfy equations of degree three or higher, such as the cube root of 2, the terms can be computed one at a time, but no general pattern is known. Whether they stay bounded is an open question as of the mid-2020s.

Try one yourself

Expand 77/30 and list its convergents.

  1. The whole part of 77/30 is 2, leaving 17/30.
  2. Flip to 30/17, whole part 1, leaving 13/17.
  3. Flip to 17/13, whole part 1, leaving 4/13.
  4. Flip to 13/4, whole part 3, leaving 1/4.
  5. Flip to 4/1, whole part 4, leaving nothing.

So 77/30 = [2; 1, 1, 3, 4]. The convergents are 2, 3, 5/2, 18/7 and 77/30. Notice that 18/7 differs from 77/30 by exactly 1/210, which is 1 divided by the product of the two denominators, as the cross-multiplying identity predicts.

Practical cautions

Calculator rounding spoils later terms. Each flip magnifies the error in the leftover, so with ordinary double-precision arithmetic only the first dozen or so terms can usually be trusted.

A rational number also has two simple continued fractions, because a final term n greater than 1 can be rewritten as (n − 1) followed by 1. Most authors choose the shorter form.

Finally, "best" depends on what you measure. If your goal is a denominator that is a power of 10, or a ratio built from small prime factors, a fraction that is not a convergent may suit you better.

Written for 4Mathematics with AI writing tools and not reviewed line by line by a person. Check anything you rely on against a textbook or primary source. Report an error.

Keep reading

All 59 articles in Number Theory →

Latest news on this topic