Continued Fraction Calculator – Partial Quotients and Convergents
A continued fraction rewrites a number as a whole part plus one divided by another whole part plus one divided by another, and so on: a0 + 1/(a1 + 1/(a2 + …)). Mathematicians write that chain compactly as [a0; a1, a2, a3, …]. This calculator works in both directions — it expands any number, fraction or constant into its partial quotients, and it folds a list of partial quotients back into the exact fraction it represents.
How the Expansion Works
The forward algorithm is the Euclidean algorithm in disguise. Take the floor of your value to get a0, subtract it, invert what is left, and repeat. For an exact fraction the arithmetic stays in whole numbers: 19/7 gives 19 = 2×7 + 5, then 7 = 1×5 + 2, then 5 = 2×2 + 1, then 2 = 2×1 + 0, so 19/7 = [2; 1, 2, 2]. The remainder reaching zero is the whole story of termination: a simple continued fraction stops if and only if the number is rational.
Convergents Are Best Rational Approximations
Cutting the expansion off after k terms produces a convergent pk/qk, generated by the recurrence pk = ak·pk-1 + pk-2 and qk = ak·qk-1 + qk-2. Convergents are not merely close, they are optimal: no fraction with a denominator up to qk comes nearer the target. That is why the convergents of π run 3/1, 22/7, 333/106, 355/113 — and why 355/113, known in China as Zu Chongzhi's ratio since the fifth century, still beats every fraction with a denominator below 16,000.
Reading a Large Partial Quotient
A big term signals an unusually good approximation just before it. π expands to [3; 7, 15, 1, 292, …], and the jump to 292 is exactly why the preceding convergent 355/113 is accurate to seven decimal places. The same reasoning drives practical design work: gear trains, camera timing belts and calendar rules all need a simple ratio close to an awkward target, and the convergents table hands you the whole shortlist ordered by complexity.
Periodic Expansions and Quadratic Irrationals
The square root of a non-square integer always produces an eventually repeating pattern — Lagrange proved this in 1770. The calculator uses the exact integer recurrence on the triples (m, d, a) rather than floating-point division, so it can identify the repeating block precisely: √2 = [1; 2, 2, 2, …], displayed with a bar over the repeating 2, and √13 = [3; 1, 1, 1, 1, 6, …] with a five-term block. The golden ratio is the extreme case, φ = [1; 1, 1, 1, …], whose all-ones expansion makes it the hardest number of all to approximate by fractions.
3.14159265 is treated as the exact rational 314159265/100000000, so its expansion tracks π for four terms and then diverges. Enter the keywords pi, e, phi or sqrt(n) when you want the constant itself.Reverse Mode and Exact Arithmetic
Given 3, 7, 15, 1, 292, reverse mode folds the expression from the last term backward — x = a_n, then x = a_i + 1/x — and returns 103993/33102, already in lowest terms. Every numerator and denominator here is computed with big-integer arithmetic, so convergent denominators can run to dozens of digits without a single rounding error. Only the first partial quotient may be zero or negative; every later term must be a positive integer, which is what makes the representation essentially unique.
Controls Worth Knowing
Max terms caps how deep an irrational expansion runs, while tolerance stops it as soon as a convergent is close enough — set it to 1e-6 and π stops at 355/113. The terms slider redraws the nested fraction diagram and trims the convergents table, which makes it easy to watch the approximation error shrink term by term before exporting the table as CSV.