MathsNumber Theory

Continued Fraction
calculator.

Turn π, a fraction or a square root into nested whole-number fractions.

How it's worked out

A simple continued fraction writes a number as a whole part, plus 1 over a whole number, plus 1 over another, and so on. The whole numbers are the terms, also called partial quotients. A short way to write them is a list with a semicolon after the first one.

x\displaystyle x=a0+1a1+1a2+⋯\displaystyle {}= a_0 + \frac{1}{a_1 + \frac{1}{a_2 + \cdots}}

For a fraction, the terms are the quotients of the Euclidean algorithm. You divide, then divide the old divisor by the remainder, and stop when the remainder is 0. For 415 over 93 that takes 4 divisions and gives [4; 2, 6, 7]. A rational number always ends, because the remainders keep shrinking.

The divisions for 415 over 93
DivisionTerm
415 = 4 × 93 + 434
93 = 2 × 43 + 72
43 = 6 × 7 + 16
7 = 7 × 17

Cut the list after any term and you get a convergent, which is an ordinary fraction. You do not have to rebuild the nest each time. Each new top and bottom comes from the two before it, starting from 1 over 0 and the first term over 1.

pk\displaystyle p_k=akpk−1+pk−2\displaystyle {}= a_k\,p_{k-1} + p_{k-2}
qk\displaystyle q_k=akqk−1+qk−2\displaystyle {}= a_k\,q_{k-1} + q_{k-2}

Dummit's course notes prove two facts the tool relies on. The convergents land on alternate sides of the number, first below, then above. And the error of each one is at most 1 over the product of its own bottom and the next bottom. That bound is how the tool can promise digits for π without knowing π exactly.

|x−pkqk|\displaystyle \left| x - \frac{p_k}{q_k} \right|≤1qkqk+1\displaystyle {}\le \frac{1}{q_k\,q_{k+1}}

A square root of a whole number that is not a perfect square never ends, but its terms repeat. The tool finds them with whole numbers only, so no rounding creeps in. The block always ends with twice the first term, as the table shows.

Square roots and their repeating blocks
RootTermsBlock
√2[1; 2, …]1
√3[1; 1, 2, …]2
√5[2; 4, …]1
√6[2; 2, 4, …]2
√7[2; 1, 1, 1, 4, …]4
√8[2; 1, 4, …]2
√10[3; 6, …]1
√11[3; 3, 6, …]2
√12[3; 2, 6, …]2
√13[3; 1, 1, 1, 1, 6, …]5
√14[3; 1, 2, 1, 6, …]4
√15[3; 1, 6, …]2

Worked examples

Mei asks why 355 over 113 is so close to π

Mei knows 22 over 7 from school and has seen 355 over 113 quoted as far better. The terms of π begin [3; 7, 15, 1, 292, 1, …], taken from OEIS A001203. The first four convergents are below.

31, 227, 333106, 355113\frac{3}{1},\ \frac{22}{7},\ \frac{333}{106},\ \frac{355}{113}

The next term is 292, which is huge. A big next term makes the next bottom huge too, so the bound for 355 over 113 is 1 over 3,740,526. The real gap is about 2.67 × 10⁻⁷, so it matches π to 6 decimal places.

No fraction with a bottom of 1,000 or less does better: the tool's limit finds 355 over 113 again. With the limit set to 100, the best is 311 over 99, a fraction that sits between two convergents.

Open this example in the tool

Ravi wants a ratio near 415 to 93 with small numbers

Ravi needs a gear pair close to 415 to 93, and his smaller gear can have at most 20 teeth. The terms are [4; 2, 6, 7], so the convergents are 4 over 1, 9 over 2, 58 over 13, 415 over 93. The last one is the ratio itself.

With a limit of 20 on the bottom, the closest is 58 over 13, about 4.46154. The true ratio is about 4.46237, so a 58 to 13 pair is off by about 0.0008.

Open this example in the tool

Sofia solves x² − 19y² = 1

Sofia's number theory homework asks for the smallest whole numbers with x² − 19y² = 1, a Pell equation. The terms of √19 are [4; 2, 1, 3, 1, 2, 8, …], with a block of 6 that repeats.

MathWorld's rule says: when the block has an even length, take the convergent just before its end. That is convergent 5, which is 170 over 39.

1702−19×392\displaystyle 170^2 - 19 \times 39^2=28,900−28,899\displaystyle {}= 28,900 - 28,899=1\displaystyle {}= 1

Open this example in the tool

Reading your result

A large term means a good fraction. The convergent just before a big term is unusually close for its size. In the terms of e, the large terms come in a steady pattern, [2; 1, 2, 1, 1, 4, 1, 1, 6, 1, 1, 8, …], so e has no single standout fraction.

All ones is the slowest case. The golden ratio has every term equal to 1. Its convergents, 1 over 1, 2 over 1, 3 over 2, 5 over 3, 8 over 5, 13 over 8, 21 over 13, 34 over 21, are ratios of Fibonacci numbers, and their bottoms grow as slowly as bottoms can.

A typed decimal is a fraction. Typing 3.14159 gives [3; 7, 15, 1, 25, 1, 7, 4], which ends, because 3.14159 is exactly 314,159 over 100,000. Its first four terms agree with π and then it goes its own way. Pick π from the constants to see its real terms.

Two endings name one number. [3; 7, 16] and [3; 7, 15, 1] both equal 355 over 113, since a last term can always split into one less, then 1. The tool shows the shorter form when it expands a number.

Negative numbers start below. The first term is rounded down, so −7 over 3 starts at −3 and reads [−3; 1, 2]. Every term after the first stays positive.

Common mistakes

  • Expanding a rounded decimal. √2 is [1; 2, 2, 2, …] with 2s for ever, but 1.41421356 breaks the pattern at term 11. Use the square root option, which never rounds.
  • Taking the wrong end of the block for Pell. √13 has a block of 5, an odd length. Its convergent 18 over 5 gives 324 − 325 = −1, not 1, so you need the end of the second block, 649 over 180.
  • Expecting each convergent to be below the number. They take turns. Convergent 0 is below, convergent 1 is above, and the number always sits between two neighbors.
  • Forgetting the whole part. The first term is the whole part, and it can be 0. So 0.75 is [0; 1, 3], not a list that starts with 1.

Questions people ask

It is the convergent that stops just before the term 292, which is far larger than the terms around it. A big next term means the error is tiny compared with the denominator. 355 over 113 differs from π by less than 0.0000003.

Sources