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

Kwas/Erdos1094Gap2.lean

122 lines2 declarationsno sorryimports Kwas.Erdos1094Gap

Lean 4 Web has Mathlib, so it can check files that import only Mathlib. This one also imports Kwas.Erdos1094Gap, 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.Erdos1094Gap
  2. /-! Erdős Problem 1094: the gap criterion for every `n ≥ 2k` (continues namespace Erdos1093).
  3. `not_exc_of_gap` needs `4k ≤ n`. Here the same conclusion is proved from `2k ≤ n`, which is the whole
  4. range of the problem. -/
  5. namespace Erdos1093
  6. /-- **Gap criterion on the whole range `n ≥ 2k`.** Suppose every interval `(a, a + L]` with
  7. `k < 2 (a + 1)` and `a + L ≤ k` contains a prime. If `2 * k ≤ n` and `8 * n * (L + 1) ≤ k * k`,
  8. then `(n, k)` is not an exception of Erdős problem 1094. -/
  9. theorem not_exc_of_gap_two {n k L : ℕ} (h2 : 2 * k ≤ n) (hL : 8 * n * (L + 1) ≤ k * k)
  10. (hgap : ∀ a : ℕ, k < 2 * (a + 1) → a + L ≤ k → ∃ p : ℕ, p.Prime ∧ a < p ∧ p ≤ a + L) :
  11. (n, k) ∉ Exc := by
  12. intro he
  13. have hk : 0 < k := he.1
  14. have hM1 : (2 * n / k) * k ≤ 2 * n := Nat.div_mul_le_self _ _
  15. have hM2 : 2 * n < (2 * n / k + 1) * k := by
  16. have := Nat.lt_succ_iff.2 (le_refl (2 * n / k))
  17. exact (Nat.div_lt_iff_lt_mul hk).1 this
  18. have hM4 : 4 ≤ 2 * n / k := by
  19. rw [Nat.le_div_iff_mul_le hk]; omega
  20. obtain ⟨m, hm⟩ : ∃ m, 2 * n / k = m + 4 := ⟨2 * n / k - 4, by omega⟩
  21. rw [hm] at hM1 hM2
  22. obtain ⟨d, rfl⟩ : ∃ d, n = k + d := ⟨n - k, by omega⟩
  23. have hNpos : 0 < m + 2 := by omega
  24. -- window ((n-k)/(m+2), n/(m+3)]
  25. have ha1 : (d / (m + 2)) * (m + 2) ≤ d := Nat.div_mul_le_self _ _
  26. have ha2 : d < (d / (m + 2) + 1) * (m + 2) := by
  27. have := Nat.lt_succ_iff.2 (le_refl (d / (m + 2)))
  28. exact (Nat.div_lt_iff_lt_mul hNpos).1 this
  29. generalize d / (m + 2) = a at ha1 ha2
  30. have e1 : (m + 4) * k = k * m + 4 * k := by ring
  31. have e2 : (m + 4 + 1) * k = k * m + 5 * k := by ring
  32. have hi : k * m + 2 * k ≤ 2 * d := by linarith
  33. have hii : 2 * d < k * m + 3 * k := by linarith
  34. have h2a : k < 2 * (a + 1) := by
  35. by_contra h
  36. push Not at h
  37. have h1 : 2 * (a + 1) * (m + 2) ≤ k * (m + 2) := Nat.mul_le_mul_right _ h
  38. have h3 : 2 * (a + 1) * (m + 2) = 2 * ((a + 1) * (m + 2)) := by ring
  39. have h4 : k * (m + 2) = k * m + 2 * k := by ring
  40. omega
  41. -- 4 (m + 4) (L + 1) ≤ k
  42. have hG : 4 * (m + 4) * (L + 1) ≤ k := by
  43. have h1 : 4 * (m + 4) * (L + 1) * k ≤ k * k := by
  44. calc 4 * (m + 4) * (L + 1) * k = 4 * (L + 1) * ((m + 4) * k) := by ring
  45. _ ≤ 4 * (L + 1) * (2 * (k + d)) := Nat.mul_le_mul_left _ hM1
  46. _ = 8 * (k + d) * (L + 1) := by ring
  47. _ ≤ k * k := hL
  48. exact Nat.le_of_mul_le_mul_right h1 hk
  49. -- the window is at least L long: 2 L (m+3)(m+2) ≤ (m+1) k
  50. have hW : 2 * (L * (m + 3) * (m + 2)) ≤ (m + 1) * k := by
  51. calc 2 * (L * (m + 3) * (m + 2))
  52. ≤ 2 * (L * (m + 3) * (m + 2))
  53. + (2 * L * m * m + 10 * L * m + 4 * L + 4 * m * m + 20 * m + 16) := Nat.le_add_right _ _
  54. _ = (m + 1) * (4 * (m + 4) * (L + 1)) := by ring
  55. _ ≤ (m + 1) * k := Nat.mul_le_mul_left _ hG
  56. have e3 : (m + 1) * k = k * m + k := by ring
  57. have e4 : k * (m + 2) = k * m + 2 * k := by ring
  58. have hkey : d + L * (m + 3) * (m + 2) ≤ k * (m + 2) := by linarith
  59. have hkey2 : (a + L) * (m + 3) * (m + 2) ≤ (k + d) * (m + 2) := by
  60. have h5 : a * (m + 2) * (m + 3) ≤ d * (m + 3) := Nat.mul_le_mul_right _ ha1
  61. have e5 : (a + L) * (m + 3) * (m + 2) = a * (m + 2) * (m + 3) + L * (m + 3) * (m + 2) := by ring
  62. have e6 : d * (m + 3) = d * (m + 2) + d := by ring
  63. have e7 : (k + d) * (m + 2) = k * (m + 2) + d * (m + 2) := by ring
  64. linarith
  65. have hlen : (a + L) * (m + 3) ≤ k + d := Nat.le_of_mul_le_mul_right hkey2 hNpos
  66. have hb : a + L ≤ k := by
  67. by_contra h
  68. push Not at h
  69. have h1 : (k + 1) * (m + 3) ≤ (a + L) * (m + 3) := Nat.mul_le_mul_right _ h
  70. have h3 : (k + 1) * (m + 3) = k * m + 3 * k + m + 3 := by ring
  71. linarith
  72. obtain ⟨p, hp, hap, hpL⟩ := hgap a h2a hb
  73. refine exc_no_window (N := m + 3) he hp (by omega) (by omega) ?_ ?_
  74. · calc (m + 3) * p ≤ (m + 3) * (a + L) := Nat.mul_le_mul_left _ hpL
  75. _ = (a + L) * (m + 3) := Nat.mul_comm _ _
  76. _ ≤ k + d := hlen
  77. · have h1 : (a + 1) * (m + 2) ≤ p * (m + 2) := Nat.mul_le_mul_right _ hap
  78. have e : (m + 3) * p = p * (m + 2) + p := by ring
  79. omega
  80. /-- The same statement with the hypothesis phrased through consecutive primes: if every prime `q < k`
  81. is followed by a prime at distance at most `L`, then there is no exception with `2k ≤ n` and
  82. `8 n (L + 1) ≤ k²`. -/
  83. theorem not_exc_of_prime_gap {n k L : ℕ} (h2 : 2 * k ≤ n) (hL : 8 * n * (L + 1) ≤ k * k)
  84. (hgap : ∀ q : ℕ, q.Prime → q < k → ∃ p : ℕ, p.Prime ∧ q < p ∧ p ≤ q + L) :
  85. (n, k) ∉ Exc := by
  86. intro he
  87. have hk : 0 < k := he.1
  88. have hk16 : 16 ≤ k := by
  89. by_contra h
  90. push Not at h
  91. have h1 : 8 * (2 * k) * 1 ≤ 8 * n * (L + 1) :=
  92. Nat.mul_le_mul (Nat.mul_le_mul_left 8 h2) (by omega)
  93. have h3 : k * k ≤ 15 * k := Nat.mul_le_mul_right k (by omega)
  94. omega
  95. have hL1 : 1 ≤ L := by
  96. obtain ⟨p, _, h2p, hpL⟩ := hgap 2 Nat.prime_two (by omega)
  97. omega
  98. refine not_exc_of_gap_two h2 hL ?_ he
  99. intro a ha haL
  100. have ha2 : 2 ≤ a := by omega
  101. have hq : (Nat.findGreatest Nat.Prime a).Prime := Nat.findGreatest_spec ha2 Nat.prime_two
  102. have hqa : Nat.findGreatest Nat.Prime a ≤ a := Nat.findGreatest_le a
  103. have hmax : ∀ r : ℕ, r.Prime → r ≤ a → r ≤ Nat.findGreatest Nat.Prime a :=
  104. fun r hr hra => Nat.le_findGreatest hra hr
  105. generalize Nat.findGreatest Nat.Prime a = q at hq hqa hmax
  106. obtain ⟨p, hp, hqp, hpq⟩ := hgap q hq (by omega)
  107. refine ⟨p, hp, ?_, by omega⟩
  108. by_contra h
  109. push Not at h
  110. have := hmax p hp h
  111. omega
  112. end Erdos1093
  113. #print axioms Erdos1093.not_exc_of_gap_two
  114. #print axioms Erdos1093.not_exc_of_prime_gap