Continued Fraction Calculator: Building the Best Rational Approximation of Any Number
About the Author

A continued fraction calculator rewrites a number as a whole part plus one over (a whole part plus one over …). For π the calculator returns [3; 7, 15, 1, 292, 1, 1, 1, 2, …], and stopping early gives the fractions 3, 22/7, 333/106 and 355/113. Those are the convergents, and each one is the closest fraction to π among all fractions whose denominator is no larger than its own. That single property is why continued fractions exist: they hand you the best possible fraction for any budget on the denominator.
The notation [a0; a1, a2, …] means a0 + 1/(a1 + 1/(a2 + …)), where a0 is any integer and every later term is a positive integer. The tool above accepts a decimal, a fraction, a mixed number, a square-root expression such as √2 or (1 + √5)/2, or the words pi and e; it lists the terms, the convergents with their exact errors, and the closest fraction under any denominator limit you set. Type a list of terms instead and it folds them back into an exact fraction or, for a repeating list, an exact surd. Everything below is computed with exact integers, and π and e are carried to 400 digits so that every listed term is certain.
How to Expand a Number by Hand: Floor, Subtract, Flip
Take 415/93. The whole part is 4, because 4 × 93 = 372 and 5 × 93 would be too big. What is left is 43/93, and the trick is to flip it: 43/93 = 1/(93/43). Now repeat on 93/43, whose whole part is 2 with 7/43 left over. Flip again: 43/7 = 6 remainder 1. Flip once more: 7/1 = 7 exactly, and the process stops.
| Step | Division | Term | Flip what is left |
|---|---|---|---|
| 1 | 415 = 4 × 93 + 43 | 4 | 93/43 |
| 2 | 93 = 2 × 43 + 7 | 2 | 43/7 |
| 3 | 43 = 6 × 7 + 1 | 6 | 7/1 |
| 4 | 7 = 7 × 1 + 0 | 7 | done |
So 415/93 = [4; 2, 6, 7] = 4 + 1/(2 + 1/(6 + 1/7)). Look at the left column of divisions: 415 = 4 × 93 + 43, 93 = 2 × 43 + 7, 43 = 6 × 7 + 1. That is the Euclidean algorithm for gcd(415, 93), and the terms of the continued fraction are exactly its quotients. The extended Euclidean algorithm calculator shows the same quotient column for any pair of integers, which is why a fraction’s expansion is always finite: the remainders 43, 7, 1, 0 must reach zero.
A decimal is just a fraction with a power-of-ten denominator, so 3.14159 = 314159/100000 expands the same way, to [3; 7, 15, 1, 25, 1, 7, 4]. An irrational number never reaches a zero remainder, so its expansion never ends. Either way, the calculator does the floor-subtract-flip loop on exact integers; if you only want the reduced fraction of a decimal, the decimal to fraction calculator stops at that first step.
Convergents: The Fractions Worth Keeping
Cut the expansion after n + 1 terms and evaluate what is left; the result pₙ/qₙ is the n-th convergent. You never have to unwind nested fractions to get it, because two recurrences build the numerators and denominators forward from the terms:
pₙ = aₙ × pₙ₋₁ + pₙ₋₂ (start with p₋₁ = 1, p₋₂ = 0)
qₙ = aₙ × qₙ₋₁ + qₙ₋₂ (start with q₋₁ = 0, q₋₂ = 1)
For π the terms 3, 7, 15, 1, 292 give p: 3, 22, 333, 355, 103993 and q: 1, 7, 106, 113, 33102. Check one line: 355 = 1 × 333 + 22 and 113 = 1 × 106 + 7. Here is what the calculator reports for the first six convergents of π:
| n | aₙ | pₙ/qₙ | Error | qₙ² × error | Correct digits |
|---|---|---|---|---|---|
| 0 | 3 | 3/1 | 1.42 × 10⁻¹ | 0.142 | 0.8 |
| 1 | 7 | 22/7 | 1.26 × 10⁻³ | 0.062 | 2.9 |
| 2 | 15 | 333/106 | 8.32 × 10⁻⁵ | 0.935 | 4.1 |
| 3 | 1 | 355/113 | 2.67 × 10⁻⁷ | 0.0034 | 6.6 |
| 4 | 292 | 103993/33102 | 5.78 × 10⁻¹⁰ | 0.633 | 9.2 |
| 5 | 1 | 104348/33215 | 3.32 × 10⁻¹⁰ | 0.366 | 9.5 |
Two facts about consecutive convergents fall out of the recurrence. First, pₙqₙ₋₁ − pₙ₋₁qₙ = (−1)ⁿ⁻¹: for π, 22 × 1 − 3 × 7 = 1, then 333 × 7 − 22 × 106 = −1, then 355 × 106 − 333 × 113 = 1. A determinant of ±1 means each convergent is automatically in lowest terms, so there is never a call to the simplify fractions calculator afterwards. Second, the convergents alternate sides: 3 and 333/106 are below π, 22/7 and 355/113 are above, and the number is always trapped between neighbours.
Why 355/113 Is So Good: Look at the Next Term
The error of a convergent is pinned between two bounds that involve the next denominator:
1/(qₙ(qₙ + qₙ₊₁)) < |x − pₙ/qₙ| < 1/(qₙ qₙ₊₁)
Since qₙ₊₁ = aₙ₊₁qₙ + qₙ₋₁, a large next term makes qₙ₊₁ huge and the error tiny. The term after 355/113 is 292, so q₄ = 292 × 113 + 106 = 33,102 and the error must be below 1/(113 × 33,102) = 2.67 × 10⁻⁷. The actual error, 2.668 × 10⁻⁷, sits right under that ceiling: a three-digit denominator buys six and a half correct decimals. Compare 333/106, which is followed by a 1. Its error is 8.3 × 10⁻⁵, only a little better than the bound would allow, and the qₙ² × error column shows the difference starkly: 0.935 for 333/106 against 0.0034 for 355/113. That column is always below 1, because the upper bound is smaller than 1/qₙ², and the closer it gets to 1 the more ordinary the convergent is.
The deeper statement, proved by Lagrange, is that convergents are exactly the fractions that beat every fraction with a smaller denominator when you measure the error as |q·x − p|. Hurwitz added that infinitely many fractions satisfy |x − p/q| < 1/(√5·q²) for every irrational x, and that √5 cannot be improved, because the golden ratio [1; 1, 1, 1, …] has the smallest possible terms and therefore the slowest-improving convergents of any number. Its convergents are ratios of consecutive Fibonacci numbers, which the Fibonacci calculator lists directly: 1/1, 2/1, 3/2, 5/3, 8/5, 13/8, and so on, each one gaining only about 0.42 decimal digits.
The Best Fraction Under a Denominator Limit
Ask for the closest fraction to π with a denominator of at most 100 and 22/7 is not the answer. The convergents jump from q = 7 straight to q = 106, and between them sit the in-between fractions (3 + k × 22)/(1 + k × 7) for k = 1 to 14: 25/8, 47/15, 69/22, …, 311/99. These semiconvergents are not convergents, yet the later ones beat 22/7. The calculator compares the largest one that fits, 311/99 (k = 14), against 22/7 using exact arithmetic: 311/99 is off by 1.79 × 10⁻⁴, 22/7 by 1.26 × 10⁻³, so 311/99 wins. With the limit at 1,000 the answer flips back to a convergent, 355/113, because the biggest in-between fraction under 1,000, 2818/897, is off by 9.6 × 10⁻⁶.
The rule behind this: the k-th semiconvergent beats the previous convergent whenever k is more than half of the next term, and when k equals exactly half it has to be checked directly. Rather than trusting the rule, the tool measures both candidates against the number itself, which is why it can also report the runner-up.
Two real budgets on the denominator show why this matters. The mean tropical year is about 365.24219 days, and a calendar can only insert whole leap days into whole-year cycles. Expanding 365.24219 gives [365; 4, 7, 1, 3, 24, …], and the convergents of the fractional part are 1/4, 7/29, 8/33 and 31/128. One leap day every 4 years is the Julian rule; 8 leap days in 33 years is the cycle of the Persian Jalali calendar; 31 in 128 gives 365.2421875, closer to the true year than the Gregorian 97 in 400 (365.2425), which is a convenient cycle rather than a convergent. Set the limit to 400 in the calculator and it returns 31/128, not 97/400.
The second budget is musical. A perfect fifth is a frequency ratio of 3/2, which is log₂(3/2) = 0.5849625… of an octave. Expanding 0.5849625007 gives [0; 1, 1, 2, 2, 3, 1, 5, 2, 23, …] with convergents 1/1, 1/2, 3/5, 7/12, 24/41, 31/53 and 179/306. The 7/12 says that seven semitones out of twelve approximate a fifth to within 0.1%, which is why the twelve-note scale works at all; 41 and 53 notes per octave are the next tunings that do markedly better, and both have been built.
Square Roots Repeat: Periodic Continued Fractions
Expand √2 and the terms settle into a loop at once: [1; 2, 2, 2, …], written [1; (2)]. Every square root of a non-square integer does this, and so does every number of the form (p + m√d)/q. Lagrange proved the converse too: an expansion is eventually periodic exactly when the number is a quadratic irrational. The calculator finds the period exactly by running an integer recurrence instead of decimals. Write each stage as (P + √D)/Q; the next term is a = ⌊(P + √D)/Q⌋, and the next stage is P′ = aQ − P, Q′ = (D − P′²)/Q. For √2 the stages are (0, 1) → (1, 1) → (1, 1) …, so the first repeat appears after one step and the period is 1. The square root calculator gives the decimal value; the expansion here is exact and needs no decimals at all.
| Number | Expansion | Period |
|---|---|---|
| √2 | [1; (2)] | 1 |
| √3 | [1; (1, 2)] | 2 |
| √7 | [2; (1, 1, 1, 4)] | 4 |
| √13 | [3; (1, 1, 1, 1, 6)] | 5 |
| √19 | [4; (2, 1, 3, 1, 2, 8)] | 6 |
| √61 | [7; (1, 4, 3, 1, 2, 2, 1, 3, 4, 1, 14)] | 11 |
| (1 + √5)/2 | [1; (1)] | 1 |
The square roots share a shape: the block ends with 2 × a0 (2, 2, 4, 6, 8, 14 above) and everything before that last term reads the same backwards, a consequence of Galois’s theorem on purely periodic expansions. The period length is erratic, though: √61 needs 11 terms while √62 needs only 4, and there is no formula for it in terms of D. There is a payoff for the patience. The convergent just before the end of a period solves Pell’s equation p² − D·q² = ±1: for √2 the convergents 3/2, 7/5, 17/12 and 41/29 give 9 − 8 = 1, 49 − 50 = −1, 289 − 288 = 1, 1681 − 1682 = −1. For √61 the smallest solution has p = 1,766,319,049, which is why Fermat liked to pose it.
Numbers that are not quadratic never settle into a period, but some still have a pattern. Euler showed e = [2; 1, 2, 1, 1, 4, 1, 1, 6, 1, 1, 8, …], with the even numbers marching up every third term; the calculator’s 400 digits confirm that shape for the first 278 terms. π has no known pattern at all, and its terms 3, 7, 15, 1, 292, 1, 1, 1, 2, 1, 3, 1, 14, … are simply what the computation produces, term after term.
Turning a List of Terms Back Into a Number
To evaluate [3; 7, 15, 1] by hand, start at the bottom: 15 + 1/1 = 16, then 7 + 1/16 = 113/16, then 3 + 16/113 = 355/113. Or run the forward recurrence from the previous section and read off the last convergent; the calculator does that, which is also how it fills its table. Notice that [3; 7, 15, 1] and [3; 7, 16] are the same number. A list ending in 1 can always be absorbed into the term before it, so expansions produced by the calculator never end in 1, but it evaluates both spellings when you type them.
A repeating list takes one more idea. In [1; (2)], call the repeating part y = [2; 2, 2, …]. Then y = 2 + 1/y, so y² − 2y − 1 = 0 and y = 1 + √2. The whole number is 1 + 1/y = 1 + 1/(1 + √2) = 1 + (√2 − 1) = √2. In general the block satisfies qₘ·y² + (qₘ₋₁ − pₘ)·y − pₘ₋₁ = 0, where p and q are the convergents of the block alone, and the terms before the block map y to the final value with the same recurrence. The calculator returns the result as an exact (p + m√d)/q, then expands that surd again to confirm that it reproduces the list you typed. Contrast this with repeating decimals, which always name a fraction: 0.(3) is 1/3, and the repeating decimal to fraction calculator handles that conversion; a repeating continued fraction, by contrast, is never rational.
What a Continued Fraction Calculator Must Handle: Rounded Decimals, Negatives and Long Periods
- Rounded input. 3.14159 expands to [3; 7, 15, 1, 25, 1, 7, 4], which is correct for the fraction 314159/100000 but says little about π. If 3.14159 is a rounded measurement, the true value lies anywhere between 3.141585 and 3.141595, and only [3; 7] is shared by that whole interval: the third step of π lands at 15.996, so a nudge of 3 × 10⁻⁷ upward turns the 15 into a 16. The calculator reports how many terms a rounded decimal guarantees. Seven decimals, 3.1415926, guarantee four terms; to see the 292 you need the π button or at least nine decimals.
- Floating point. A double-precision number holds about 16 significant digits, so a spreadsheet loop that floors and flips π drifts into nonsense after roughly 15 terms. This page never touches floating point for the terms: fractions and decimals are exact integers, surds use the integer recurrence, and π and e are computed to 400 digits with the interval method described in the working section, which is how it can list 403 certain terms of π.
- Negative numbers. Two conventions exist. This calculator uses the floor: −7/3 = [−3; 1, 2], because −3 + 1/(1 + 1/2) = −3 + 2/3 = −7/3. The alternative, negating the expansion of 7/3 to get −[2; 3], is common in textbooks; the floor version keeps every later term positive, which is what the convergent theorems assume.
- Roots in the denominator. Write 1/√2 as √2/2 and (1 + √5)/2 with its parentheses; the parser accepts whole numbers only inside the root form, and a radicand up to 10¹⁵ after the coefficient is absorbed (2√3 becomes √12).
- Long periods. The period of √D can be as long as roughly √D × ln D, so a fifteen-digit radicand may repeat only after hundreds of thousands of terms. The tool searches 100,000 terms, shows what it found exactly, and says so when the period is still open.
- A zero after the first term. [3; 0, 2] is not a valid simple continued fraction; the calculator refuses it instead of guessing. Decimal commas and scientific notation are refused with a message for the same reason: every accepted input has one unambiguous meaning.
The definitions, the error bounds and the best-approximation theorems used here follow the treatment in Wikipedia’s simple continued fraction article; the results of Lagrange and Galois on periodicity, and the palindromic shape of √D, are set out in its periodic continued fraction article. The terms of π listed by the calculator were checked against OEIS A001203, and the year length against the J2000 mean tropical year of 365.2421897 days in Wikipedia’s tropical year article; with those seven decimals the expansion begins [365; 4, 7, 1, 3, 27, …] and five terms are certain, which is enough to confirm the 31/128 cycle.



