Documentation

Formalizations.NumberTheory.LegendreSieve

The Eratosthenes–Legendre sieve #

For P ≠ 0, the number of n ∈ [1, x] coprime to P is Σ_{d ∣ P} μ(d) ⌊x/d⌋ (inclusion–exclusion by Möbius inversion; P need not be squarefree). With P the primorial of ⌊√x⌋, the integers in [1, x] with no prime factor ≤ √x are 1 and the primes in (√x, x], which gives Legendre's formula π(x) - π(√x) + 1 = Σ_{d ∣ P(√x)} μ(d) ⌊x/d⌋ for x ≥ 1.

Main results #

References #

Möbius inversion: Σ_{d ∣ n} μ d = [n = 1].

theorem LegendreSieve.card_coprime_eq_sum_moebius (x P : ℕ) (hP : P ≠ 0) :
↑{n ∈ Finset.Ioc 0 x | n.Coprime P}.card = ∑ d ∈ P.divisors, ArithmeticFunction.moebius d * ↑(x / d)

Exact sieve. For P ≠ 0, the integers in [1, x] coprime to P are counted by inclusion–exclusion: Σ_{d ∣ P} μ(d) ⌊x/d⌋.

theorem LegendreSieve.coprime_primorial_iff (n z : ℕ) :
n.Coprime (primorial z) ↔ ∀ (p : ℕ), Nat.Prime p → p ≤ z → ¬p ∣ n

Coprime to the primorial iff no prime factor ≤ z.

theorem LegendreSieve.sifted_eq (x : ℕ) (hx : 1 ≤ x) :

The sifted set is {1} ∪ {primes in (z, x]}.

Legendre's sieve. For x ≥ 1, π(x) - π(⌊√x⌋) + 1 = Σ_{d ∣ P(⌊√x⌋)} μ(d) ⌊x/d⌋.