-- Dirichlet's approximation theorem by pigeonhole: for a nonnegative real -- α and N > 0 there are naturals x and 0 < y ≤ N with |x − y α| N < 1. -- Part of Cumulus, the Grace17 mathematics library. -- Copyright (c) 2026 Grant Passmore. -- Released under the Apache License 2.0. See LICENSE in the repository root. -- Authors: Grant Passmore module Grace.Cumulus.NumberTheory.Dirichlet import Grace.Cumulus.Real.Floor import Grace.Cumulus.Combinatorics.Pigeonhole import Grace.Cumulus.Real.AbsoluteValue -- The boxes: y ↦ ⌊N · frac(y α)⌋ sends 0..N into the N boxes 0..N − 1, -- since the scaled fractional part N · frac(y α) lies in [0, N). theorem dirichletBox.scaled_nonnegative (α : Real) (N y : Nat) requires nonnegative : (0:R) ≤ α shows (0:R) ≤ (realNat(N):R) * realFrac((realNat(y):R) * α) proof have yNonnegative := realNat.zero_le at y have product := realMulValue.nonnegative with yNonnegative, nonnegative have frac := realFrac.nonnegative with product have nNonnegative := realNat.zero_le at N exact realMulValue.nonnegative with nNonnegative, frac theorem dirichletBox.scaled_lt (α : Real) (N y : Nat) requires nonnegative : (0:R) ≤ α requires positive : 0 < N shows (realNat(N):R) * realFrac((realNat(y):R) * α) < realNat(N) proof have yNonnegative := realNat.zero_le at y have product := realMulValue.nonnegative with yNonnegative, nonnegative have frac := realFrac.lt_one with product have nonzero := Less.ne_zero_of_positive with positive have nPositive := realNat.positive with nonzero have scaled := realLess.mul_left_of_positive with frac, nPositive have one : (realNat(N):R) * 1 = realNat(N) by ring rwa one at scaled defgraph dirichletBox (α : Set, N : Nat) (y ∈ succ(N)) : N := realFloor((realNat(N):R) * realFrac((realNat(y):R) * α)) requires real : Real(α) requires nonnegative : (0:R) ≤ α requires positive : 0 < N proof have yNat := natural.member at succ(N) with member have lower := dirichletBox.scaled_nonnegative at α, N, y with * have upper := dirichletBox.scaled_lt at α, N, y with * have below := realFloor.lt_natural with lower, upper exact membership.of_Less with *, below -- N + 1 values into N boxes: two of them collide. theorem dirichletBox.collision (α : Real) (N : Nat) requires nonnegative : (0:R) ≤ α requires positive : 0 < N shows ∃ y ∈ succ(N), ∃ z ∈ succ(N), y ≠ z ∧ dirichletBox(α, N)(y) = dirichletBox(α, N)(z) proof have function := dirichletBox.function with * have finite := Finite.natural at N have more : cardinality(N) < cardinality(succ(N)) rw [(cardinality.natural at N), (cardinality.natural at succ(N))] unfold Less exact LessEqual.reflexive at succ(N) with * exact pigeonhole.collision with function, finite, more -- A collision: two fractional parts in one box of width 1/N differ by -- less than 1/N, and their difference is x − (y − z) α for a natural x. theorem dirichletBox.close (α : Real) (N y z : Nat) requires nonnegative : (0:R) ≤ α requires positive : 0 < N requires yBound : y ≤ N requires zBound : z ≤ N requires same : dirichletBox(α, N)(y) = dirichletBox(α, N)(z) shows abs(realFrac((realNat(y):R) * α) - realFrac((realNat(z):R) * α)) * (realNat(N):R) < (1:R) proof have yMember := membership.successor_of_LessEqual with yBound have zMember := membership.successor_of_LessEqual with zBound have yValue := dirichletBox.value with nonnegative, positive, yMember have zValue := dirichletBox.value with nonnegative, positive, zMember rw [yValue, zValue] at same have nNonnegative := realNat.zero_le at N -- The two scaled fractional parts lie in [⌊·⌋, ⌊·⌋ + 1) for one floor. have yScaled := dirichletBox.scaled_nonnegative at α, N, y with nonnegative have zScaled := dirichletBox.scaled_nonnegative at α, N, z with nonnegative have yLower := realFloor.le with yScaled have yUpper := realFloor.lt with yScaled have zLower := realFloor.le with zScaled have zUpper := realFloor.lt with zScaled rw ← same at zLower rw ← same at zUpper have absN := abs.of_nonnegative with nNonnegative have split := abs.mul at realFrac((realNat(y):R) * α) - realFrac((realNat(z):R) * α), realNat(N) rw [absN] at split rw ← split have difference : (realFrac((realNat(y):R) * α) - realFrac((realNat(z):R) * α)) * realNat(N) = (realNat(N):R) * realFrac((realNat(y):R) * α) - (realNat(N):R) * realFrac((realNat(z):R) * α) ring rw difference by_cases sign : (0:R) ≤ (realNat(N):R) * realFrac((realNat(y):R) * α) - (realNat(N):R) * realFrac((realNat(z):R) * α) . rw abs.of_nonnegative with sign linarith . have negative := realLess.of_not_le with sign have nonpositive := realLess.implies_le with negative rw abs.of_nonpositive with nonpositive linarith -- The difference y − z of naturals z ≤ y is the k with z + k = y. theorem natural.sub_of_add (y z k : Nat) requires equation : add(z, k) = y shows y - z = k proof have commute := add.commutative at k, z with * rw [← equation, commute] exact sub.add_right at k, z -- For z < y, the floors of z α and y α differ by a natural x -- (realFloor.monotone), and x − (y − z) α is the difference of the two -- fractional parts. theorem dirichletBox.difference (α : Real) (y z : Nat) requires nonnegative : (0:R) ≤ α requires below : z < y shows ∃ x, Nat(x) ∧ (realNat(x):R) - (realNat(y - z):R) * α = realFrac((realNat(z):R) * α) - realFrac((realNat(y):R) * α) proof have zNonnegative := realNat.zero_le at z have zProduct := realMulValue.nonnegative with zNonnegative, nonnegative have le := Less.implies_LessEqual with below have mono := realNat.le_of_natural_le with le have scaled := realLe.mul_right_of_nonnegative with mono, nonnegative obtain ⟨x, xNat, equation⟩ := realFloor.monotone with zProduct, scaled have sum := realNat.add at realFloor((realNat(z):R) * α), x with * rw equation at sum obtain ⟨k, kNat, decomposition⟩ := le have split := natural.sub_of_add with decomposition have parts := realNat.add at z, k rw decomposition at parts witness x rw [realFrac.value, realFrac.value, sum, split, parts] ring -- Dirichlet's theorem: a colliding pair z < y gives the approximation -- with denominator y − z. theorem dirichletBox.approximation_of_pair (α : Real) (N y z : Nat) requires nonnegative : (0:R) ≤ α requires positive : 0 < N requires yBound : y ≤ N requires zBound : z ≤ N requires below : z < y requires same : dirichletBox(α, N)(y) = dirichletBox(α, N)(z) shows ∃ x, Nat(x) ∧ ∃ w, Nat(w) ∧ 0 < w ∧ w ≤ N ∧ abs((realNat(x):R) - (realNat(w):R) * α) * (realNat(N):R) < (1:R) proof obtain ⟨x, xNat, equation⟩ := dirichletBox.difference with nonnegative, below have close := dirichletBox.close with * obtain ⟨k, kNat, decomposition⟩ := Less.implies_LessEqual with below have split := natural.sub_of_add with decomposition have kPositive : 0 < k by arith have kBound : k ≤ N by arith have flip : realFrac((realNat(z):R) * α) - realFrac((realNat(y):R) * α) = -(realFrac((realNat(y):R) * α) - realFrac((realNat(z):R) * α)) ring rw [split, flip] at equation refine ⟨x, xNat, k, kNat, kPositive, kBound, ?_⟩ rw [equation, abs.neg] exact close theorem dirichlet.approximation (α : Real) (N : Nat) requires nonnegative : (0:R) ≤ α requires positive : 0 < N shows ∃ x, Nat(x) ∧ ∃ y, Nat(y) ∧ 0 < y ∧ y ≤ N ∧ abs((realNat(x):R) - (realNat(y):R) * α) * (realNat(N):R) < (1:R) proof obtain ⟨y, yMember, z, zMember, different, same⟩ := dirichletBox.collision with nonnegative, positive have yNat := natural.member at succ(N) with yMember have zNat := natural.member at succ(N) with zMember have yBound := LessEqual.of_membership_successor with yMember have zBound := LessEqual.of_membership_successor with zMember have shape := Less.trichotomy at y, z with * rcases shape with less | equal | greater . symm at same exact dirichletBox.approximation_of_pair with nonnegative, positive, zBound, yBound, less, same . exfalso exact different with equal . exact dirichletBox.approximation_of_pair with nonnegative, positive, yBound, zBound, greater, same