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

Kwas/Erdos680.lean

180 lines32 declarationsno sorryimports Mathlib
  1. import Mathlib
  2. /-!
  3. # Erdős problem 680: the clock rule, the eight small exceptions, and a conditional reduction
  4. `Good n` is the inner statement of `Erdos680.erdos_680.parts.i` in Formal Conjectures:
  5. `∃ k ≠ 0, (n + k).minFac > k^2 + 1`. Nothing here proves the conjecture.
  6. -/
  7. namespace Erdos680
  8. open Filter
  9. /-- `n` is good when some `k ≠ 0` has least prime factor of `n + k` above `k^2 + 1`. -/
  10. def Good (n : ℕ) : Prop := ∃ k ≠ 0, (n + k).minFac > k ^ 2 + 1
  11. /-- The clock rule: for `m ≥ 2`, `minFac m > y` iff no prime clock `p ≤ y` divides `m`. -/
  12. theorem minFac_gt_iff {m y : ℕ} (hm : 2 ≤ m) :
  13. m.minFac > y ↔ ∀ p : ℕ, p.Prime → p ≤ y → ¬ p ∣ m := by
  14. constructor
  15. · intro h p hp hpy hdvd
  16. have := Nat.minFac_le_of_dvd hp.two_le hdvd
  17. omega
  18. · intro h
  19. by_contra hle
  20. exact h m.minFac (Nat.minFac_prime (by omega)) (by omega) (Nat.minFac_dvd m)
  21. /-- `k` works for `n` iff no clock `p ≤ k^2 + 1` shows `n ≡ -k (mod p)`. -/
  22. theorem works_iff {n k : ℕ} (h : 2 ≤ n + k) :
  23. (n + k).minFac > k ^ 2 + 1 ↔ ∀ p : ℕ, p.Prime → p ≤ k ^ 2 + 1 → ¬ p ∣ n + k :=
  24. minFac_gt_iff h
  25. /-- A `k` with `n + k ≤ k^2 + 1` never works. -/
  26. theorem not_works_of_large {n k : ℕ} (hk : k ≠ 0) (h : n + k ≤ k ^ 2 + 1) :
  27. ¬ (n + k).minFac > k ^ 2 + 1 := by
  28. have := Nat.minFac_le (n := n + k) (by omega)
  29. omega
  30. /-- Every even `n ≥ 2` is good, with `k = 1`. -/
  31. theorem good_of_even {n : ℕ} (hn : Even n) (h2 : 2 ≤ n) : Good n := by
  32. refine ⟨1, one_ne_zero, ?_⟩
  33. have hodd : ¬ 2 ∣ n + 1 := by
  34. obtain ⟨m, rfl⟩ := hn
  35. omega
  36. have hp := Nat.minFac_prime (n := n + 1) (by omega)
  37. have hne : (n + 1).minFac ≠ 2 := fun h => hodd (h ▸ Nat.minFac_dvd (n + 1))
  38. have := hp.two_le
  39. norm_num
  40. omega
  41. /-- A prime `p > n` with `(p - n)^2 + 1 < p` makes `n` good. -/
  42. theorem good_of_prime {n p : ℕ} (hp : p.Prime) (h1 : n < p) (h2 : (p - n) ^ 2 + 1 < p) : Good n := by
  43. refine ⟨p - n, by omega, ?_⟩
  44. have : n + (p - n) = p := by omega
  45. rw [this, hp.minFac_eq]
  46. exact h2
  47. /-- Conditional reduction: if every large `n` is followed by a prime `p` with `(p - n)^2 + 1 < p`
  48. (prime gaps below about `√p`), then the Formal Conjectures statement of part (i) holds. -/
  49. theorem erdos_680_of_short_gaps
  50. (H : ∀ᶠ (n : ℕ) in atTop, ∃ p : ℕ, p.Prime ∧ n < p ∧ (p - n) ^ 2 + 1 < p) :
  51. ∀ᶠ (n : ℕ) in atTop, ∃ k ≠ 0, (n + k).minFac > k ^ 2 + 1 := by
  52. filter_upwards [H] with n hn
  53. obtain ⟨p, hp, h1, h2⟩ := hn
  54. exact good_of_prime hp h1 h2
  55. /-- If `n` is bad, every prime `q > n` is far from `n`: `q ≤ (q - n)^2 + 1`. -/
  56. theorem prime_far_of_bad {n q : ℕ} (h : ¬ Good n) (hq : q.Prime) (hn : n < q) : q ≤ (q - n) ^ 2 + 1 := by
  57. by_contra hlt
  58. exact h (good_of_prime hq hn (by omega))
  59. /-- **Bad `n` lie in long prime gaps.** If `n` is bad and `p ≤ n < q` with `q` prime, then
  60. `q ≤ (q - p)^2 + 1`, i.e. the gap `q - p` is at least `√(q - 1)`. -/
  61. theorem long_gap_of_bad {n p q : ℕ} (h : ¬ Good n) (hq : q.Prime) (hpn : p ≤ n) (hn : n < q) :
  62. q ≤ (q - p) ^ 2 + 1 := by
  63. have h1 := prime_far_of_bad h hq hn
  64. have h2 : (q - n) ^ 2 ≤ (q - p) ^ 2 := Nat.pow_le_pow_left (by omega) 2
  65. omega
  66. /-! ## Finite checks -/
  67. /-- no `d` with `2 ≤ d ≤ y` divides `m` -/
  68. def noDiv (m y : ℕ) : Bool := (List.range (y + 1)).all fun d => d < 2 || m % d != 0
  69. /-- some `d` with `2 ≤ d ≤ y` divides `m` -/
  70. def hasDiv (m y : ℕ) : Bool := (List.range (y + 1)).any fun d => 2 ≤ d && m % d == 0
  71. /-- some `1 ≤ k ≤ K` works for `n` -/
  72. def goodB (n K : ℕ) : Bool := (List.range (K + 1)).any fun k => k != 0 && noDiv (n + k) (k ^ 2 + 1)
  73. /-- every `1 ≤ k < B` fails for `n` -/
  74. def badB (n B : ℕ) : Bool := (List.range B).all fun k => k == 0 || hasDiv (n + k) (k ^ 2 + 1)
  75. theorem minFac_gt_of_noDiv {m y : ℕ} (hm : 2 ≤ m) (h : noDiv m y = true) : m.minFac > y := by
  76. rw [minFac_gt_iff hm]
  77. intro p hp hpy hdvd
  78. simp only [noDiv, List.all_eq_true, List.mem_range, Bool.or_eq_true, decide_eq_true_eq, bne_iff_ne] at h
  79. rcases h p (by omega) with h | h
  80. · have := hp.two_le
  81. omega
  82. · exact h (Nat.mod_eq_zero_of_dvd hdvd)
  83. theorem minFac_le_of_hasDiv {m y : ℕ} (h : hasDiv m y = true) : m.minFac ≤ y := by
  84. simp only [hasDiv, List.any_eq_true, List.mem_range, Bool.and_eq_true, decide_eq_true_eq, beq_iff_eq] at h
  85. obtain ⟨d, hd, h2, hmod⟩ := h
  86. have := Nat.minFac_le_of_dvd h2 (Nat.dvd_of_mod_eq_zero hmod)
  87. omega
  88. theorem good_of_goodB {n K : ℕ} (hn : 1 ≤ n) (h : goodB n K = true) : Good n := by
  89. simp only [goodB, List.any_eq_true, List.mem_range, Bool.and_eq_true, bne_iff_ne] at h
  90. obtain ⟨k, -, hk, hd⟩ := h
  91. exact ⟨k, hk, minFac_gt_of_noDiv (by omega) hd⟩
  92. theorem not_good_of_badB {n B : ℕ} (hB : n ≤ B * (B - 1)) (h : badB n B = true) : ¬ Good n := by
  93. rintro ⟨k, hk, hgt⟩
  94. simp only [badB, List.all_eq_true, List.mem_range, Bool.or_eq_true, beq_iff_eq] at h
  95. by_cases hkB : k < B
  96. · rcases h k hkB with h | h
  97. · exact hk h
  98. · have := minFac_le_of_hasDiv h
  99. omega
  100. · refine not_works_of_large hk ?_ hgt
  101. have h1 : B * (B - 1) ≤ k * (k - 1) := Nat.mul_le_mul (by omega) (by omega)
  102. have h2 : k * (k - 1) + k = k ^ 2 := by
  103. cases k with
  104. | zero => exact absurd rfl hk
  105. | succ j => simp; ring
  106. omega
  107. /-- The eight positive integers found by the search with no admissible `k`. -/
  108. theorem bad_1 : ¬ Good 1 := not_good_of_badB (B := 2) (by decide) (by decide +kernel)
  109. theorem bad_3 : ¬ Good 3 := not_good_of_badB (B := 3) (by decide) (by decide +kernel)
  110. theorem bad_7 : ¬ Good 7 := not_good_of_badB (B := 4) (by decide) (by decide +kernel)
  111. theorem bad_13 : ¬ Good 13 := not_good_of_badB (B := 5) (by decide) (by decide +kernel)
  112. theorem bad_23 : ¬ Good 23 := not_good_of_badB (B := 6) (by decide) (by decide +kernel)
  113. theorem bad_31 : ¬ Good 31 := not_good_of_badB (B := 7) (by decide) (by decide +kernel)
  114. theorem bad_113 : ¬ Good 113 := not_good_of_badB (B := 12) (by decide) (by decide +kernel)
  115. theorem bad_115 : ¬ Good 115 := not_good_of_badB (B := 12) (by decide) (by decide +kernel)
  116. /-- `0` is bad as well (trivially). -/
  117. theorem bad_0 : ¬ Good 0 := not_good_of_badB (B := 1) (by decide) (by decide +kernel)
  118. def exc (n : ℕ) : Bool := n == 0 || n == 1 || n == 3 || n == 7 || n == 13 || n == 23 || n == 31 || n == 113 || n == 115
  119. def checkBelow (N K : ℕ) : Bool := (List.range N).all fun n => exc n || goodB n K
  120. theorem good_of_checkBelow {N K n : ℕ} (h : checkBelow N K = true) (hn : n < N) (he : exc n = false) :
  121. Good n := by
  122. simp only [checkBelow, List.all_eq_true, List.mem_range, Bool.or_eq_true] at h
  123. rcases h n hn with h | h
  124. · simp [he] at h
  125. · refine good_of_goodB ?_ h
  126. rcases n with _ | n
  127. · simp [exc] at he
  128. · omega
  129. theorem check_20000 : checkBelow 20000 60 = true := by decide +kernel
  130. /-- Below 20000 the bad `n` are exactly `0, 1, 3, 7, 13, 23, 31, 113, 115`. -/
  131. theorem good_below_20000 {n : ℕ} (hn : n < 20000) (he : exc n = false) : Good n :=
  132. good_of_checkBelow check_20000 hn he
  133. /-- 681: if `n + 1` is composite then `k = 1` works, so every bad `n` for 681 has `n + 1` prime. -/
  134. theorem good681_of_succ_composite {n : ℕ} (h2 : 2 ≤ n + 1) (_h : ¬ (n + 1).Prime) :
  135. ∃ k > 0, ¬ (n + k).Prime ∧ 2 ≤ n + k ∧ (n + k).minFac > k ^ 2 := by
  136. refine ⟨1, one_pos, _h, h2, ?_⟩
  137. have := (Nat.minFac_prime (n := n + 1) (by omega)).two_le
  138. simp only [one_pow]
  139. omega
  140. end Erdos680
  141. #print axioms Erdos680.works_iff
  142. #print axioms Erdos680.erdos_680_of_short_gaps
  143. #print axioms Erdos680.long_gap_of_bad
  144. #print axioms Erdos680.bad_115
  145. #print axioms Erdos680.good_below_20000
  146. #print axioms Erdos680.good_of_even
  147. #print axioms Erdos680.good681_of_succ_composite