Continued Fraction Calculator
Convert decimals to continued fractions, find convergents & best rational approximations
Enter a₀; then a₁, a₂, ... — first term may be negative, rest must be positive integers.
Famous Continued Fractions
What is a Continued Fraction?
A continued fraction is a way of writing any real number as a nested sequence of integer additions and reciprocals. For a number x, the continued fraction form is:
Notation: x = [a₀; a₁, a₂, a₃, ...]
The integers a₀, a₁, a₂, ... are called partial quotients. The first term a₀ = ⌊x⌋ is the integer part (floor function), and can be any integer including negative. All subsequent terms aₙ (n ≥ 1) are strictly positive integers.
Every rational number has a finite continued fraction. Every irrational number has an infinite continued fraction. Quadratic irrationals (like √2, √3, the golden ratio) have periodic continued fractions — the partial quotients eventually cycle. This is Lagrange's famous theorem from 1770.
How to Convert a Decimal to a Continued Fraction
The algorithm is a straightforward application of the Euclidean algorithm:
- Take x. Set a₀ = ⌊x⌋ (floor). Compute remainder r₀ = x − a₀.
- If r₀ ≈ 0, stop — x is an integer or we have reached tolerance.
- Replace x with 1/r₀. Set a₁ = ⌊1/r₀⌋. Compute r₁ = 1/r₀ − a₁.
- Repeat until the remainder falls below a small tolerance (e.g. 10⁻¹⁰).
Worked Example: π ≈ 3.14159265358979
| Step n | Value | aₙ = ⌊value⌋ | Remainder |
|---|---|---|---|
| 0 | 3.14159265… | 3 | 0.14159265… |
| 1 | 1/0.14159… = 7.0625… | 7 | 0.06251… |
| 2 | 1/0.06251… = 15.9966… | 15 | 0.99659… |
| 3 | 1/0.99659… = 1.0034… | 1 | 0.0034… |
| 4 | 1/0.0034… = 292.63… | 292 | 0.63… |
Result: π = [3; 7, 15, 1, 292, 1, 1, 1, 2, ...]
Understanding Convergents
The convergents pₙ/qₙ are obtained by truncating the continued fraction at each step. They form the sequence of best rational approximations to x. The recurrence relations are:
k₋₁ = 0, k₀ = 1, kₙ = aₙ · kₙ₋₁ + kₙ₋₂
Convergent n: pₙ/qₙ = hₙ/kₙ
The key property: if p/q is a fraction with q ≤ qₙ, then |x − pₙ/qₙ| ≤ |x − p/q|. No rational with a smaller denominator does better. The error satisfies 1/(qₙ(qₙ₊₁+qₙ)) < |x − pₙ/qₙ| < 1/(qₙ·qₙ₊₁).
Convergents of π
| n | aₙ | pₙ (numerator) | qₙ (denominator) | pₙ/qₙ | Error |
|---|---|---|---|---|---|
| 0 | 3 | 3 | 1 | 3 | −0.14159… |
| 1 | 7 | 22 | 7 | 3.142857… | +0.00126… |
| 2 | 15 | 333 | 106 | 3.141509… | −0.0000832… |
| 3 | 1 | 355 | 113 | 3.1415929… | +2.67×10⁻⁷ |
| 4 | 292 | 103993 | 33102 | 3.14159265301… | −5.78×10⁻¹⁰ |
Notice the jump: 355/113 (denominator 113) achieves error ~2.67×10⁻⁷, while the next convergent (denominator 33102) only appears because of the large partial quotient 292. No fraction with denominator between 114 and 33101 beats 355/113 for approximating π.
Famous Continued Fractions and Their Patterns
The Golden Ratio φ = [1; 1, 1, 1, ...]
The golden ratio has all partial quotients equal to 1 — the smallest possible value. This means each convergent hₙ/kₙ is as far from φ as allowed by the theory, making φ the "most irrational" number. Its convergents are Fibonacci number ratios: 1/1, 2/1, 3/2, 5/3, 8/5, 13/8, 21/13, ... The Fibonacci sequence arises directly from the recurrence with all aₙ = 1.
Euler's number e = [2; 1, 2, 1, 1, 4, 1, 1, 6, ...]
e has a beautiful pattern. After the first few terms, every third partial quotient is 2, 4, 6, 8, ... (the even numbers), with 1s filling the gaps. This elegant regularity was discovered by Euler himself and provides the most compact closed-form description of e's continued fraction.
√2 = [1; 2, 2, 2, ...]
The square root of 2 is the simplest periodic continued fraction beyond the trivial case. Its convergents 1/1, 3/2, 7/5, 17/12, 41/29, ... are the Pell numbers, solving the equation p² − 2q² = ±1. The ancient Greeks used these approximations in their study of incommensurable magnitudes.
Applications of Continued Fractions
- Calendar design: The Gregorian calendar's 97 leap years in 400 years comes from convergents of the tropical year. The Julian calendar's 1/4 = [0; 4] approximation was replaced by better convergents.
- Gear ratios: Engineers use convergents to find gear tooth counts that approximate target ratios with integer constraints.
- Music theory: The 12-tone equal temperament scale arises from continued fraction approximations to log₂(3/2). The convergents give 3, 5, 7, 12, 17, 29, 41, 53-tone scales.
- Cryptography: The Wiener attack on RSA uses continued fractions to factor an RSA modulus when the private exponent is small.
- Stern-Brocot tree: The Stern-Brocot tree organizes all positive rationals — navigating left/right choices corresponds to reading a continued fraction from the root.
- Diophantine approximation: Continued fractions are central to Hurwitz's theorem and Roth's theorem, which quantify how well irrationals can be approximated by rationals.