Continued Fractions
Introduction to Continued Fractions
Recall how we previously applied the Euclidean algorithm to the numbers and :
Suppose we divide each equation through by its original divisor;
Notice how the fraction on the left of each line is the reciprocal of the "remainder" fraction on the line before it; is flipped upside down, and so on. This means we can keep substituting each equation into the one above it:
This nested expression is called a continued fraction.
Note
Definition
A continued fraction is a representation for a real number written as and interpreted as
where and for all . The entries in the expression may continue forever or terminate.
Basically, we peel off the integer part of our number, leaving something between and ; flipping that leftover upside down gives a number bigger than , so we can peel off its integer part , and repeat forever (or until nothing is left over). The entries are called the partial quotients. Using this notation, the working above becomes
It is important to remember that only 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 for some natural number . To ensure uniqueness, we require that if .
Note
Definition
A periodic (or eventually periodic) continued fraction is a continued fraction of the form for some positive integers . The line over the periodic part indicates that it repeats indefinitely, so
The rule might look arbitrary, but without it every terminating continued fraction would have two different forms, since a final entry of can always be absorbed into the entry before it.
Example. Show that and represent the same number.
and clearly as well. Therefore the two expansions agree, and the convention tells us that is the correct (canonical) way to write .
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 expansion can.
Note
Theorem
Every real number has a unique continued fraction representation.
Compare this with base representations, where some numbers get two expansions (recall in base ). For continued fractions, the convention removes the only source of ambiguity, so every real number gets exactly one expansion.
Note
Theorem
A number 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 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 with rational and a non-square positive integer; things like , or . Notice the pattern shift compared to base expansions:
- In base : 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
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 as a simple fraction.
Therefore, ; 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 to be the periodic part of the continued fraction.
- Expand out the continued fraction representation of until another instance of appears further down the fractional chain.
- Replace that new instance with the variable again.
- Simplify the equation to produce a quadratic equation in .
- Solve the quadratic (it will have up to two solutions) and choose the solution that matches (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 as a simple fraction.
Let . Expanding the chain, the part after the is another copy of ;
Now we simplify to a quadratic:
Since , we know that , so we must choose the positive root. Therefore,
Note that this is still a "simple fraction" in the sense that it is a real numerator over an integer denominator, namely . It is also a quadratic irrational (a root of ), exactly as the periodicity theorem predicts.
Example. Write as a simple fraction.
This time the periodic part does not start immediately. Set , i.e. the repeating tail on its own;
Simplifying,
Here starts with the partial quotient , so and we take the positive root; .
Now we resolve the non-periodic entries by evaluating the rest of the chain, rationalising denominators as we go:
Therefore, ; again a quadratic irrational.
The Continued Fraction Algorithm
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 and the fractional part , then iteratively do the same to the reciprocal of each fractional part:
The stopping conditions match the characterisation theorems:
- If is rational, the process terminates at the th step when , giving the terminating expansion .
- If is a quadratic irrational, the process stops at the th step when the fractional part has appeared earlier in the sequence, say for some ; from that point everything must repeat, giving the periodic expansion .
Basically, the values are the "state" of the algorithm; if the state hits you are done, and if the state ever repeats then so will everything after it.
Rational Inputs
Example. Write as a continued fraction.
Since , the algorithm terminates. Therefore,
Notice that the partial quotients are precisely the quotients from the Euclidean algorithm applied to and ;
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 as a continued fraction.
Here , and the floor function always rounds down, so for a negative number becomes more negative — , not . This keeps the fractional part positive, which is exactly what the definition requires.
Therefore, . 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 .
Since , we have , so . From there, each step is: flip the fractional part, rationalise, and take the floor.
For instance, in the second line, , and since this lies between and , so its floor is ; the other floors are decided the same way. Now , so the state has repeated and everything from onwards cycles. Therefore,
A periodic expansion, as expected, since is a quadratic irrational.
Example. Find the continued fraction expansion for .
Since , we have , so .
Already , so the expansion repeats immediately;
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 with continued fraction expansion , we define the th convergent of to be
Basically, a convergent is what you get by chopping off the expansion after the th partial quotient and evaluating what remains; 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 .
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 .
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 appear amongst the entries of its extended Euclidean algorithm table. Since might not be rational, we can no longer record the remainder row , but we keep the row (which now holds the partial quotients) and generate the and rows exactly as before, seeded by the columns and :
Having done so, the th convergent is given by
where the th convergent comes from the column containing the partial quotient , and so on.
Example. Find the convergents of using the EEA.
For instance, and , so ; these match the convergents we found by hand. Notice how the signs of and alternate down the rows, and the final column recovers , i.e. the number itself.
Example. Find the first seven convergents of .
The partial quotient row just keeps cycling through after the initial :
So , 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 and rows.
- Read off the th convergent as .
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 ; you can see this in the table above, e.g. the denominators satisfy and .
Convergents as Approximations
As implied by their name, the convergents of a real number are increasingly good approximations of . For , the convergents from the table above evaluate to
Notice how the convergents alternate around the target; the even-indexed ones sit below and the odd-indexed ones sit above it,
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 (a floor) always undershoots, 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 th convergent is the best rational approximation for with respect to its denominator, in the precise sense that
where is the denominator of , and is the partial quotient in immediately following the last partial quotient in .
Two things to take away from this bound. First, the error shrinks like — the square of the denominator — which is far better than truncating a decimal expansion (a decimal cut after denominator only guarantees an error of about ). Second, the 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 th convergent of .
From the table, , so . The expansion is , so the partial quotient after is . Therefore,
Therefore, is guaranteed to be within of ; the actual error is , which indeed fits inside the bound.
Example. Find the first five convergents of and verify the error bound for .
Since we want several convergents, we build the EEA table:
For we have and , so
The actual error is , comfortably inside the bound . Also notice the alternation again: .
Famous Approximations of Pi
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 :
So , and the convergents are
The convergent is the primary-school approximation of , with error bound
The convergent is spectacular for its size, precisely because the next partial quotient is the enormous ;
Indeed, while ; seven correct significant figures from a three-digit denominator. Both and were known in ancient times, and no fraction with a smaller denominator beats .
An Application: Designing a Calendar
Example. A tropical year is approximately days. Use continued fractions to decide how many leap years a calendar should have.
The whole problem is about approximating the fractional part by a fraction with a manageable denominator, which is exactly what convergents are best at. Running the algorithm numerically,
So , with convergents
The convergent is the Julian calendar: one leap year every years, which drifts by a full day roughly every years. The convergent says a calendar with leap years every years would drift by only a day every years or so. Interestingly, the Gregorian calendar we actually use ( leap years every years, i.e. ) is not a convergent, and drifts a day every years; the continued fraction answer 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 .
(a) Find the continued fraction expansion and the first few convergents of .
(b) Find a general formula for the th convergent of .
(c) What is the expected error bound for the th convergent of ?
(a). We run the continued fraction algorithm. Since , we have , so .
Already , so the expansion repeats immediately;
There is also a slicker way to see this: satisfies , so dividing by gives , 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 :
So the convergents are — and both the numerators and denominators are the Fibonacci numbers appearing in the table (up to sign). This is no surprise: with every , the recurrence becomes , which is exactly the Fibonacci recurrence.
(b). The pattern above suggests that the th convergent is a ratio of consecutive Fibonacci numbers,
where and .
Proof. Because the expansion is all ones, chopping it after entries gives (the tail of a truncated all-ones expansion is a shorter all-ones expansion). We induct on . For the base case, . Now suppose ; 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 and denominator , the expected error bound for the th convergent is
For instance, for we have and , so
and the actual error is , which fits the bound.
Notice that 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 made 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.