Continued Fraction Calculator

Convert decimals to continued fractions, find convergents & best rational approximations

10
Quick Examples

Famous Continued Fractions

π Non-periodic
[3; 7, 15, 1, 292, 1, 1, 1, 2, ...]
Large term 292 makes 355/113 spectacularly accurate
e Pattern: 1,2n,1
[2; 1, 2, 1, 1, 4, 1, 1, 6, 1, 1, 8, ...]
Beautiful pattern: every 3rd term is 2, 4, 6, 8, ...
√2 Period 1
[1; 2̄] = [1; 2, 2, 2, 2, ...]
Simplest periodic CF — all 2s after the first term
φ Most irrational
[1; 1̄] = [1; 1, 1, 1, 1, ...]
All ones — convergents are Fibonacci ratios
√3 Period 2
[1; 1, 2̄] = [1; 1, 2, 1, 2, 1, 2, ...]
Repeating block of length 2 — classic quadratic irrational

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:

x = a₀ + 1 / (a₁ + 1 / (a₂ + 1 / (a₃ + ...)))
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 nValueaₙ = ⌊value⌋Remainder
03.14159265…30.14159265…
11/0.14159… = 7.0625…70.06251…
21/0.06251… = 15.9966…150.99659…
31/0.99659… = 1.0034…10.0034…
41/0.0034… = 292.63…2920.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:

h₋₁ = 1, h₀ = a₀, hₙ = aₙ · hₙ₋₁ + hₙ₋₂
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 π

naₙpₙ (numerator)qₙ (denominator)pₙ/qₙError
03313−0.14159…
172273.142857…+0.00126…
2153331063.141509…−0.0000832…
313551133.1415929…+2.67×10⁻⁷
4292103993331023.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.

Frequently Asked Questions

What is a continued fraction?
A continued fraction is a representation of a number as a sequence of integers a₀, a₁, a₂, ... written as x = a₀ + 1/(a₁ + 1/(a₂ + 1/(a₃ + ...))). The notation [a₀; a₁, a₂, a₃, ...] is standard shorthand. If the sequence terminates (finite), the number is rational. If it goes on forever (infinite), the number is irrational. For example, √2 = [1; 2, 2, 2, ...] is infinite and periodic, while 22/7 = [3; 7] is finite.
How do you convert a decimal to a continued fraction?
Use the Euclidean-style algorithm: (1) Set a₀ = floor(x). (2) Subtract to get remainder r₀ = x − a₀. (3) If r₀ ≈ 0, stop. (4) Otherwise compute x₁ = 1/r₀, set a₁ = floor(x₁), and repeat. For π: a₀=3 (remainder 0.14159…), a₁=7 (1/0.14159≈7.06), a₂=15 (1/0.0625≈15.99), a₃=1, a₄=292 — giving π = [3; 7, 15, 1, 292, ...].
What are convergents and why are they useful?
Convergents are the fractions pₙ/qₙ obtained by truncating the continued fraction at each step. They are computed by the recurrence hₙ = aₙ·hₙ₋₁ + hₙ₋₂ and kₙ = aₙ·kₙ₋₁ + kₙ₋₂. Their key property is being best rational approximations — no fraction with a denominator ≤ qₙ can approximate x more closely than pₙ/qₙ. This is why 355/113 is the standard engineering approximation for π: no fraction with denominator below 33102 beats it.
What is special about the golden ratio's continued fraction?
φ = [1; 1, 1, 1, ...] has all partial quotients equal to 1 — the minimum possible. This makes its convergents converge as slowly as theoretically possible. No irrational number is harder to approximate by rationals. Its convergents are consecutive Fibonacci ratios: 1/1, 2/1, 3/2, 5/3, 8/5, 13/8, ... This property underpins many natural growth patterns — phyllotaxis (leaf and seed arrangements) use golden-angle spacing precisely because it is "most irrational."
What is the continued fraction for π?
π = [3; 7, 15, 1, 292, 1, 1, 1, 2, 1, 3, 1, 14, ...]. The large partial quotient 292 at position 4 means the preceding convergent 355/113 is extraordinarily accurate. It satisfies |π − 355/113| ≈ 2.67 × 10⁻⁷, matching π to 6 decimal places. No fraction with denominator between 114 and 33101 comes closer. The continued fraction of π has no known pattern, unlike those of e and the algebraic irrationals.
Can continued fractions be negative?
Yes. The first term a₀ can be any integer, including negatives. Because the floor function is used, a₀ = ⌊x⌋ for a number like −1.5 gives a₀ = −2 (since floor(−1.5) = −2). The subsequent partial quotients a₁, a₂, ... are always strictly positive integers in the standard algorithm. For example, −1.5 = [−2; 2] because −1.5 = −2 + 0.5 = −2 + 1/2, so the CF terminates after two terms.
What is a periodic continued fraction?
A periodic continued fraction is one where the sequence of partial quotients eventually repeats. By Lagrange's theorem (1770), a continued fraction is periodic if and only if the number is a quadratic irrational — a root of ax² + bx + c = 0 with integer coefficients. Examples include √2 = [1; 2̄] (period 1), √3 = [1; 1, 2̄] (period 2), √5 = [2; 4̄] (period 1), and (1+√5)/2 = [1; 1̄]. Transcendental numbers like π and e are never periodic.