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 each one 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 := by obtain ⟨-, -, hmin⟩ := he have hnd : ¬ p ∣ n.choose k := by intro hd have h1 := Nat.minFac_le_of_dvd hp.two_le hd have h2 := le_max_right (n / k) k omega exact 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 kernel evaluates 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) cnt theorem loopD_spec {f : ℕ → Bool} : ∀ {cnt : ℕ}, loopD f cnt = true → ∀ i, i < cnt → f i = true := by intro cnt induction cnt with | zero => intro _ i hi; omega | succ c ih => intro h i hi have h' : (f c && loopD f c) = true := h rw [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 := by unfold isPrimeB at h simp only [Bool.and_eq_true, Nat.ble_eq, Nat.blt_eq] at h obtain ⟨⟨h2, hc⟩, hall⟩ := h by_contra hnp have hm2 : 2 ≤ p.minFac := (Nat.minFac_prime (by omega)).two_le have hsq : p.minFac ^ 2 ≤ p := Nat.minFac_sq_le_self (by omega) hnp rw [pow_two] at hsq have hd : p.minFac ∣ p := Nat.minFac_dvd p have hmc : p.minFac < 2 ^ (Nat.log2 p / 2 + 1) := by by_contra hge have hge' : 2 ^ (Nat.log2 p / 2 + 1) ≤ p.minFac := Nat.le_of_not_lt hge have := Nat.mul_le_mul hge' hge' omega have hmp : p.minFac < p := by have := Nat.mul_le_mul_right p.minFac hm2 omega have h3 := loopD_spec hall (p.minFac - 2) (by omega) have e : p.minFac - 2 + 2 = p.minFac := by omega simp only [e, Nat.mod_eq_zero_of_dvd hd, Bool.or_eq_true, Nat.ble_eq] at h3 rcases 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) qs theorem killedBy_spec {n k : ℕ} : ∀ {qs : List (ℕ × ℕ)}, killedBy qs n k = true → ∃ pe ∈ qs, n % pe.1 ^ pe.2 < k % pe.1 ^ pe.2 := by intro qs induction qs with | nil => intro h; exact absurd h Bool.false_ne_true | cons pe t ih => intro h have h' : (Nat.blt (n % pe.1 ^ pe.2) (k % pe.1 ^ pe.2) || killedBy t n k) = true := h rw [Bool.or_eq_true] at h' rcases h' with h1 | h1 · simp only [Nat.blt_eq] at h1 exact ⟨pe, List.mem_cons.2 (Or.inl rfl), h1⟩ · obtain ⟨pe', hm, hlt⟩ := ih h1 exact ⟨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` holds pairs `(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 by some `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 := by unfold chkK at h simp only [Bool.and_eq_true, List.all_eq_true, Nat.blt_eq, Nat.ble_eq] at h obtain ⟨⟨⟨⟨⟨⟨hp1, hle⟩, hlt⟩, hp2⟩, hle2⟩, hqs⟩, hall⟩ := h have hp : p₁.Prime := prime_of_isPrimeB hp1 have hmod := exc_mod_pow_le he hp hle 1 rw [pow_one] at hmod have hkm : k % p₁ = k - p₁ := by rw [Nat.mod_eq_sub_mod hle, Nat.mod_eq_of_lt (by omega)] have h2k : 2 * k ≤ n := by obtain ⟨-, h, -⟩ := he exact h have 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 := by have := Nat.mod_lt n hp.pos omega have hnn : n / p₁ * p₁ + (k - p₁) + (n % p₁ - (k - p₁)) = n := by have := Nat.div_add_mod n p₁ rw [Nat.mul_comm] at this omega have 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) _ hs rw [hnn, Bool.or_eq_true] at h4 rcases h4 with h0 | h4 · exfalso simp only [Nat.blt_eq] at h0 have h5 := exc_mod_pow_le he (prime_of_isPrimeB hp2) hle2 1 rw [pow_one] at h5 exact absurd h0 (Nat.not_lt.2 h5) unfold tst at h4 simp only [Bool.or_eq_true, Nat.blt_eq, Nat.ble_eq] at h4 rcases h4 with ((h1 | h1) | h1) | h1 · exfalso obtain ⟨pe, hpe, hlt2⟩ := killedBy_spec h1 have hq2 := hqs pe hpe have h5 := exc_mod_pow_le he (prime_of_isPrimeB hq2.1) hq2.2 pe.2 exact 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.1 theorem knownBelow_mem {n k : ℕ} (h : n ∈ knownBelow k) : (n, k) ∈ below13 := by unfold knownBelow at h rw [List.mem_map] at h obtain ⟨⟨a, b⟩, hmem, rfl⟩ := h rw [List.mem_filter] at hmem obtain ⟨hm, hb⟩ := hmem have : b = k := Nat.eq_of_beq_eq_true hb subst this exact 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) t theorem 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 := by intro data induction data with | nil => intro a _ n k h1 h2 simp only [List.length_nil, Nat.add_zero] at h2 omega | cons d t ih => intro a h n k h1 h2 he hn simp only [chkFrom, Bool.and_eq_true] at h simp only [List.length_cons] at h2 rcases 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 := by subst hlen exact 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 := by have hle : (n.choose k).minFac ≤ max (n / k) k := by by_contra hc exact hne ⟨hk, h2, by omega⟩ have hdiv : n / k < k := Nat.div_lt_of_lt_mul hn have hmax : max (n / k) k = k := max_eq_right hdiv.le rw [hmax] at hle have hne1 : n.choose k ≠ 1 := by intro h1 rcases Nat.choose_eq_one_iff.1 h1 with h | h <;> omega exact ⟨_, 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