Home › Fractions › Lesson 5

Farey Sequences and the Stern–Brocot Tree

Lesson 5 of 12

Key idea of this section

If a/b < c/d are consecutive in the Farey sequence Fn, then bc − ad = 1,
b + d > n, and the first fraction to appear between them is the mediant (a + c)/(b + d).

Definition and first examples

The Farey sequence of order n, Fn, is the increasing list of all reduced fractions h/k with 0 ≤ h/k ≤ 1 and 1 ≤ k ≤ n. Thus

F1:  0/1, 1/1
F2:  0/1, 1/2, 1/1
F3:  0/1, 1/3, 1/2, 2/3, 1/1
F4:  0/1, 1/4, 1/3, 1/2, 2/3, 3/4, 1/1

Each Fn arises from Fn−1 by inserting the fractions with denominator exactly n, and there are φ(n) of these, so

|Fn| = 1 + φ(1) + φ(2) + ⋯ + φ(n).

The sum of φ(k) for k ≤ n is asymptotic to 3n²/π², so |Fn| ~ 3n²/π²; the constant 6/π² = 1/ζ(2) is the probability that two random integers are coprime.

The determinant property

The key algebraic fact is a lemma about fractions caught between a unimodular pair.

Lemma 5.1. Let a/b < c/d with b, d > 0 and bc − ad = 1. If h/k is a fraction with k > 0 and a/b < h/k < c/d, then there are positive integers λ, μ with

h = λa + μc,     k = λb + μd.

In particular k ≥ b + d, with equality only for the mediant (a + c)/(b + d).

Proof. The matrix M with columns (a, b) and (c, d) has determinant ad − bc = −1, so M⁻¹ has integer entries, and solving M·(λ, μ) = (h, k) gives

λ = ck − dh,     μ = bh − ak.

Both are integers, and both are positive: ck − dh > 0 because h/k < c/d, and bh − ak > 0 because h/k > a/b. Since λ, μ ≥ 1, k = λb + μd ≥ b + d. ∎

Theorem 5.2. If a/b < c/d are consecutive in Fn, then bc − ad = 1 and b + d > n.

Proof. Induct on n. For F1 the pair 0/1 < 1/1 has 1·1 − 0·1 = 1. Assume the statement for Fn−1, and let a/b < c/d be consecutive there. By Lemma 5.1, every fraction strictly between them has denominator at least b + d, which by induction exceeds n − 1. So if b + d > n nothing is inserted between them in Fn; if b + d = n then exactly one fraction is inserted, namely the mediant, since equality in k ≥ b + d forces λ = μ = 1. The new consecutive pairs a/b < (a + c)/(b + d) and (a + c)/(b + d) < c/d again satisfy the determinant condition, because b(a + c) − a(b + d) = bc − ad = 1 and similarly for the other pair. Every consecutive pair in Fn is of one of these forms, which proves bc − ad = 1. Finally, if b + d ≤ n for neighbours in Fn, the mediant would lie in Fn strictly between them, a contradiction. ∎

Note that bc − ad = 1 also shows that every fraction in Fn is automatically reduced when obtained as a mediant: any common divisor of a + c and b + d divides b(a + c) − a(b + d) = 1.

Computing neighbours and successors

The determinant condition turns the search for neighbours into a congruence. The left neighbour of a reduced fraction h/k in Fn (k ≤ n) is the a/b with hb − ka = 1 and b ≤ n as large as possible, so b is the largest integer ≤ n with b ≡ h⁻¹ (mod k). For instance the left neighbour of 5/12 in F12 has b ≡ 5⁻¹ ≡ 5 (mod 12), so b = 5, a = (5·5 − 1)/12 = 2, giving 2/5. Similarly the right neighbour c/d satisfies 12c − 5d = 1, so d ≡ 7 (mod 12) and c/d = 3/7.

Given two consecutive terms a/b < c/d of Fn, the next term e/f is:

e = ⌊(n + b)/d⌋·c − a,     f = ⌊(n + b)/d⌋·d − b.

Indeed, by Theorem 5.2 the next term e/f satisfies de − cf = 1. Since bc − ad = 1, the pair (e, f) = (−a, −b) is one integer solution of this equation, so the general solution is (e, f) = (κc − a, κd − b) with κ ∈ ℤ. Each solution with 0 < f ≤ n gives an element of Fn to the right of c/d with e/f − c/d = 1/(df), so the nearest one, which is the next term, has the largest admissible f; that is κ = ⌊(n + b)/d⌋. This recurrence lets one generate Fn in O(|Fn|) arithmetic operations.

The Stern–Brocot tree

Start from the formal fractions 0/1 and 1/0 and repeatedly insert mediants between neighbours. The first stages are

0/1, 1/0
0/1, 1/1, 1/0
0/1, 1/2, 1/1, 2/1, 1/0
0/1, 1/3, 1/2, 2/3, 1/1, 3/2, 2/1, 3/1, 1/0

Arranging the inserted fractions as a binary tree, with 1/1 at the root, gives the Stern–Brocot tree (Stern 1858, Brocot 1861). By the argument of Theorem 5.2 every consecutive pair in every stage satisfies bc − ad = 1, so every fraction produced is reduced; and by Lemma 5.1 every positive reduced fraction h/k appears, at a stage bounded by h + k, exactly once. Locating h/k is a binary search: at each node move left (L) if h/k is smaller and right (R) if it is larger. For example 7/5 is reached by the path R L L R: 1/1 → 2/1 → 3/2 → 4/3 → 7/5.

The path encodes the continued fraction of Lesson 7: if h/k = [a₀; a₁, …, ar] then the path is Ra₀ La₁ Ra₂ ⋯ with the final exponent reduced by one. Since 7/5 = [1; 2, 2], the path is R¹L²R¹, as found. Algebraically, the matrices L = (1 0; 1 1) and R = (1 1; 0 1) generate a free monoid, and the products of L and R are exactly the integer matrices of determinant 1 with nonnegative entries; the Stern–Brocot tree is a picture of this monoid.

Farey fractions and the Riemann hypothesis

The Farey fractions are remarkably evenly spread. Enumerate Fn as f₁ < f₂ < ⋯ < fN with N = |Fn|, and compare fν with the equally spaced points ν/N. Franel (1924) and Landau showed that the Riemann hypothesis is equivalent to the estimate

∑ν=1N |fν − ν/N| = O(n1/2 + ε)   for every ε > 0.

Thus one of the most famous open problems in mathematics can be phrased entirely in terms of the arrangement of fractions with bounded denominator.

Exercise

  • (a)
    List F5 and check its length against the totient formula.
  • (b)
    Find the two neighbours of 3/7 in F7 and verify the determinant property.
Show solution

Answers: (a) F5: 0/1, 1/5, 1/4, 1/3, 2/5, 1/2, 3/5, 2/3, 3/4, 4/5, 1/1, which has 11 terms; and 1 + φ(1) + ⋯ + φ(5) = 1 + 1 + 1 + 2 + 2 + 4 = 11. (b) The left neighbour a/b needs 3b − 7a = 1, so b ≡ 5 (mod 7), b = 5, a = 2: it is 2/5. The right neighbour needs 7c − 3d = 1, so d ≡ 2 (mod 7), d = 2, c = 1: it is 1/2. Check: 3·5 − 7·2 = 1 and 7·1 − 3·2 = 1.

V.3.1