Lean / janjarosz05/0kwas / Erdos1094Below.lean
Lean reader

Kwas/Erdos1094Below.lean

222 lines19 declarationsno sorryimports Kwas.Erdos1094Classify

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.

  1. import Kwas.Erdos1094Classify
  2. /-! Erdős Problem 1094: the half `n < k ^ 2` (continues namespace Erdos1093).
  3. For `2k ≤ n < k ^ 2` the bound `max (n / k) k` equals `k`, so `(n, k)` is an exception iff no prime `p ≤ k`
  4. divides `C(n, k)`. For a prime `p₁` with `k / 2 < p₁ ≤ k` this forces `n mod p₁ ≥ k - p₁`, which leaves only
  5. `2 p₁ - k` residues modulo `p₁`. The checker `chkK` runs through these candidates `n < k ^ 2` and asks that each
  6. 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.
  7. `exc_of_chkK` proves that a successful run classifies the exceptions `(n, k)` with `n < k ^ 2`.
  8. The data for the runs are in `Kwas.Erdos1094Below1` … `Kwas.Erdos1094Below4` and the final statements in
  9. `Kwas.Erdos1094BelowData` (all generated by `reports/erdos1094_criteria/gen_below.py`). -/
  10. namespace Erdos1093
  11. /-- For an exception `(n, k)`, a prime `p ≤ k` and every `e`: `k mod p ^ e ≤ n mod p ^ e`. -/
  12. theorem exc_mod_pow_le {n k p : ℕ} (he : (n, k) ∈ Exc) (hp : p.Prime) (hpk : p ≤ k) (e : ℕ) :
  13. k % p ^ e ≤ n % p ^ e := by
  14. obtain ⟨-, -, hmin⟩ := he
  15. have hnd : ¬ p ∣ n.choose k := by
  16. intro hd
  17. have h1 := Nat.minFac_le_of_dvd hp.two_le hd
  18. have h2 := le_max_right (n / k) k
  19. omega
  20. exact passes_mod_pow hp.two_le e n k ((not_dvd_choose_iff_passes hp k n).1 hnd)
  21. /-- `loopD f cnt` checks `f i` for every `i < cnt`. It is written with `Nat.rec` directly: the kernel
  22. evaluates this several times faster than `List.range` with `List.all`. -/
  23. def loopD (f : ℕ → Bool) (cnt : ℕ) : Bool :=
  24. Nat.rec (motive := fun _ => Bool) true (fun i ih => f i && ih) cnt
  25. theorem loopD_spec {f : ℕ → Bool} : ∀ {cnt : ℕ}, loopD f cnt = true → ∀ i, i < cnt → f i = true := by
  26. intro cnt
  27. induction cnt with
  28. | zero => intro _ i hi; omega
  29. | succ c ih =>
  30. intro h i hi
  31. have h' : (f c && loopD f c) = true := h
  32. rw [Bool.and_eq_true] at h'
  33. by_cases h1 : i = c
  34. · rw [h1]; exact h'.1
  35. · exact ih h'.2 i (by omega)
  36. /-- Trial division up to a power of two above `√p`, written so that the kernel evaluates it quickly. -/
  37. def isPrimeB (p : ℕ) : Bool :=
  38. Nat.ble 2 p && Nat.blt p (2 ^ (Nat.log2 p / 2 + 1) * 2 ^ (Nat.log2 p / 2 + 1)) &&
  39. loopD (fun i => Nat.ble p (i + 2) || !(Nat.beq (p % (i + 2)) 0)) (2 ^ (Nat.log2 p / 2 + 1))
  40. theorem prime_of_isPrimeB {p : ℕ} (h : isPrimeB p = true) : p.Prime := by
  41. unfold isPrimeB at h
  42. simp only [Bool.and_eq_true, Nat.ble_eq, Nat.blt_eq] at h
  43. obtain ⟨⟨h2, hc⟩, hall⟩ := h
  44. by_contra hnp
  45. have hm2 : 2 ≤ p.minFac := (Nat.minFac_prime (by omega)).two_le
  46. have hsq : p.minFac ^ 2 ≤ p := Nat.minFac_sq_le_self (by omega) hnp
  47. rw [pow_two] at hsq
  48. have hd : p.minFac ∣ p := Nat.minFac_dvd p
  49. have hmc : p.minFac < 2 ^ (Nat.log2 p / 2 + 1) := by
  50. by_contra hge
  51. have hge' : 2 ^ (Nat.log2 p / 2 + 1) ≤ p.minFac := Nat.le_of_not_lt hge
  52. have := Nat.mul_le_mul hge' hge'
  53. omega
  54. have hmp : p.minFac < p := by
  55. have := Nat.mul_le_mul_right p.minFac hm2
  56. omega
  57. have h3 := loopD_spec hall (p.minFac - 2) (by omega)
  58. have e : p.minFac - 2 + 2 = p.minFac := by omega
  59. simp only [e, Nat.mod_eq_zero_of_dvd hd, Bool.or_eq_true, Nat.ble_eq] at h3
  60. rcases h3 with h3 | h3
  61. · omega
  62. · exact absurd h3 (by decide)
  63. /-- `n` is ruled out by one of the prime powers `p ^ e` of the list: `n mod p ^ e < k mod p ^ e`. -/
  64. def killedBy (qs : List (ℕ × ℕ)) (n k : ℕ) : Bool :=
  65. List.rec (motive := fun _ => Bool) false
  66. (fun pe _ ih => Nat.blt (n % pe.1 ^ pe.2) (k % pe.1 ^ pe.2) || ih) qs
  67. theorem killedBy_spec {n k : ℕ} : ∀ {qs : List (ℕ × ℕ)}, killedBy qs n k = true →
  68. ∃ pe ∈ qs, n % pe.1 ^ pe.2 < k % pe.1 ^ pe.2 := by
  69. intro qs
  70. induction qs with
  71. | nil => intro h; exact absurd h Bool.false_ne_true
  72. | cons pe t ih =>
  73. intro h
  74. have h' : (Nat.blt (n % pe.1 ^ pe.2) (k % pe.1 ^ pe.2) || killedBy t n k) = true := h
  75. rw [Bool.or_eq_true] at h'
  76. rcases h' with h1 | h1
  77. · simp only [Nat.blt_eq] at h1
  78. exact ⟨pe, List.mem_cons.2 (Or.inl rfl), h1⟩
  79. · obtain ⟨pe', hm, hlt⟩ := ih h1
  80. exact ⟨pe', List.mem_cons.2 (Or.inr hm), hlt⟩
  81. /-- The test for one candidate `n`: ruled out by the list, outside `[2k, k ^ 2)`, or known. -/
  82. def tst (qs : List (ℕ × ℕ)) (known : List ℕ) (k n : ℕ) : Bool :=
  83. killedBy qs n k || Nat.blt n (2 * k) || Nat.ble (k * k) n || known.contains n
  84. /-- The check for one `k`: `p₁` is a prime with `k / 2 < p₁ ≤ k`, `p₂` is a prime `≤ k`, the list `qs` holds
  85. pairs `(p, e)` with `p` a prime `≤ k`, and every `n = q p₁ + r` with `k - p₁ ≤ r < p₁` and `2k ≤ n < k ^ 2`
  86. is ruled out by `p₂` (`n mod p₂ < k mod p₂`, tested first because it is the cheapest for the kernel), or by
  87. some `p ^ e` of the list, or belongs to `known`. -/
  88. def chkK (k p₁ p₂ : ℕ) (qs : List (ℕ × ℕ)) (known : List ℕ) : Bool :=
  89. isPrimeB p₁ && Nat.ble p₁ k && Nat.blt k (2 * p₁) && isPrimeB p₂ && Nat.ble p₂ k &&
  90. qs.all (fun pe => isPrimeB pe.1 && Nat.ble pe.1 k) &&
  91. loopD (fun q => loopD (fun s => Nat.blt ((q * p₁ + (k - p₁) + s) % p₂) (k % p₂) ||
  92. tst qs known k (q * p₁ + (k - p₁) + s)) (2 * p₁ - k)) (k * k / p₁ + 1)
  93. /-- **Soundness of the check.** If `chkK k p₁ p₂ qs known` succeeds, every exception `(n, k)` with `n < k ^ 2`
  94. has `n` in `known`. -/
  95. theorem exc_of_chkK {k p₁ p₂ : ℕ} {qs : List (ℕ × ℕ)} {known : List ℕ}
  96. (h : chkK k p₁ p₂ qs known = true) {n : ℕ} (he : (n, k) ∈ Exc) (hn : n < k * k) : n ∈ known := by
  97. unfold chkK at h
  98. simp only [Bool.and_eq_true, List.all_eq_true, Nat.blt_eq, Nat.ble_eq] at h
  99. obtain ⟨⟨⟨⟨⟨⟨hp1, hle⟩, hlt⟩, hp2⟩, hle2⟩, hqs⟩, hall⟩ := h
  100. have hp : p₁.Prime := prime_of_isPrimeB hp1
  101. have hmod := exc_mod_pow_le he hp hle 1
  102. rw [pow_one] at hmod
  103. have hkm : k % p₁ = k - p₁ := by
  104. rw [Nat.mod_eq_sub_mod hle, Nat.mod_eq_of_lt (by omega)]
  105. have h2k : 2 * k ≤ n := by
  106. obtain ⟨-, h, -⟩ := he
  107. exact h
  108. have hq : n / p₁ < k * k / p₁ + 1 := Nat.lt_succ_of_le (Nat.div_le_div_right hn.le)
  109. have hs : n % p₁ - (k - p₁) < 2 * p₁ - k := by
  110. have := Nat.mod_lt n hp.pos
  111. omega
  112. have hnn : n / p₁ * p₁ + (k - p₁) + (n % p₁ - (k - p₁)) = n := by
  113. have := Nat.div_add_mod n p₁
  114. rw [Nat.mul_comm] at this
  115. omega
  116. have h4 : (Nat.blt ((n / p₁ * p₁ + (k - p₁) + (n % p₁ - (k - p₁))) % p₂) (k % p₂) ||
  117. tst qs known k (n / p₁ * p₁ + (k - p₁) + (n % p₁ - (k - p₁)))) = true :=
  118. loopD_spec (loopD_spec hall _ hq) _ hs
  119. rw [hnn, Bool.or_eq_true] at h4
  120. rcases h4 with h0 | h4
  121. · exfalso
  122. simp only [Nat.blt_eq] at h0
  123. have h5 := exc_mod_pow_le he (prime_of_isPrimeB hp2) hle2 1
  124. rw [pow_one] at h5
  125. exact absurd h0 (Nat.not_lt.2 h5)
  126. unfold tst at h4
  127. simp only [Bool.or_eq_true, Nat.blt_eq, Nat.ble_eq] at h4
  128. rcases h4 with ((h1 | h1) | h1) | h1
  129. · exfalso
  130. obtain ⟨pe, hpe, hlt2⟩ := killedBy_spec h1
  131. have hq2 := hqs pe hpe
  132. have h5 := exc_mod_pow_le he (prime_of_isPrimeB hq2.1) hq2.2 pe.2
  133. exact absurd hlt2 (Nat.not_lt.2 h5)
  134. · omega
  135. · omega
  136. · simpa using h1
  137. /-- The 13 known exceptions with `n < k ^ 2` (the fourteenth, `(62, 6)`, has `n ≥ k ^ 2`). -/
  138. def below13 : List (ℕ × ℕ) :=
  139. [(7, 3), (13, 4), (14, 4), (23, 5), (44, 8), (46, 10), (47, 10), (74, 10), (94, 10), (95, 10), (47, 11),
  140. (241, 16), (284, 28)]
  141. theorem below13_sub : ∀ x ∈ below13, x ∈ known14 := by decide
  142. /-- All 14 known exceptions have `k ≤ 28`. -/
  143. theorem known14_k_le : ∀ x ∈ known14, x.2 ≤ 28 := by decide
  144. /-- The known exceptions `n` for a given `k`, among those with `n < k ^ 2`. -/
  145. def knownBelow (k : ℕ) : List ℕ :=
  146. (below13.filter fun x => Nat.beq x.2 k).map fun x => x.1
  147. theorem knownBelow_mem {n k : ℕ} (h : n ∈ knownBelow k) : (n, k) ∈ below13 := by
  148. unfold knownBelow at h
  149. rw [List.mem_map] at h
  150. obtain ⟨⟨a, b⟩, hmem, rfl⟩ := h
  151. rw [List.mem_filter] at hmem
  152. obtain ⟨hm, hb⟩ := hmem
  153. have : b = k := Nat.eq_of_beq_eq_true hb
  154. subst this
  155. exact hm
  156. /-- Runs `chkK` for `k = a, a + 1, …` with the data `(p₁, p₂, qs)` of the list. -/
  157. def chkFrom : ℕ → List (ℕ × ℕ × List (ℕ × ℕ)) → Bool
  158. | _, [] => true
  159. | a, d :: t => chkK a d.1 d.2.1 d.2.2 (knownBelow a) && chkFrom (a + 1) t
  160. theorem exc_of_chkFrom : ∀ (data : List (ℕ × ℕ × List (ℕ × ℕ))) (a : ℕ), chkFrom a data = true →
  161. ∀ {n k : ℕ}, a ≤ k → k < a + data.length → (n, k) ∈ Exc → n < k * k → (n, k) ∈ known14 := by
  162. intro data
  163. induction data with
  164. | nil =>
  165. intro a _ n k h1 h2
  166. simp only [List.length_nil, Nat.add_zero] at h2
  167. omega
  168. | cons d t ih =>
  169. intro a h n k h1 h2 he hn
  170. simp only [chkFrom, Bool.and_eq_true] at h
  171. simp only [List.length_cons] at h2
  172. rcases Nat.eq_or_lt_of_le h1 with rfl | hlt
  173. · exact below13_sub _ (knownBelow_mem (exc_of_chkK h.1 he hn))
  174. · exact ih (a + 1) h.2 (by omega) (by omega) he hn
  175. /-- The same with the length of the data given separately (for long literal lists). -/
  176. theorem exc_of_chkFrom_len (data : List (ℕ × ℕ × List (ℕ × ℕ))) (a N : ℕ) (h : chkFrom a data = true)
  177. (hlen : data.length = N) {n k : ℕ} (h1 : a ≤ k) (h2 : k < a + N) (he : (n, k) ∈ Exc) (hn : n < k * k) :
  178. (n, k) ∈ known14 := by
  179. subst hlen
  180. exact exc_of_chkFrom data a h h1 h2 he hn
  181. /-- For `2k ≤ n < k ^ 2` a pair that is not an exception has a prime `p ≤ k` dividing `C(n, k)`. -/
  182. theorem exists_prime_dvd_of_not_exc {n k : ℕ} (hk : 0 < k) (h2 : 2 * k ≤ n) (hn : n < k * k)
  183. (hne : (n, k) ∉ Exc) : ∃ p : ℕ, p.Prime ∧ p ≤ k ∧ p ∣ n.choose k := by
  184. have hle : (n.choose k).minFac ≤ max (n / k) k := by
  185. by_contra hc
  186. exact hne ⟨hk, h2, by omega⟩
  187. have hdiv : n / k < k := Nat.div_lt_of_lt_mul hn
  188. have hmax : max (n / k) k = k := max_eq_right hdiv.le
  189. rw [hmax] at hle
  190. have hne1 : n.choose k ≠ 1 := by
  191. intro h1
  192. rcases Nat.choose_eq_one_iff.1 h1 with h | h <;> omega
  193. exact ⟨_, Nat.minFac_prime hne1, hle, Nat.minFac_dvd _⟩
  194. end Erdos1093
  195. #print axioms Erdos1093.exc_of_chkK
  196. #print axioms Erdos1093.exc_of_chkFrom
  197. #print axioms Erdos1093.exists_prime_dvd_of_not_exc