MATH2400 2,635 words·14 min read

Continued Fractions

Introduction to Continued Fractions#

Recall how we previously applied the Euclidean algorithm to the numbers 403403 and 286286:

403=1×286+117,286=2×117+52,117=2×52+13,52=4×13+0.\begin{align*} 403 &= 1 \times 286 + 117, \\ 286 &= 2 \times 117 + 52, \\ 117 &= 2 \times 52 + 13, \\ 52 &= 4 \times 13 + 0. \end{align*}

Suppose we divide each equation through by its original divisor;

403286=1+117286,286117=2+52117,11752=2+1352,5213=4.\begin{align*} \frac{403}{286} &= 1 + \frac{117}{286}, \\ \frac{286}{117} &= 2 + \frac{52}{117}, \\ \frac{117}{52} &= 2 + \frac{13}{52}, \\ \frac{52}{13} &= 4. \end{align*}

Notice how the fraction on the left of each line is the reciprocal of the "remainder" fraction on the line before it; 286117\frac{286}{117} is 117286\frac{117}{286} flipped upside down, and so on. This means we can keep substituting each equation into the one above it:

403286=1+1286117=1+12+12+14.\frac{403}{286} = 1 + \cfrac{1}{\frac{286}{117}} = 1 + \cfrac{1}{2 + \cfrac{1}{2 + \cfrac{1}{4}}}.

This nested expression is called a continued fraction.

Note

Definition
A continued fraction is a representation for a real number written as [q0;q1,q2,q3,… ][q_0; q_1, q_2, q_3, \dots] and interpreted as

q0+1q1+1q2+1q3+⋯,q_0 + \cfrac{1}{q_1 + \cfrac{1}{q_2 + \cfrac{1}{q_3 + \cdots}}},

where q0∈Zq_0 \in \mathbb{Z} and qi∈Z+q_i \in \mathbb{Z}^+ for all i>0i > 0. The entries in the expression may continue forever or terminate.

Basically, we peel off the integer part q0q_0 of our number, leaving something between 00 and 11; flipping that leftover upside down gives a number bigger than 11, so we can peel off its integer part q1q_1, and repeat forever (or until nothing is left over). The entries qiq_i are called the partial quotients. Using this notation, the working above becomes

403286=[1;2,2,4].\frac{403}{286} = [1; 2, 2, 4].

It is important to remember that only q0q_0 is allowed to be zero or negative; every entry after the semicolon must be a positive integer.

Just like with base representations, we classify continued fractions by how their expansions end (or don't).

Note

Definition
A terminating continued fraction is a continued fraction of the form [q0;q1,q2,…,qn][q_0; q_1, q_2, \dots, q_n] for some natural number nn. To ensure uniqueness, we require that qn≠1q_n \neq 1 if n>0n > 0.

Note

Definition
A periodic (or eventually periodic) continued fraction is a continued fraction of the form [q0;q1,q2,…,qk,qk+1,…,qn‾][q_0; q_1, q_2, \dots, \overline{q_k, q_{k+1}, \dots, q_n}] for some positive integers k≤nk \leq n. The line over the periodic part indicates that it repeats indefinitely, so

[q0;q1,…,qk,…,qn‾]=[q0;q1,…,qk,…,qn,qk,…,qn,qk,…,qn,… ].[q_0; q_1, \dots, \overline{q_k, \dots, q_n}] = [q_0; q_1, \dots, q_k, \dots, q_n, q_k, \dots, q_n, q_k, \dots, q_n, \dots].

The qn≠1q_n \neq 1 rule might look arbitrary, but without it every terminating continued fraction would have two different forms, since a final entry of 11 can always be absorbed into the entry before it.

Example. Show that [2;3,1][2;3,1] and [2;4][2;4] represent the same number.

[2;3,1]=2+13+11=2+14=94,\begin{align*} [2;3,1] &= 2 + \cfrac{1}{3 + \cfrac{1}{1}} \\ &= 2 + \frac{1}{4} \\ &= \frac{9}{4}, \end{align*}

and clearly [2;4]=2+14=94[2;4] = 2 + \frac{1}{4} = \frac{9}{4} as well. Therefore the two expansions agree, and the convention qn≠1q_n \neq 1 tells us that [2;4][2;4] is the correct (canonical) way to write 94\frac{9}{4}.

The Three Characterisation Theorems#

These three theorems are what make continued fractions such a strong representation; they can often store more information about a number in less space than any base bb expansion can.

Note

Theorem
Every real number α∈R\alpha \in \mathbb{R} has a unique continued fraction representation.

Compare this with base representations, where some numbers get two expansions (recall 0.9‾=10.\overline{9} = 1 in base 1010). For continued fractions, the qn≠1q_n \neq 1 convention removes the only source of ambiguity, so every real number gets exactly one expansion.

Note

Theorem
A number α∈R\alpha \in \mathbb{R} has a terminating continued fraction expansion if and only if α\alpha is rational.

This makes sense because the continued fraction expansion of a rational number is just the Euclidean algorithm in disguise (we will see this explicitly below), and the Euclidean algorithm always terminates.

Note

Theorem
A number α∈R\alpha \in \mathbb{R} has a periodic continued fraction expansion if and only if α\alpha is a quadratic irrational — that is, the root of a quadratic polynomial with rational coefficients.

Quadratic irrationals are numbers of the form a+bda + b\sqrt{d} with a,ba,b rational and dd a non-square positive integer; things like 2\sqrt{2}, 6−2\sqrt{6}-2 or 1+52\frac{1+\sqrt{5}}{2}. Notice the pattern shift compared to base expansions:

  • In base bb: terminating or periodic   ⟺  \iff rational.
  • Continued fractions: terminating   ⟺  \iff rational, and periodic   ⟺  \iff quadratic irrational.

So continued fractions climb one level higher; the "repeating" behaviour now captures a whole class of irrational numbers. A number like π\pi, which is neither rational nor a quadratic irrational, must therefore have an expansion that never terminates and never repeats.

The algorithms in the rest of this note (continued fraction →\to number, and number →\to continued fraction) are well-defined in both directions, which is what justifies these theorems.

Converting Continued Fractions to Simple Fractions#

Terminating Continued Fractions#

To convert a terminating continued fraction to a simple fraction (an ordinary numerator over an integer denominator), we just write out the nested fraction and evaluate it from the bottom up.

Example. Write [1;2,3,4][1;2,3,4] as a simple fraction.

[1;2,3,4]=1+12+13+14=1+12+1134=1+12+413=1+13013=1+1330=4330.\begin{align*} [1;2,3,4] &= 1 + \cfrac{1}{2 + \cfrac{1}{3 + \cfrac{1}{4}}} \\ &= 1 + \cfrac{1}{2 + \cfrac{1}{\frac{13}{4}}} \\ &= 1 + \cfrac{1}{2 + \frac{4}{13}} \\ &= 1 + \cfrac{1}{\frac{30}{13}} \\ &= 1 + \frac{13}{30} \\ &= \frac{43}{30}. \end{align*}

Therefore, [1;2,3,4]=4330[1;2,3,4] = \frac{43}{30}; a terminating expansion gave us a rational number, as the theorem promised.

Periodic Continued Fractions#

For a periodic continued fraction we cannot evaluate from the bottom up, because there is no bottom. Instead, we exploit the self-similarity of the repeating part:

  • Set xx to be the periodic part of the continued fraction.
  • Expand out the continued fraction representation of xx until another instance of xx appears further down the fractional chain.
  • Replace that new instance with the variable xx again.
  • Simplify the equation to produce a quadratic equation in xx.
  • Solve the quadratic (it will have up to two solutions) and choose the solution that matches xx (i.e. the one with the correct sign/size).
  • Once the periodic part is known, resolve any non-periodic entries by simply evaluating the continued fraction.

Example. Write [0;2,4‾][0;\overline{2,4}] as a simple fraction.
Let x=[0;2,4‾]x = [0;\overline{2,4}]. Expanding the chain, the part after the 44 is another copy of xx;

x=12+14+x.x = \cfrac{1}{2 + \cfrac{1}{4 + x}}.

Now we simplify to a quadratic:

x=12(4+x)+14+xx=4+x2x+9x(2x+9)=4+x2x2+9x=4+x2x2+8x−4=0x2+4x−2=0x=−4±16+82x=−2±6.\begin{align*} x &= \cfrac{1}{\frac{2(4+x)+1}{4+x}} \\ x &= \frac{4+x}{2x+9} \\ x(2x+9) &= 4+x \\ 2x^2 + 9x &= 4 + x \\ 2x^2 + 8x - 4 &= 0 \\ x^2 + 4x - 2 &= 0 \\ x &= \frac{-4 \pm \sqrt{16+8}}{2} \\ x &= -2 \pm \sqrt{6}. \end{align*}

Since x=0+12+(something positive)x = 0 + \frac{1}{2 + (\text{something positive})}, we know that 0<x<120 < x < \frac{1}{2}, so we must choose the positive root. Therefore,

[0;2,4‾]=6−2.[0;\overline{2,4}] = \sqrt{6} - 2.

Note that this is still a "simple fraction" in the sense that it is a real numerator over an integer denominator, namely 6−21\frac{\sqrt{6}-2}{1}. It is also a quadratic irrational (a root of x2+4x−2x^2+4x-2), exactly as the periodicity theorem predicts.

Example. Write [1;3,2,4‾][1;3,\overline{2,4}] as a simple fraction.
This time the periodic part does not start immediately. Set x=[2;4‾]x = [\overline{2;4}], i.e. the repeating tail on its own;

x=2+14+1x.x = 2 + \cfrac{1}{4 + \cfrac{1}{x}}.

Simplifying,

x=2+14x+1xx=2+x4x+1x(4x+1)=2(4x+1)+x4x2+x=8x+2+x4x2−8x−2=02x2−4x−1=0x=4±16+84x=1±62.\begin{align*} x &= 2 + \cfrac{1}{\frac{4x+1}{x}} \\ x &= 2 + \frac{x}{4x+1} \\ x(4x+1) &= 2(4x+1) + x \\ 4x^2 + x &= 8x + 2 + x \\ 4x^2 - 8x - 2 &= 0 \\ 2x^2 - 4x - 1 &= 0 \\ x &= \frac{4 \pm \sqrt{16+8}}{4} \\ x &= 1 \pm \frac{\sqrt{6}}{2}. \end{align*}

Here xx starts with the partial quotient 22, so x>2x > 2 and we take the positive root; x=1+62=2+62x = 1 + \frac{\sqrt{6}}{2} = \frac{2+\sqrt{6}}{2}.
Now we resolve the non-periodic entries by evaluating the rest of the chain, rationalising denominators as we go:

[1;3,2,4‾]=1+13+1x=1+13+22+6=1+13+2(6−2)(6+2)(6−2)=1+13+2(6−2)2=1+13+6−2=1+11+6=1+6−1(6+1)(6−1)=1+6−15=4+65.\begin{align*} [1;3,\overline{2,4}] &= 1 + \cfrac{1}{3 + \cfrac{1}{x}} \\ &= 1 + \cfrac{1}{3 + \frac{2}{2+\sqrt{6}}} \\ &= 1 + \cfrac{1}{3 + \frac{2(\sqrt{6}-2)}{(\sqrt{6}+2)(\sqrt{6}-2)}} \\ &= 1 + \cfrac{1}{3 + \frac{2(\sqrt{6}-2)}{2}} \\ &= 1 + \cfrac{1}{3 + \sqrt{6} - 2} \\ &= 1 + \frac{1}{1+\sqrt{6}} \\ &= 1 + \frac{\sqrt{6}-1}{(\sqrt{6}+1)(\sqrt{6}-1)} \\ &= 1 + \frac{\sqrt{6}-1}{5} \\ &= \frac{4+\sqrt{6}}{5}. \end{align*}

Therefore, [1;3,2,4‾]=4+65≈1.28990[1;3,\overline{2,4}] = \frac{4+\sqrt{6}}{5} \approx 1.28990; again a quadratic irrational.

The Continued Fraction Algorithm#

To go the other way — converting any real number α\alpha into its continued fraction — we formalise the "peel off the integer part, then flip the leftover" idea. First find the integer part q0=⌊α⌋q_0 = \lfloor \alpha \rfloor and the fractional part r0={α}=α−⌊α⌋r_0 = \{\alpha\} = \alpha - \lfloor \alpha \rfloor, then iteratively do the same to the reciprocal of each fractional part:

α=q0+r0where q0=⌊α⌋ and r0={α},1r0=q1+r1where q1=⌊1r0⌋ and r1={1r0},1r1=q2+r2where q2=⌊1r1⌋ and r2={1r1},    ⋮    ⋮1rn−1=qn+rnwhere qn=⌊1rn−1⌋ and rn={1rn−1}.\begin{aligned} \alpha &= q_0 + r_0 && \text{where } q_0 = \lfloor \alpha \rfloor \text{ and } r_0 = \{\alpha\}, \\ \frac{1}{r_0} &= q_1 + r_1 && \text{where } q_1 = \left\lfloor \frac{1}{r_0} \right\rfloor \text{ and } r_1 = \left\{ \frac{1}{r_0} \right\}, \\ \frac{1}{r_1} &= q_2 + r_2 && \text{where } q_2 = \left\lfloor \frac{1}{r_1} \right\rfloor \text{ and } r_2 = \left\{ \frac{1}{r_1} \right\}, \\ &\;\;\vdots && \;\;\vdots \\ \frac{1}{r_{n-1}} &= q_n + r_n && \text{where } q_n = \left\lfloor \frac{1}{r_{n-1}} \right\rfloor \text{ and } r_n = \left\{ \frac{1}{r_{n-1}} \right\}. \end{aligned}

The stopping conditions match the characterisation theorems:

  • If α\alpha is rational, the process terminates at the nnth step when rn=0r_n = 0, giving the terminating expansion α=[q0;q1,q2,…,qn]\alpha = [q_0; q_1, q_2, \dots, q_n].
  • If α\alpha is a quadratic irrational, the process stops at the nnth step when the fractional part rnr_n has appeared earlier in the sequence, say rn=rkr_n = r_k for some k<nk < n; from that point everything must repeat, giving the periodic expansion α=[q0;q1,…,qk,qk+1,…,qn‾]\alpha = [q_0; q_1, \dots, q_k, \overline{q_{k+1}, \dots, q_n}].

Basically, the rir_i values are the "state" of the algorithm; if the state hits 00 you are done, and if the state ever repeats then so will everything after it.

Rational Inputs#

Example. Write 151115\frac{151}{115} as a continued fraction.

151115=1+36115where q0=1 and r0=36115,1r0=11536=3+736where q1=3 and r1=736,1r1=367=5+17where q2=5 and r2=17,1r2=71=7+0where q3=7 and r3=0.\begin{aligned} \frac{151}{115} &= 1 + \frac{36}{115} && \text{where } q_0 = 1 \text{ and } r_0 = \tfrac{36}{115}, \\ \frac{1}{r_0} = \frac{115}{36} &= 3 + \frac{7}{36} && \text{where } q_1 = 3 \text{ and } r_1 = \tfrac{7}{36}, \\ \frac{1}{r_1} = \frac{36}{7} &= 5 + \frac{1}{7} && \text{where } q_2 = 5 \text{ and } r_2 = \tfrac{1}{7}, \\ \frac{1}{r_2} = \frac{7}{1} &= 7 + 0 && \text{where } q_3 = 7 \text{ and } r_3 = 0. \end{aligned}

Since r3=0r_3 = 0, the algorithm terminates. Therefore,

151115=[1;3,5,7].\frac{151}{115} = [1;3,5,7].

Notice that the partial quotients 1,3,5,71, 3, 5, 7 are precisely the quotients from the Euclidean algorithm applied to 151151 and 115115;

151=1×115+36,115=3×36+7,36=5×7+1,7=7×1+0.\begin{align*} 151 &= 1 \times 115 + 36, \\ 115 &= 3 \times 36 + 7, \\ 36 &= 5 \times 7 + 1, \\ 7 &= 7 \times 1 + 0. \end{align*}

So for rational numbers you never actually need the fractional-part bookkeeping; just run the Euclidean algorithm on the numerator and denominator and read off the quotients.

Example. Write −3711-\frac{37}{11} as a continued fraction.
Here −3711≈−3.36-\frac{37}{11} \approx -3.36, and the floor function always rounds down, so for a negative number q0q_0 becomes more negative — q0=−4q_0 = -4, not −3-3. This keeps the fractional part positive, which is exactly what the definition requires.

−3711=−4+711where q0=−4 and r0=711,1r0=117=1+47where q1=1 and r1=47,1r1=74=1+34where q2=1 and r2=34,1r2=43=1+13where q3=1 and r3=13,1r3=31=3+0where q4=3 and r4=0.\begin{aligned} -\frac{37}{11} &= -4 + \frac{7}{11} && \text{where } q_0 = -4 \text{ and } r_0 = \tfrac{7}{11}, \\ \frac{1}{r_0} = \frac{11}{7} &= 1 + \frac{4}{7} && \text{where } q_1 = 1 \text{ and } r_1 = \tfrac{4}{7}, \\ \frac{1}{r_1} = \frac{7}{4} &= 1 + \frac{3}{4} && \text{where } q_2 = 1 \text{ and } r_2 = \tfrac{3}{4}, \\ \frac{1}{r_2} = \frac{4}{3} &= 1 + \frac{1}{3} && \text{where } q_3 = 1 \text{ and } r_3 = \tfrac{1}{3}, \\ \frac{1}{r_3} = \frac{3}{1} &= 3 + 0 && \text{where } q_4 = 3 \text{ and } r_4 = 0. \end{aligned}

Therefore, −3711=[−4;1,1,1,3]-\frac{37}{11} = [-4;1,1,1,3]. Only the leading entry carries the minus sign; all the later partial quotients are still positive.

Irrational Inputs#

For irrational inputs we run the exact same algorithm, but we must work with exact surds and rationalise denominators at every step. Never round the surds to decimals midway through the algorithm; rounding errors quickly corrupt the later partial quotients. Decimal estimates are only used on the side, to decide what each floor is.

Example. Find the continued fraction expansion for 7\sqrt{7}.
Since 4<7<94 < 7 < 9, we have 2<7<32 < \sqrt{7} < 3, so q0=2q_0 = 2. From there, each step is: flip the fractional part, rationalise, and take the floor.

7=2+(7−2)q0=2, r0=7−2,1r0=17−2=7+23=1+7−13q1=1, r1=7−13,1r1=37−1=7+12=1+7−12q2=1, r2=7−12,1r2=27−1=7+13=1+7−23q3=1, r3=7−23,1r3=37−2=7+2=4+(7−2)q4=4, r4=7−2.\begin{aligned} \sqrt{7} &= 2 + \left(\sqrt{7}-2\right) && q_0 = 2,\ r_0 = \sqrt{7}-2, \\ \frac{1}{r_0} = \frac{1}{\sqrt{7}-2} = \frac{\sqrt{7}+2}{3} &= 1 + \frac{\sqrt{7}-1}{3} && q_1 = 1,\ r_1 = \frac{\sqrt{7}-1}{3}, \\ \frac{1}{r_1} = \frac{3}{\sqrt{7}-1} = \frac{\sqrt{7}+1}{2} &= 1 + \frac{\sqrt{7}-1}{2} && q_2 = 1,\ r_2 = \frac{\sqrt{7}-1}{2}, \\ \frac{1}{r_2} = \frac{2}{\sqrt{7}-1} = \frac{\sqrt{7}+1}{3} &= 1 + \frac{\sqrt{7}-2}{3} && q_3 = 1,\ r_3 = \frac{\sqrt{7}-2}{3}, \\ \frac{1}{r_3} = \frac{3}{\sqrt{7}-2} = \sqrt{7}+2 &= 4 + \left(\sqrt{7}-2\right) && q_4 = 4,\ r_4 = \sqrt{7}-2. \end{aligned}

For instance, in the second line, 17−2=7+2(7−2)(7+2)=7+23\frac{1}{\sqrt{7}-2} = \frac{\sqrt{7}+2}{(\sqrt{7}-2)(\sqrt{7}+2)} = \frac{\sqrt{7}+2}{3}, and since 2<7<32 < \sqrt{7} < 3 this lies between 43\frac{4}{3} and 53\frac{5}{3}, so its floor is 11; the other floors are decided the same way. Now r4=7−2=r0r_4 = \sqrt{7}-2 = r_0, so the state has repeated and everything from q1q_1 onwards cycles. Therefore,

7=[2;1,1,1,4‾].\sqrt{7} = [2;\overline{1,1,1,4}].

A periodic expansion, as expected, since 7\sqrt{7} is a quadratic irrational.

Example. Find the continued fraction expansion for 2\sqrt{2}.
Since 1<2<41 < 2 < 4, we have 1<2<21 < \sqrt{2} < 2, so q0=1q_0 = 1.

2=1+(2−1)q0=1, r0=2−1,1r0=12−1=2+1=2+(2−1)q1=2, r1=2−1.\begin{aligned} \sqrt{2} &= 1 + \left(\sqrt{2}-1\right) && q_0 = 1,\ r_0 = \sqrt{2}-1, \\ \frac{1}{r_0} = \frac{1}{\sqrt{2}-1} = \sqrt{2}+1 &= 2 + \left(\sqrt{2}-1\right) && q_1 = 2,\ r_1 = \sqrt{2}-1. \end{aligned}

Already r1=r0r_1 = r_0, so the expansion repeats immediately;

2=[1;2‾]=[1;2,2,2,… ].\boxed{\sqrt{2} = [1;\overline{2}] = [1;2,2,2,\dots].}

This is the shortest possible period, and it is worth memorising as the classic example of a periodic continued fraction.

Convergents#

Note

Definition
Given any real number α\alpha with continued fraction expansion [q0;q1,q2,q3,… ][q_0; q_1, q_2, q_3, \dots], we define the nnth convergent of α\alpha to be

cn=[q0;q1,q2,…,qn].c_n = [q_0; q_1, q_2, \dots, q_n].

Basically, a convergent is what you get by chopping off the expansion after the nnth partial quotient and evaluating what remains; c0c_0 is just the integer part, and each further convergent uses one more entry, giving better and better rational snapshots of α\alpha.

Example. Find the convergents of 151115=[1;3,5,7]\frac{151}{115} = [1;3,5,7].

c0=[1]=1,c1=[1;3]=1+13=43,c2=[1;3,5]=1+13+15=1+516=2116,c3=[1;3,5,7]=151115.\begin{align*} c_0 &= [1] = 1, \\ c_1 &= [1;3] = 1 + \frac{1}{3} = \frac{4}{3}, \\ c_2 &= [1;3,5] = 1 + \cfrac{1}{3 + \cfrac{1}{5}} = 1 + \frac{5}{16} = \frac{21}{16}, \\ c_3 &= [1;3,5,7] = \frac{151}{115}. \end{align*}

The last convergent of a rational number is just the number itself, so there is nothing to compute there.

Example. Find the first four convergents of 7=[2;1,1,1,4‾]\sqrt{7} = [2;\overline{1,1,1,4}].

c0=[2]=2,c1=[2;1]=2+11=3,c2=[2;1,1]=2+11+11=2+12=52,c3=[2;1,1,1]=2+11+11+11=2+23=83.\begin{align*} c_0 &= [2] = 2, \\ c_1 &= [2;1] = 2 + \frac{1}{1} = 3, \\ c_2 &= [2;1,1] = 2 + \cfrac{1}{1 + \cfrac{1}{1}} = 2 + \frac{1}{2} = \frac{5}{2}, \\ c_3 &= [2;1,1,1] = 2 + \cfrac{1}{1 + \cfrac{1}{1 + \cfrac{1}{1}}} = 2 + \frac{2}{3} = \frac{8}{3}. \end{align*}

Unlike the rational case, this list never ends; we can keep taking convergents forever.

Convergents from the EEA#

Evaluating each convergent from scratch gets painful quickly; every new convergent means re-evaluating a whole nested fraction. The fix is that the convergents of any real number α\alpha appear amongst the entries of its extended Euclidean algorithm table. Since α\alpha might not be rational, we can no longer record the remainder row rir_i, but we keep the qiq_i row (which now holds the partial quotients) and generate the xix_i and yiy_i rows exactly as before, seeded by the columns (x,y)=(1,0)(x,y) = (1,0) and (0,1)(0,1):

xi=xi−2−qi xi−1,yi=yi−2−qi yi−1.x_i = x_{i-2} - q_i \, x_{i-1}, \qquad y_i = y_{i-2} - q_i \, y_{i-1}.

Having done so, the nnth convergent is given by

cn=−ynxn,\boxed{c_n = -\frac{y_n}{x_n},}

where the 00th convergent c0c_0 comes from the column containing the partial quotient q0q_0, and so on.

Example. Find the convergents of 151115=[1;3,5,7]\frac{151}{115} = [1;3,5,7] using the EEA.

qiq_i 11 33 55 77
xix_i 11 00 11 −3-3 1616 −115-115
yiy_i 00 11 −1-1 44 −21-21 151151
cic_i 11 43\frac{4}{3} 2116\frac{21}{16} 151115\frac{151}{115}

For instance, x2=x0−q2x1=1−5(−3)=16x_2 = x_0 - q_2 x_1 = 1 - 5(-3) = 16 and y2=y0−q2y1=−1−5(4)=−21y_2 = y_0 - q_2 y_1 = -1 - 5(4) = -21, so c2=−−2116=2116c_2 = -\frac{-21}{16} = \frac{21}{16}; these match the convergents we found by hand. Notice how the signs of xix_i and yiy_i alternate down the rows, and the final column recovers ±(151,115)\pm(151, 115), i.e. the number itself.

Example. Find the first seven convergents of 7=[2;1,1,1,4‾]\sqrt{7} = [2;\overline{1,1,1,4}].
The partial quotient row just keeps cycling through 1,1,1,41,1,1,4 after the initial 22:

qiq_i 22 11 11 11 44 11 11 11
xix_i 11 00 11 −1-1 22 −3-3 1414 −17-17 3131 −48-48
yiy_i 00 11 −2-2 33 −5-5 88 −37-37 4545 −82-82 127127
cic_i 22 33 52\frac{5}{2} 83\frac{8}{3} 3714\frac{37}{14} 4517\frac{45}{17} 8231\frac{82}{31} 12748\frac{127}{48}

So c7=12748c_7 = \frac{127}{48}, and the whole list took a fraction of the effort of evaluating eight nested fractions.

This gives us a fairly efficient procedure for finding convergents of any real number α\alpha:

  • Find the first few (or all) entries in the continued fraction expansion of α\alpha.
  • Use these entries as the partial quotient row in a modified EEA table, and fill out the xix_i and yiy_i rows.
  • Read off the nnth convergent as cn=−ynxnc_n = -\frac{y_n}{x_n}.

As a rule of thumb: if you only need one small convergent, bottom-up evaluation is fine; if you need several convergents (or high-index ones), always build the table. Also, if you ignore the alternating signs, the numerators and denominators of the convergents each satisfy the recurrence an=qnan−1+an−2a_n = q_n a_{n-1} + a_{n-2}; you can see this in the table above, e.g. the denominators satisfy 14=4×3+214 = 4 \times 3 + 2 and 48=1×31+1748 = 1 \times 31 + 17.

Convergents as Approximations#

As implied by their name, the convergents of a real number α\alpha are increasingly good approximations of α\alpha. For 7≈2.64575\sqrt{7} \approx 2.64575, the convergents from the table above evaluate to

c0=2,c1=3,c2=52=2.5,c3=83≈2.66667,c4=3714≈2.64286,c5=4517≈2.64706,c6=8231≈2.64516,c7=12748≈2.64583.\begin{align*} c_0 &= 2, \\ c_1 &= 3, \\ c_2 &= \tfrac{5}{2} = 2.5, \\ c_3 &= \tfrac{8}{3} \approx 2.66667, \\ c_4 &= \tfrac{37}{14} \approx 2.64286, \\ c_5 &= \tfrac{45}{17} \approx 2.64706, \\ c_6 &= \tfrac{82}{31} \approx 2.64516, \\ c_7 &= \tfrac{127}{48} \approx 2.64583. \end{align*}

Notice how the convergents alternate around the target; the even-indexed ones sit below 7\sqrt{7} and the odd-indexed ones sit above it,

c0<c2<c4<c6<7<c7<c5<c3<c1.c_0 < c_2 < c_4 < c_6 < \sqrt{7} < c_7 < c_5 < c_3 < c_1.

This is not a coincidence. Truncating the expansion throws away a positive tail, and that tail sits underneath some number of reciprocals; each extra reciprocal flips the direction of the error. So c0c_0 (a floor) always undershoots, c1c_1 overshoots, and so on, meaning α\alpha is always squeezed between any two consecutive convergents. This is a useful sanity check in exams: if your convergents do not alternate above and below the target, you have made an arithmetic mistake somewhere.

How good are these approximations? There is a precise answer.

Note

Fact
For any real number α\alpha, its nnth convergent cnc_n is the best rational approximation for α\alpha with respect to its denominator, in the precise sense that

∣α−cn∣=∣α+ynxn∣<1qn+1 xn2,|\alpha - c_n| = \left|\alpha + \frac{y_n}{x_n}\right| < \frac{1}{q_{n+1} \, x_n^2},

where xnx_n is the denominator of cnc_n, and qn+1q_{n+1} is the partial quotient in α\alpha immediately following the last partial quotient in cnc_n.

Two things to take away from this bound. First, the error shrinks like 1xn2\frac{1}{x_n^2} — the square of the denominator — which is far better than truncating a decimal expansion (a decimal cut after denominator 10k10^k only guarantees an error of about 110k\frac{1}{10^k}). Second, the qn+1q_{n+1} out the front means that stopping just before a huge partial quotient gives an unusually good approximation; basically, a big entry says the tail contributes almost nothing, so the truncation was nearly free.

Example. Find the error bound for the 77th convergent c7c_7 of 7\sqrt{7}.
From the table, c7=12748c_7 = \frac{127}{48}, so x7=48x_7 = 48. The expansion is [2;1,1,1,4‾][2;\overline{1,1,1,4}], so the partial quotient after q7q_7 is q8=4q_8 = 4. Therefore,

∣7−12748∣<1q8 x72=14×482=19216.\begin{align*} \left|\sqrt{7} - \frac{127}{48}\right| &< \frac{1}{q_8 \, x_7^2} \\ &= \frac{1}{4 \times 48^2} \\ &= \frac{1}{9216}. \end{align*}

Therefore, c7c_7 is guaranteed to be within 19216≈0.000109\frac{1}{9216} \approx 0.000109 of 7\sqrt{7}; the actual error is ∣2.6457513…−2.64583‾∣≈0.000082\left|2.6457513\ldots - 2.6458\overline{3}\right| \approx 0.000082, which indeed fits inside the bound.

Example. Find the first five convergents of 2=[1;2‾]\sqrt{2} = [1;\overline{2}] and verify the error bound for c3c_3.
Since we want several convergents, we build the EEA table:

qiq_i 11 22 22 22 22
xix_i 11 00 11 −2-2 55 −12-12 2929
yiy_i 00 11 −1-1 33 −7-7 1717 −41-41
cic_i 11 32\frac{3}{2} 75\frac{7}{5} 1712\frac{17}{12} 4129\frac{41}{29}

For c3=1712c_3 = \frac{17}{12} we have x3=12x_3 = 12 and q4=2q_4 = 2, so

∣2−1712∣<1q4 x32=12×122=1288.\begin{align*} \left|\sqrt{2} - \frac{17}{12}\right| &< \frac{1}{q_4 \, x_3^2} \\ &= \frac{1}{2 \times 12^2} \\ &= \frac{1}{288}. \end{align*}

The actual error is ∣1.414214…−1.416‾∣≈0.00245|1.414214\ldots - 1.41\overline{6}| \approx 0.00245, comfortably inside the bound 1288≈0.00347\frac{1}{288} \approx 0.00347. Also notice the alternation again: 1<75<4129<2<1712<321 < \frac{7}{5} < \frac{41}{29} < \sqrt{2} < \frac{17}{12} < \frac{3}{2}.

Famous Approximations of Pi#

Example. Find the first few convergents of π\pi.
π\pi is not a quadratic irrational, so its expansion never terminates or repeats and we must compute it numerically (with plenty of decimal places in reserve). Working with π≈3.14159265359\pi \approx 3.14159265359:

π=3+0.14159265…q0=3,1r0≈7.06251331q1=7, r1≈0.06251331,1r1≈15.99659441q2=15, r2≈0.99659441,1r2≈1.00341723q3=1, r3≈0.00341723,1r3≈292.63459q4=292.\begin{aligned} \pi &= 3 + 0.14159265\ldots && q_0 = 3, \\ \frac{1}{r_0} &\approx 7.06251331 && q_1 = 7,\ r_1 \approx 0.06251331, \\ \frac{1}{r_1} &\approx 15.99659441 && q_2 = 15,\ r_2 \approx 0.99659441, \\ \frac{1}{r_2} &\approx 1.00341723 && q_3 = 1,\ r_3 \approx 0.00341723, \\ \frac{1}{r_3} &\approx 292.63459 && q_4 = 292. \end{aligned}

So π=[3;7,15,1,292,… ]\pi = [3;7,15,1,292,\dots], and the convergents are

c0=3,c1=[3;7]=3+17=227,c2=[3;7,15]=3+17+115=3+15106=333106,c3=[3;7,15,1]=3+17+116=3+16113=355113.\begin{align*} c_0 &= 3, \\ c_1 &= [3;7] = 3 + \frac{1}{7} = \frac{22}{7}, \\ c_2 &= [3;7,15] = 3 + \cfrac{1}{7 + \cfrac{1}{15}} = 3 + \frac{15}{106} = \frac{333}{106}, \\ c_3 &= [3;7,15,1] = 3 + \cfrac{1}{7 + \cfrac{1}{16}} = 3 + \frac{16}{113} = \frac{355}{113}. \end{align*}

The convergent c1=227c_1 = \frac{22}{7} is the primary-school approximation of π\pi, with error bound

∣π−227∣<1q2 x12=115×72=1735.\left|\pi - \frac{22}{7}\right| < \frac{1}{q_2 \, x_1^2} = \frac{1}{15 \times 7^2} = \frac{1}{735}.

The convergent c3=355113c_3 = \frac{355}{113} is spectacular for its size, precisely because the next partial quotient is the enormous q4=292q_4 = 292;

∣π−355113∣<1292×1132=13728548.\left|\pi - \frac{355}{113}\right| < \frac{1}{292 \times 113^2} = \frac{1}{3728548}.

Indeed, 355113≈3.14159292\frac{355}{113} \approx 3.14159292 while π≈3.14159265\pi \approx 3.14159265; seven correct significant figures from a three-digit denominator. Both 227\frac{22}{7} and 355113\frac{355}{113} were known in ancient times, and no fraction with a smaller denominator beats 355113\frac{355}{113}.

An Application: Designing a Calendar#

Example. A tropical year is approximately 365.24219365.24219 days. Use continued fractions to decide how many leap years a calendar should have.
The whole problem is about approximating the fractional part 0.242190.24219 by a fraction leap yearscycle length\frac{\text{leap years}}{\text{cycle length}} with a manageable denominator, which is exactly what convergents are best at. Running the algorithm numerically,

0.24219=0+0.24219q0=0,1r0≈4.12899q1=4, r1≈0.12899,1r1≈7.75256q2=7, r2≈0.75256,1r2≈1.32880q3=1, r3≈0.32880,1r3≈3.04140q4=3.\begin{aligned} 0.24219 &= 0 + 0.24219 && q_0 = 0, \\ \frac{1}{r_0} &\approx 4.12899 && q_1 = 4,\ r_1 \approx 0.12899, \\ \frac{1}{r_1} &\approx 7.75256 && q_2 = 7,\ r_2 \approx 0.75256, \\ \frac{1}{r_2} &\approx 1.32880 && q_3 = 1,\ r_3 \approx 0.32880, \\ \frac{1}{r_3} &\approx 3.04140 && q_4 = 3. \end{aligned}

So 0.24219≈[0;4,7,1,3,… ]0.24219 \approx [0;4,7,1,3,\dots], with convergents

c1=[0;4]=14=0.25,c2=[0;4,7]=14+17=729≈0.24138,c3=[0;4,7,1]=14+18=833≈0.24242,c4=[0;4,7,1,3]=14+431=31128≈0.24219.\begin{align*} c_1 &= [0;4] = \frac{1}{4} = 0.25, \\ c_2 &= [0;4,7] = \cfrac{1}{4 + \cfrac{1}{7}} = \frac{7}{29} \approx 0.24138, \\ c_3 &= [0;4,7,1] = \cfrac{1}{4 + \cfrac{1}{8}} = \frac{8}{33} \approx 0.24242, \\ c_4 &= [0;4,7,1,3] = \cfrac{1}{4 + \cfrac{4}{31}} = \frac{31}{128} \approx 0.24219. \end{align*}

The convergent c1=14c_1 = \frac{1}{4} is the Julian calendar: one leap year every 44 years, which drifts by a full day roughly every 128128 years. The convergent c4=31128c_4 = \frac{31}{128} says a calendar with 3131 leap years every 128128 years would drift by only a day every 400,000400{,}000 years or so. Interestingly, the Gregorian calendar we actually use (9797 leap years every 400400 years, i.e. 0.24250.2425) is not a convergent, and drifts a day every ≈3200\approx 3200 years; the continued fraction answer 31128\frac{31}{128} is both more accurate and uses a smaller denominator. The lesson: when you need the best fraction subject to a denominator budget, compute convergents rather than guessing.

The Golden Ratio#

The golden ratio is the irrational number ϕ=12(1+5)≈1.61803\phi = \frac{1}{2}\left(1+\sqrt{5}\right) \approx 1.61803.
(a) Find the continued fraction expansion and the first few convergents of ϕ\phi.
(b) Find a general formula for the nnth convergent of ϕ\phi.
(c) What is the expected error bound for the nnth convergent of ϕ\phi?

(a). We run the continued fraction algorithm. Since 2<5<32 < \sqrt{5} < 3, we have 32<ϕ<2\frac{3}{2} < \phi < 2, so q0=1q_0 = 1.

ϕ=1+5−12q0=1, r0=5−12,1r0=25−1=2(5+1)4=5+12=1+5−12q1=1, r1=5−12.\begin{aligned} \phi &= 1 + \frac{\sqrt{5}-1}{2} && q_0 = 1,\ r_0 = \frac{\sqrt{5}-1}{2}, \\ \frac{1}{r_0} = \frac{2}{\sqrt{5}-1} = \frac{2\left(\sqrt{5}+1\right)}{4} = \frac{\sqrt{5}+1}{2} &= 1 + \frac{\sqrt{5}-1}{2} && q_1 = 1,\ r_1 = \frac{\sqrt{5}-1}{2}. \end{aligned}

Already r1=r0r_1 = r_0, so the expansion repeats immediately;

ϕ=[1;1‾]=[1;1,1,1,… ].\boxed{\phi = [1;\overline{1}] = [1;1,1,1,\dots].}

There is also a slicker way to see this: ϕ\phi satisfies ϕ2=ϕ+1\phi^2 = \phi + 1, so dividing by ϕ\phi gives ϕ=1+1ϕ\phi = 1 + \frac{1}{\phi}, and substituting this identity into itself over and over spins out the all-ones expansion directly.
The first few convergents come from the EEA table with every partial quotient equal to 11:

qiq_i 11 11 11 11 11 11
xix_i 11 00 11 −1-1 22 −3-3 55 −8-8
yiy_i 00 11 −1-1 22 −3-3 55 −8-8 1313
cic_i 11 22 32\frac{3}{2} 53\frac{5}{3} 85\frac{8}{5} 138\frac{13}{8}

So the convergents are 1,2,32,53,85,138,…1, 2, \frac{3}{2}, \frac{5}{3}, \frac{8}{5}, \frac{13}{8}, \dots — and both the numerators and denominators are the Fibonacci numbers 1,1,2,3,5,8,13,…1, 1, 2, 3, 5, 8, 13, \dots appearing in the table (up to sign). This is no surprise: with every qi=1q_i = 1, the recurrence an=qnan−1+an−2a_n = q_n a_{n-1} + a_{n-2} becomes an=an−1+an−2a_n = a_{n-1} + a_{n-2}, which is exactly the Fibonacci recurrence.

(b). The pattern above suggests that the nnth convergent is a ratio of consecutive Fibonacci numbers,

cn=Fn+2Fn+1,\boxed{c_n = \frac{F_{n+2}}{F_{n+1}},}

where F1=F2=1F_1 = F_2 = 1 and Fn=Fn−1+Fn−2F_{n} = F_{n-1} + F_{n-2}.
Proof. Because the expansion is all ones, chopping it after n+1n+1 entries gives cn=1+1cn−1c_n = 1 + \frac{1}{c_{n-1}} (the tail of a truncated all-ones expansion is a shorter all-ones expansion). We induct on nn. For the base case, c0=1=F2F1c_0 = 1 = \frac{F_2}{F_1}. Now suppose cn−1=Fn+1Fnc_{n-1} = \frac{F_{n+1}}{F_n}; then

cn=1+1cn−1=1+FnFn+1=Fn+1+FnFn+1=Fn+2Fn+1.■\begin{align*} c_n &= 1 + \frac{1}{c_{n-1}} \\ &= 1 + \frac{F_n}{F_{n+1}} \\ &= \frac{F_{n+1} + F_n}{F_{n+1}} \\ &= \frac{F_{n+2}}{F_{n+1}}. \qquad \blacksquare \end{align*}

As a bonus, since convergents approach the number they expand, this proves the famous fact that the ratio of consecutive Fibonacci numbers tends to ϕ\phi.

(c). By the error bound fact, with every partial quotient qn+1=1q_{n+1} = 1 and denominator xn=Fn+1x_n = F_{n+1}, the expected error bound for the nnth convergent is

∣ϕ−cn∣<1qn+1 xn2=1Fn+12.\boxed{|\phi - c_n| < \frac{1}{q_{n+1} \, x_n^2} = \frac{1}{F_{n+1}^2}.}

For instance, for n=5n = 5 we have c5=138c_5 = \frac{13}{8} and F6=8F_6 = 8, so

∣ϕ−138∣<11×82=164≈0.01563,\begin{align*} \left|\phi - \frac{13}{8}\right| &< \frac{1}{1 \times 8^2} \\ &= \frac{1}{64} \\ &\approx 0.01563, \end{align*}

and the actual error is ∣1.61803−1.625∣≈0.00697|1.61803 - 1.625| \approx 0.00697, which fits the bound.
Notice that qn+1=1q_{n+1} = 1 is the smallest value a partial quotient can take, so this is the weakest error bound a continued fraction can produce; the convergents of ϕ\phi close in on their target as slowly as convergents possibly can. Contrast this with π\pi, where the huge q4=292q_4 = 292 made 355113\frac{355}{113} absurdly accurate. In this precise sense, ϕ\phi is the number that resists rational approximation the most — which is why it is sometimes called the "most irrational" number.