Folioby Interconnected
Log InSign Up

Continued fractions: how to find the best rational approximation to any number

Continued fractions are the Euclidean algorithm in disguise. We develop convergents and the best-approximation property, compute the continued fraction of sqrt(2), connect it to Pell's equation, and sketch the Stern-Brocot tree.

FO
Folio Official
March 1, 2026

The fraction 22/7=3.142… approximates π≈3.14159… with an error of less than 0.04%, using a denominator of only 7. Why is 22/7 so good? The answer lies in the theory of continued fractions.

1 Definition

Definition 1 (Continued fraction).
The (simple) continued fraction expansion of a real number α is the expression
α=a0​+a1​+a2​+a3​+⋯1​1​1​
where a0​∈Z and a1​,a2​,…∈Z>0​ are called the partial quotients. We write α=[a0​;a1​,a2​,a3​,…].
Example 2.
3.245=3+0.245=3+1/(4+1/(12+1/4)), so 3.245=[3;4,12,4].

2 The connection to the Euclidean algorithm

The continued fraction expansion and the Euclidean algorithm are exactly the same computation.

Example 3.
The Euclidean algorithm applied to gcd(47,13):
  • 47=3×13+8

  • 13=1×8+5

  • 8=1×5+3

  • 5=1×3+2

  • 3=1×2+1

  • 2=2×1+0

The sequence of quotients is 3,1,1,1,1,2. Meanwhile, 47/13=[3;1,1,1,1,2]. The quotients produced by the Euclidean algorithm are precisely the partial quotients of the continued fraction.
Remark 4.
The continued fraction expansion of a rational number is finite; that of an irrational number is infinite. This mirrors the fact that the Euclidean algorithm terminates in finitely many steps for rational inputs (when the remainder reaches 0).

3 Convergents: truncating for good approximations

Definition 5 (Convergent).
The n-th convergent of the continued fraction α=[a0​;a1​,a2​,…] is
qn​pn​​=[a0​;a1​,a2​,…,an​].

Convergents can be computed efficiently via a recurrence.

Theorem 6 (Convergent recurrence).
pn​=an​pn−1​+pn−2​,qn​=an​qn−1​+qn−2​,
with initial values p−1​=1, p−2​=0, q−1​=0, q−2​=1.
Theorem 7 (Determinant identity).
pn​qn−1​−pn−1​qn​=(−1)n−1.
Remark 8.
This identity implies that the difference between consecutive convergents pn​/qn​ and pn−1​/qn−1​ is ±1/(qn​qn−1​). Since qn​ is strictly increasing, the differences shrink rapidly and the convergents approach α at great speed.

4 Best rational approximations

Theorem 9 (Best approximation property).
If pn​/qn​ is the n-th convergent of α, then for any fraction a/b with b≤qn​ and a/b=pn​/qn​,
​α−qn​pn​​​<​α−ba​​.
Remark 10.
In other words, each convergent is the fraction closest to α among all fractions whose denominator does not exceed qn​. The reason 22/7 approximates π so well is that it is the first convergent of π=[3;7,15,1,292,…].

5 The continued fraction of2​

Example 11 (Continued fraction of 2​).
2​=1+(2​−1). Since 2​−1=1/(2​+1),
2​=1+2​+11​=1+2+(2​−1)1​=1+2+2​+11​1​.
The pattern repeats: 2​=[1;2] (the partial quotient 2 repeats indefinitely).

The convergents are 1/1,3/2,7/5,17/12,41/29,…

The convergent 17/12=1.416 is an excellent approximation to 2​=1.41421…
Remark 12.
More generally, for any positive integer D that is not a perfect square, the continued fraction expansion of D​ is eventually periodic. This is Lagrange's theorem: quadratic irrationals correspond bijectively to periodic continued fractions.

6 Pell's equation

The continued fraction of D​ leads directly to solutions of Pell's equation.

Definition 13 (Pell's equation).
For a positive integer D (not a perfect square),
x2−Dy2=1
is called Pell's equation.
Theorem 14.
The smallest positive solution (x1​,y1​) of x2−Dy2=1 is given by the convergent pk​/qk​ just before the period of the continued fraction of D​ repeats: (x1​,y1​)=(pk​,qk​).
Example 15.
D=2: 2​=[1;2], with period length 1. The convergent p0​/q0​=1/1 gives (x,y)=(1,1): 1−2=−1 (wrong sign). The next convergent p1​/q1​=3/2 gives (x,y)=(3,2): 9−8=1. This is the fundamental solution.

7 The Stern–Brocot tree

Definition 16 (Stern–Brocot tree).
Starting from 0/1 and 1/0, the Stern–Brocot tree is constructed by repeatedly inserting the mediantb+da+c​ between adjacent fractions a/b and c/d.
Remark 17.
The Stern–Brocot tree has remarkable properties:
  • Every positive rational number appears exactly once, in lowest terms.

  • The tree is always sorted (left child < parent < right child).

  • The path to each node (a sequence of left and right turns) corresponds to the continued fraction expansion.

  • A binary search on the tree finds the closest fraction with denominator ≤N in O(logN) time.

8 Takeaway

  • Continued fractions are the Euclidean algorithm viewed from a different angle.

  • Convergents are the best rational approximations with bounded denominator.

  • The continued fraction of D​ is periodic and yields solutions to Pell's equation.

  • The Stern–Brocot tree organizes all positive rationals into a single systematic structure.

In the final article of this series, we shall see how the theorems developed throughout these eight articles converge in a single, celebrated application: the RSA cryptosystem.

Number TheoryMathematicsBetween the Lines
FO
Folio Official

Mathematics "between the lines" — exploring the intuition textbooks leave out, written in LaTeX on Folio.

1 followers·107 articles
Number Theory — Between the LinesPart 7 of 8
Previous
Quadratic residues: why exactly half the nonzero elements mod p are squares
Next
Number theory meets cryptography: why RSA works and what makes it secure

Share your expertise with the world

Write articles with LaTeX support, build your audience, and earn from your knowledge.

Start Writing — It's Free

More from Folio Official

Folio Official·March 1, 2026

Why primes are the atoms of the integers: what the Fundamental Theorem of Arithmetic really says

Unique prime factorization is not as obvious as it seems. Through a counterexample in Z[sqrt(-5)], we witness uniqueness break down, then prove both the existence and uniqueness parts of the Fundamental Theorem, and survey the Sieve of Eratosthenes and the linear sieve.

Number TheoryMathematicsBetween the Lines
1
Folio Official·March 1, 2026

The Chinese Remainder Theorem: why simultaneous congruences can be merged

We view CRT as the number-theoretic incarnation of divide-and-conquer, prove the ring isomorphism Z/mnZ = Z/mZ x Z/nZ, and show how Garner's algorithm avoids overflow in practice.

Number TheoryMathematicsBetween the Lines
1
Folio Official·March 1, 2026

Why the Euclidean algorithm finds the greatest common divisor: from subtraction to division

The recursion gcd(a,b) = gcd(b, a mod b) hides an invariant: the set of common divisors never changes. Starting from a naive subtraction-based method and upgrading to division, we derive Bezout's identity, the extended Euclidean algorithm, and modular inverses.

Number TheoryMathematicsBetween the Lines
1
Folio Official·March 1, 2026

What does it mean to "divide evenly"? Why number theory begins with mod

The modular reduction map is a projection that deliberately discards information. Clocks, days of the week, and the competitive-programming modulus 10^9+7 all share the same algebraic structure. From the definition of divisibility through congruences to the ring Z/nZ, we uncover the reason number theory starts where it does.

Number TheoryMathematicsBetween the Lines