Lean / janjarosz05/0kwas / Erdos1094Below.lean
Lean readerKwas/Erdos1094Below.lean
Lean 4 Web has Mathlib, so it can check files that import only Mathlib. This one also imports Kwas.Erdos1094Classify, which it does not have: you can read it there, but it will not check. Build the project itself to check it.
import Kwas.Erdos1094Classify/-! Erdős Problem 1094: the half `n < k ^ 2` (continues namespace Erdos1093).For `2k ≤ n < k ^ 2` the bound `max (n / k) k` equals `k`, so `(n, k)` is an exception iff no prime `p ≤ k`divides `C(n, k)`. For a prime `p₁` with `k / 2 < p₁ ≤ k` this forces `n mod p₁ ≥ k - p₁`, which leaves only`2 p₁ - k` residues modulo `p₁`. The checker `chkK` runs through these candidates `n < k ^ 2` and asks that eachone is ruled out by a prime power `p ^ e` with `p ≤ k` (`n mod p ^ e < k mod p ^ e`) or is a known exception.`exc_of_chkK` proves that a successful run classifies the exceptions `(n, k)` with `n < k ^ 2`.The data for the runs are in `Kwas.Erdos1094Below1` … `Kwas.Erdos1094Below4` and the final statements in`Kwas.Erdos1094BelowData` (all generated by `reports/erdos1094_criteria/gen_below.py`). -/namespace Erdos1093/-- For an exception `(n, k)`, a prime `p ≤ k` and every `e`: `k mod p ^ e ≤ n mod p ^ e`. -/theorem exc_mod_pow_le {n k p : ℕ} (he : (n, k) ∈ Exc) (hp : p.Prime) (hpk : p ≤ k) (e : ℕ) :k % p ^ e ≤ n % p ^ e := byobtain ⟨-, -, hmin⟩ := hehave hnd : ¬ p ∣ n.choose k := byintro hdhave h1 := Nat.minFac_le_of_dvd hp.two_le hdhave h2 := le_max_right (n / k) komegaexact passes_mod_pow hp.two_le e n k ((not_dvd_choose_iff_passes hp k n).1 hnd)/-- `loopD f cnt` checks `f i` for every `i < cnt`. It is written with `Nat.rec` directly: the kernelevaluates this several times faster than `List.range` with `List.all`. -/def loopD (f : ℕ → Bool) (cnt : ℕ) : Bool :=Nat.rec (motive := fun _ => Bool) true (fun i ih => f i && ih) cnttheorem loopD_spec {f : ℕ → Bool} : ∀ {cnt : ℕ}, loopD f cnt = true → ∀ i, i < cnt → f i = true := byintro cntinduction cnt with| zero => intro _ i hi; omega| succ c ih =>intro h i hihave h' : (f c && loopD f c) = true := hrw [Bool.and_eq_true] at h'by_cases h1 : i = c· rw [h1]; exact h'.1· exact ih h'.2 i (by omega)/-- Trial division up to a power of two above `√p`, written so that the kernel evaluates it quickly. -/def isPrimeB (p : ℕ) : Bool :=Nat.ble 2 p && Nat.blt p (2 ^ (Nat.log2 p / 2 + 1) * 2 ^ (Nat.log2 p / 2 + 1)) &&loopD (fun i => Nat.ble p (i + 2) || !(Nat.beq (p % (i + 2)) 0)) (2 ^ (Nat.log2 p / 2 + 1))theorem prime_of_isPrimeB {p : ℕ} (h : isPrimeB p = true) : p.Prime := byunfold isPrimeB at hsimp only [Bool.and_eq_true, Nat.ble_eq, Nat.blt_eq] at hobtain ⟨⟨h2, hc⟩, hall⟩ := hby_contra hnphave hm2 : 2 ≤ p.minFac := (Nat.minFac_prime (by omega)).two_lehave hsq : p.minFac ^ 2 ≤ p := Nat.minFac_sq_le_self (by omega) hnprw [pow_two] at hsqhave hd : p.minFac ∣ p := Nat.minFac_dvd phave hmc : p.minFac < 2 ^ (Nat.log2 p / 2 + 1) := byby_contra hgehave hge' : 2 ^ (Nat.log2 p / 2 + 1) ≤ p.minFac := Nat.le_of_not_lt hgehave := Nat.mul_le_mul hge' hge'omegahave hmp : p.minFac < p := byhave := Nat.mul_le_mul_right p.minFac hm2omegahave h3 := loopD_spec hall (p.minFac - 2) (by omega)have e : p.minFac - 2 + 2 = p.minFac := by omegasimp only [e, Nat.mod_eq_zero_of_dvd hd, Bool.or_eq_true, Nat.ble_eq] at h3rcases h3 with h3 | h3· omega· exact absurd h3 (by decide)/-- `n` is ruled out by one of the prime powers `p ^ e` of the list: `n mod p ^ e < k mod p ^ e`. -/def killedBy (qs : List (ℕ × ℕ)) (n k : ℕ) : Bool :=List.rec (motive := fun _ => Bool) false(fun pe _ ih => Nat.blt (n % pe.1 ^ pe.2) (k % pe.1 ^ pe.2) || ih) qstheorem killedBy_spec {n k : ℕ} : ∀ {qs : List (ℕ × ℕ)}, killedBy qs n k = true →∃ pe ∈ qs, n % pe.1 ^ pe.2 < k % pe.1 ^ pe.2 := byintro qsinduction qs with| nil => intro h; exact absurd h Bool.false_ne_true| cons pe t ih =>intro hhave h' : (Nat.blt (n % pe.1 ^ pe.2) (k % pe.1 ^ pe.2) || killedBy t n k) = true := hrw [Bool.or_eq_true] at h'rcases h' with h1 | h1· simp only [Nat.blt_eq] at h1exact ⟨pe, List.mem_cons.2 (Or.inl rfl), h1⟩· obtain ⟨pe', hm, hlt⟩ := ih h1exact ⟨pe', List.mem_cons.2 (Or.inr hm), hlt⟩/-- The test for one candidate `n`: ruled out by the list, outside `[2k, k ^ 2)`, or known. -/def tst (qs : List (ℕ × ℕ)) (known : List ℕ) (k n : ℕ) : Bool :=killedBy qs n k || Nat.blt n (2 * k) || Nat.ble (k * k) n || known.contains n/-- The check for one `k`: `p₁` is a prime with `k / 2 < p₁ ≤ k`, `p₂` is a prime `≤ k`, the list `qs` holdspairs `(p, e)` with `p` a prime `≤ k`, and every `n = q p₁ + r` with `k - p₁ ≤ r < p₁` and `2k ≤ n < k ^ 2`is ruled out by `p₂` (`n mod p₂ < k mod p₂`, tested first because it is the cheapest for the kernel), or bysome `p ^ e` of the list, or belongs to `known`. -/def chkK (k p₁ p₂ : ℕ) (qs : List (ℕ × ℕ)) (known : List ℕ) : Bool :=isPrimeB p₁ && Nat.ble p₁ k && Nat.blt k (2 * p₁) && isPrimeB p₂ && Nat.ble p₂ k &&qs.all (fun pe => isPrimeB pe.1 && Nat.ble pe.1 k) &&loopD (fun q => loopD (fun s => Nat.blt ((q * p₁ + (k - p₁) + s) % p₂) (k % p₂) ||tst qs known k (q * p₁ + (k - p₁) + s)) (2 * p₁ - k)) (k * k / p₁ + 1)/-- **Soundness of the check.** If `chkK k p₁ p₂ qs known` succeeds, every exception `(n, k)` with `n < k ^ 2`has `n` in `known`. -/theorem exc_of_chkK {k p₁ p₂ : ℕ} {qs : List (ℕ × ℕ)} {known : List ℕ}(h : chkK k p₁ p₂ qs known = true) {n : ℕ} (he : (n, k) ∈ Exc) (hn : n < k * k) : n ∈ known := byunfold chkK at hsimp only [Bool.and_eq_true, List.all_eq_true, Nat.blt_eq, Nat.ble_eq] at hobtain ⟨⟨⟨⟨⟨⟨hp1, hle⟩, hlt⟩, hp2⟩, hle2⟩, hqs⟩, hall⟩ := hhave hp : p₁.Prime := prime_of_isPrimeB hp1have hmod := exc_mod_pow_le he hp hle 1rw [pow_one] at hmodhave hkm : k % p₁ = k - p₁ := byrw [Nat.mod_eq_sub_mod hle, Nat.mod_eq_of_lt (by omega)]have h2k : 2 * k ≤ n := byobtain ⟨-, h, -⟩ := heexact hhave hq : n / p₁ < k * k / p₁ + 1 := Nat.lt_succ_of_le (Nat.div_le_div_right hn.le)have hs : n % p₁ - (k - p₁) < 2 * p₁ - k := byhave := Nat.mod_lt n hp.posomegahave hnn : n / p₁ * p₁ + (k - p₁) + (n % p₁ - (k - p₁)) = n := byhave := Nat.div_add_mod n p₁rw [Nat.mul_comm] at thisomegahave h4 : (Nat.blt ((n / p₁ * p₁ + (k - p₁) + (n % p₁ - (k - p₁))) % p₂) (k % p₂) ||tst qs known k (n / p₁ * p₁ + (k - p₁) + (n % p₁ - (k - p₁)))) = true :=loopD_spec (loopD_spec hall _ hq) _ hsrw [hnn, Bool.or_eq_true] at h4rcases h4 with h0 | h4· exfalsosimp only [Nat.blt_eq] at h0have h5 := exc_mod_pow_le he (prime_of_isPrimeB hp2) hle2 1rw [pow_one] at h5exact absurd h0 (Nat.not_lt.2 h5)unfold tst at h4simp only [Bool.or_eq_true, Nat.blt_eq, Nat.ble_eq] at h4rcases h4 with ((h1 | h1) | h1) | h1· exfalsoobtain ⟨pe, hpe, hlt2⟩ := killedBy_spec h1have hq2 := hqs pe hpehave h5 := exc_mod_pow_le he (prime_of_isPrimeB hq2.1) hq2.2 pe.2exact absurd hlt2 (Nat.not_lt.2 h5)· omega· omega· simpa using h1/-- The 13 known exceptions with `n < k ^ 2` (the fourteenth, `(62, 6)`, has `n ≥ k ^ 2`). -/def below13 : List (ℕ × ℕ) :=[(7, 3), (13, 4), (14, 4), (23, 5), (44, 8), (46, 10), (47, 10), (74, 10), (94, 10), (95, 10), (47, 11),(241, 16), (284, 28)]theorem below13_sub : ∀ x ∈ below13, x ∈ known14 := by decide/-- All 14 known exceptions have `k ≤ 28`. -/theorem known14_k_le : ∀ x ∈ known14, x.2 ≤ 28 := by decide/-- The known exceptions `n` for a given `k`, among those with `n < k ^ 2`. -/def knownBelow (k : ℕ) : List ℕ :=(below13.filter fun x => Nat.beq x.2 k).map fun x => x.1theorem knownBelow_mem {n k : ℕ} (h : n ∈ knownBelow k) : (n, k) ∈ below13 := byunfold knownBelow at hrw [List.mem_map] at hobtain ⟨⟨a, b⟩, hmem, rfl⟩ := hrw [List.mem_filter] at hmemobtain ⟨hm, hb⟩ := hmemhave : b = k := Nat.eq_of_beq_eq_true hbsubst thisexact hm/-- Runs `chkK` for `k = a, a + 1, …` with the data `(p₁, p₂, qs)` of the list. -/def chkFrom : ℕ → List (ℕ × ℕ × List (ℕ × ℕ)) → Bool| _, [] => true| a, d :: t => chkK a d.1 d.2.1 d.2.2 (knownBelow a) && chkFrom (a + 1) ttheorem exc_of_chkFrom : ∀ (data : List (ℕ × ℕ × List (ℕ × ℕ))) (a : ℕ), chkFrom a data = true →∀ {n k : ℕ}, a ≤ k → k < a + data.length → (n, k) ∈ Exc → n < k * k → (n, k) ∈ known14 := byintro datainduction data with| nil =>intro a _ n k h1 h2simp only [List.length_nil, Nat.add_zero] at h2omega| cons d t ih =>intro a h n k h1 h2 he hnsimp only [chkFrom, Bool.and_eq_true] at hsimp only [List.length_cons] at h2rcases Nat.eq_or_lt_of_le h1 with rfl | hlt· exact below13_sub _ (knownBelow_mem (exc_of_chkK h.1 he hn))· exact ih (a + 1) h.2 (by omega) (by omega) he hn/-- The same with the length of the data given separately (for long literal lists). -/theorem exc_of_chkFrom_len (data : List (ℕ × ℕ × List (ℕ × ℕ))) (a N : ℕ) (h : chkFrom a data = true)(hlen : data.length = N) {n k : ℕ} (h1 : a ≤ k) (h2 : k < a + N) (he : (n, k) ∈ Exc) (hn : n < k * k) :(n, k) ∈ known14 := bysubst hlenexact exc_of_chkFrom data a h h1 h2 he hn/-- For `2k ≤ n < k ^ 2` a pair that is not an exception has a prime `p ≤ k` dividing `C(n, k)`. -/theorem exists_prime_dvd_of_not_exc {n k : ℕ} (hk : 0 < k) (h2 : 2 * k ≤ n) (hn : n < k * k)(hne : (n, k) ∉ Exc) : ∃ p : ℕ, p.Prime ∧ p ≤ k ∧ p ∣ n.choose k := byhave hle : (n.choose k).minFac ≤ max (n / k) k := byby_contra hcexact hne ⟨hk, h2, by omega⟩have hdiv : n / k < k := Nat.div_lt_of_lt_mul hnhave hmax : max (n / k) k = k := max_eq_right hdiv.lerw [hmax] at hlehave hne1 : n.choose k ≠ 1 := byintro h1rcases Nat.choose_eq_one_iff.1 h1 with h | h <;> omegaexact ⟨_, Nat.minFac_prime hne1, hle, Nat.minFac_dvd _⟩end Erdos1093#print axioms Erdos1093.exc_of_chkK#print axioms Erdos1093.exc_of_chkFrom#print axioms Erdos1093.exists_prime_dvd_of_not_exc