Notice how the fraction on the left of each line is the reciprocal of the "remainder" fraction on the line before it; 117286 is 286117 flipped upside down, and so on. This means we can keep substituting each equation into the one above it:
286403=1+1172861=1+2+2+4111.
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,…] and interpreted as
q0+q1+q2+q3+⋯111,
where q0∈Z and qi∈Z+ for all i>0. The entries in the expression may continue forever or terminate.
Basically, we peel off the integer part q0 of our number, leaving something between 0 and 1; flipping that leftover upside down gives a number bigger than 1, so we can peel off its integer part q1, and repeat forever (or until nothing is left over). The entries qi are called the partial quotients. Using this notation, the working above becomes
286403=[1;2,2,4].
It is important to remember that only q0 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] for some natural number n. To ensure uniqueness, we require that qn=1 if n>0.
Note
Definition
A periodic (or eventually periodic) continued fraction is a continued fraction of the form [q0;q1,q2,…,qk,qk+1,…,qn] for some positive integers k≤n. The line over the periodic part indicates that it repeats indefinitely, so
The qn=1 rule might look arbitrary, but without it every terminating continued fraction would have two different forms, since a final entry of 1 can always be absorbed into the entry before it.
Example. Show that [2;3,1] and [2;4] represent the same number.
[2;3,1]=2+3+111=2+41=49,
and clearly [2;4]=2+41=49 as well. Therefore the two expansions agree, and the convention qn=1 tells us that [2;4] is the correct (canonical) way to write 49.
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 b expansion can.
Note
Theorem Every real number α∈R has a unique continued fraction representation.
Compare this with base representations, where some numbers get two expansions (recall 0.9=1 in base 10). For continued fractions, the qn=1 convention removes the only source of ambiguity, so every real number gets exactly one expansion.
Note
Theorem A number α∈R has a terminating continued fraction expansion if and only if α 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 has a periodic continued fraction expansion if and only if α is a quadratic irrational — that is, the root of a quadratic polynomial with rational coefficients.
Quadratic irrationals are numbers of the form a+bd with a,b rational and d a non-square positive integer; things like 2, 6−2 or 21+5. Notice the pattern shift compared to base expansions:
In base b: terminating or periodic ⟺ rational.
Continued fractions: terminating ⟺ rational, and periodic ⟺ quadratic irrational.
So continued fractions climb one level higher; the "repeating" behaviour now captures a whole class of irrational numbers. A number like π, 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 → number, and number → continued fraction) are well-defined in both directions, which is what justifies these theorems.
Converting Continued Fractions to Simple 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.
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 x to be the periodic part of the continued fraction.
Expand out the continued fraction representation of x until another instance of x appears further down the fractional chain.
Replace that new instance with the variable x again.
Simplify the equation to produce a quadratic equation in x.
Solve the quadratic (it will have up to two solutions) and choose the solution that matches x (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] as a simple fraction.
Let x=[0;2,4]. Expanding the chain, the part after the 4 is another copy of x;
Since x=0+2+(something positive)1, we know that 0<x<21, so we must choose the positive root. Therefore,
[0;2,4]=6−2.
Note that this is still a "simple fraction" in the sense that it is a real numerator over an integer denominator, namely 16−2. It is also a quadratic irrational (a root of x2+4x−2), exactly as the periodicity theorem predicts.
Example. Write [1;3,2,4] as a simple fraction.
This time the periodic part does not start immediately. Set x=[2;4], i.e. the repeating tail on its own;
Here x starts with the partial quotient 2, so x>2 and we take the positive root; x=1+26=22+6.
Now we resolve the non-periodic entries by evaluating the rest of the chain, rationalising denominators as we go:
To go the other way — converting any real number α into its continued fraction — we formalise the "peel off the integer part, then flip the leftover" idea. First find the integer part q0=⌊α⌋ and the fractional part r0={α}=α−⌊α⌋, then iteratively do the same to the reciprocal of each fractional part:
αr01r11rn−11=q0+r0=q1+r1=q2+r2⋮=qn+rnwhere q0=⌊α⌋ and r0={α},where q1=⌊r01⌋ and r1={r01},where q2=⌊r11⌋ and r2={r11},⋮where qn=⌊rn−11⌋ and rn={rn−11}.
The stopping conditions match the characterisation theorems:
If α is rational, the process terminates at the nth step when rn=0, giving the terminating expansion α=[q0;q1,q2,…,qn].
If α is a quadratic irrational, the process stops at the nth step when the fractional part rn has appeared earlier in the sequence, say rn=rk for some k<n; from that point everything must repeat, giving the periodic expansion α=[q0;q1,…,qk,qk+1,…,qn].
Basically, the ri values are the "state" of the algorithm; if the state hits 0 you are done, and if the state ever repeats then so will everything after it.
115151r01=36115r11=736r21=17=1+11536=3+367=5+71=7+0where q0=1 and r0=11536,where q1=3 and r1=367,where q2=5 and r2=71,where q3=7 and r3=0.
Since r3=0, the algorithm terminates. Therefore,
115151=[1;3,5,7].
Notice that the partial quotients 1,3,5,7 are precisely the quotients from the Euclidean algorithm applied to 151 and 115;
151115367=1×115+36,=3×36+7,=5×7+1,=7×1+0.
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 −1137 as a continued fraction.
Here −1137≈−3.36, and the floor function always rounds down, so for a negative number q0 becomes more negative — q0=−4, not −3. This keeps the fractional part positive, which is exactly what the definition requires.
−1137r01=711r11=47r21=34r31=13=−4+117=1+74=1+43=1+31=3+0where q0=−4 and r0=117,where q1=1 and r1=74,where q2=1 and r2=43,where q3=1 and r3=31,where q4=3 and r4=0.
Therefore, −1137=[−4;1,1,1,3]. Only the leading entry carries the minus sign; all the later partial quotients are still positive.
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.
Since 4<7<9, we have 2<7<3, so q0=2. From there, each step is: flip the fractional part, rationalise, and take the floor.
For instance, in the second line, 7−21=(7−2)(7+2)7+2=37+2, and since 2<7<3 this lies between 34 and 35, so its floor is 1; the other floors are decided the same way. Now r4=7−2=r0, so the state has repeated and everything from q1 onwards cycles. Therefore,
7=[2;1,1,1,4].
A periodic expansion, as expected, since 7 is a quadratic irrational.
Example. Find the continued fraction expansion for 2.
Since 1<2<4, we have 1<2<2, so q0=1.
Definition
Given any real number α with continued fraction expansion [q0;q1,q2,q3,…], we define the nth convergent of α to be
cn=[q0;q1,q2,…,qn].
Basically, a convergent is what you get by chopping off the expansion after the nth partial quotient and evaluating what remains; c0 is just the integer part, and each further convergent uses one more entry, giving better and better rational snapshots of α.
Example. Find the convergents of 115151=[1;3,5,7].
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 α appear amongst the entries of its extended Euclidean algorithm table. Since α might not be rational, we can no longer record the remainder row ri, but we keep the qi row (which now holds the partial quotients) and generate the xi and yi rows exactly as before, seeded by the columns (x,y)=(1,0) and (0,1):
xi=xi−2−qixi−1,yi=yi−2−qiyi−1.
Having done so, the nth convergent is given by
cn=−xnyn,
where the 0th convergent c0 comes from the column containing the partial quotient q0, and so on.
Example. Find the convergents of 115151=[1;3,5,7] using the EEA.
qi
1
3
5
7
xi
1
0
1
−3
16
−115
yi
0
1
−1
4
−21
151
ci
1
34
1621
115151
For instance, x2=x0−q2x1=1−5(−3)=16 and y2=y0−q2y1=−1−5(4)=−21, so c2=−16−21=1621; these match the convergents we found by hand. Notice how the signs of xi and yi alternate down the rows, and the final column recovers ±(151,115), i.e. the number itself.
Example. Find the first seven convergents of 7=[2;1,1,1,4].
The partial quotient row just keeps cycling through 1,1,1,4 after the initial 2:
qi
2
1
1
1
4
1
1
1
xi
1
0
1
−1
2
−3
14
−17
31
−48
yi
0
1
−2
3
−5
8
−37
45
−82
127
ci
2
3
25
38
1437
1745
3182
48127
So c7=48127, 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 α:
Find the first few (or all) entries in the continued fraction expansion of α.
Use these entries as the partial quotient row in a modified EEA table, and fill out the xi and yi rows.
Read off the nth convergent as cn=−xnyn.
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−2; you can see this in the table above, e.g. the denominators satisfy 14=4×3+2 and 48=1×31+17.
As implied by their name, the convergents of a real number α are increasingly good approximations of α. For 7≈2.64575, the convergents from the table above evaluate to
Notice how the convergents alternate around the target; the even-indexed ones sit below 7 and the odd-indexed ones sit above it,
c0<c2<c4<c6<7<c7<c5<c3<c1.
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 c0 (a floor) always undershoots, c1 overshoots, and so on, meaning α 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 α, its nth convergent cn is the best rational approximation for α with respect to its denominator, in the precise sense that
∣α−cn∣=α+xnyn<qn+1xn21,
where xn is the denominator of cn, and qn+1 is the partial quotient in α immediately following the last partial quotient in cn.
Two things to take away from this bound. First, the error shrinks like xn21 — the square of the denominator — which is far better than truncating a decimal expansion (a decimal cut after denominator 10k only guarantees an error of about 10k1). Second, the qn+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 7th convergent c7 of 7.
From the table, c7=48127, so x7=48. The expansion is [2;1,1,1,4], so the partial quotient after q7 is q8=4. Therefore,
7−48127<q8x721=4×4821=92161.
Therefore, c7 is guaranteed to be within 92161≈0.000109 of 7; the actual error is 2.6457513…−2.64583≈0.000082, which indeed fits inside the bound.
Example. Find the first five convergents of 2=[1;2] and verify the error bound for c3.
Since we want several convergents, we build the EEA table:
qi
1
2
2
2
2
xi
1
0
1
−2
5
−12
29
yi
0
1
−1
3
−7
17
−41
ci
1
23
57
1217
2941
For c3=1217 we have x3=12 and q4=2, so
2−1217<q4x321=2×1221=2881.
The actual error is ∣1.414214…−1.416∣≈0.00245, comfortably inside the bound 2881≈0.00347. Also notice the alternation again: 1<57<2941<2<1217<23.
Example. Find the first few convergents of π. π 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:
The convergent c1=722 is the primary-school approximation of π, with error bound
π−722<q2x121=15×721=7351.
The convergent c3=113355 is spectacular for its size, precisely because the next partial quotient is the enormous q4=292;
π−113355<292×11321=37285481.
Indeed, 113355≈3.14159292 while π≈3.14159265; seven correct significant figures from a three-digit denominator. Both 722 and 113355 were known in ancient times, and no fraction with a smaller denominator beats 113355.
Example. A tropical year is approximately 365.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.24219 by a fraction cycle lengthleap years with a manageable denominator, which is exactly what convergents are best at. Running the algorithm numerically,
The convergent c1=41 is the Julian calendar: one leap year every 4 years, which drifts by a full day roughly every 128 years. The convergent c4=12831 says a calendar with 31 leap years every 128 years would drift by only a day every 400,000 years or so. Interestingly, the Gregorian calendar we actually use (97 leap years every 400 years, i.e. 0.2425) is not a convergent, and drifts a day every ≈3200 years; the continued fraction answer 12831 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 is the irrational number ϕ=21(1+5)≈1.61803.
(a) Find the continued fraction expansion and the first few convergents of ϕ.
(b) Find a general formula for the nth convergent of ϕ.
(c) What is the expected error bound for the nth convergent of ϕ?
Already r1=r0, so the expansion repeats immediately;
ϕ=[1;1]=[1;1,1,1,…].
There is also a slicker way to see this: ϕ satisfies ϕ2=ϕ+1, so dividing by ϕ gives ϕ=1+ϕ1, 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 1:
qi
1
1
1
1
1
1
xi
1
0
1
−1
2
−3
5
−8
yi
0
1
−1
2
−3
5
−8
13
ci
1
2
23
35
58
813
So the convergents are 1,2,23,35,58,813,… — and both the numerators and denominators are the Fibonacci numbers 1,1,2,3,5,8,13,… appearing in the table (up to sign). This is no surprise: with every qi=1, the recurrence an=qnan−1+an−2 becomes an=an−1+an−2, which is exactly the Fibonacci recurrence.
(b). The pattern above suggests that the nth convergent is a ratio of consecutive Fibonacci numbers,
cn=Fn+1Fn+2,
where F1=F2=1 and Fn=Fn−1+Fn−2. Proof. Because the expansion is all ones, chopping it after n+1 entries gives cn=1+cn−11 (the tail of a truncated all-ones expansion is a shorter all-ones expansion). We induct on n. For the base case, c0=1=F1F2. Now suppose cn−1=FnFn+1; then
As a bonus, since convergents approach the number they expand, this proves the famous fact that the ratio of consecutive Fibonacci numbers tends to ϕ.
(c). By the error bound fact, with every partial quotient qn+1=1 and denominator xn=Fn+1, the expected error bound for the nth convergent is
∣ϕ−cn∣<qn+1xn21=Fn+121.
For instance, for n=5 we have c5=813 and F6=8, so
ϕ−813<1×821=641≈0.01563,
and the actual error is ∣1.61803−1.625∣≈0.00697, which fits the bound.
Notice that qn+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 ϕ close in on their target as slowly as convergents possibly can. Contrast this with π, where the huge q4=292 made 113355 absurdly accurate. In this precise sense, ϕ is the number that resists rational approximation the most — which is why it is sometimes called the "most irrational" number.