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

Kwas/Erdos1094Count.lean

245 lines9 declarationsno sorryimports Kwas.Erdos1094Gap2

Lean 4 Web has Mathlib, so it can check files that import only Mathlib. This one also imports Kwas.Erdos1094Gap2, 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.Erdos1094Gap2
  2. /-! Erdős Problem 1094: a counting criterion (continues namespace Erdos1093).
  3. For an exception `(n, k)` the primes `p` in `(k/2, B]` all avoid the windows of `exc_no_window`. If
  4. `2 B² ≤ n + B (k + 1)`, two such primes never have the same quotient `n / p`, so there are at most
  5. `⌊2n/k⌋ - ⌊n/B⌋ + 1` of them. More primes than that in `(k/2, B]` exclude the exception. -/
  6. namespace Erdos1093
  7. /-- Two distinct primes of `(k/2, B]` have different quotients `n / p` when `(n, k)` is an exception and
  8. `2 B² ≤ n + B (k + 1)`. -/
  9. theorem exc_div_ne {n k B p p' : ℕ} (he : (n, k) ∈ Exc)
  10. (hn : B * (2 * B) ≤ n + B * (k + 1)) (hp : p.Prime) (hp' : p'.Prime)
  11. (h1 : k < 2 * p) (hlt : p < p') (h2' : p' ≤ B) (hB : B ≤ k) : n / p ≠ n / p' := by
  12. intro heq
  13. have hq1 : n / p' * p' ≤ n := Nat.div_mul_le_self n p'
  14. have hq2 : n < (n / p + 1) * p := by
  15. have := Nat.lt_succ_iff.2 (le_refl (n / p))
  16. exact (Nat.div_lt_iff_lt_mul hp.pos).1 this
  17. rw [heq] at hq2
  18. have hbad : n / p' * p' + k ≤ n + p' := by
  19. by_contra h
  20. push Not at h
  21. exact exc_no_window (N := n / p') he hp' (by omega) (by omega) hq1 h
  22. generalize n / p' = q at hq1 hq2 hbad
  23. obtain ⟨t, rfl⟩ : ∃ t, p' = p + 1 + t := ⟨p' - p - 1, by omega⟩
  24. have e1 : q * (p + 1 + t) = q * p + q + q * t := by ring
  25. have e2 : (q + 1) * p = q * p + p := by ring
  26. have he' : q + 1 + (k + 1) ≤ 2 * B := by omega
  27. have hf : (q + 1 + (k + 1)) * B ≤ 2 * B * B := Nat.mul_le_mul_right B he'
  28. have e3 : (q + 1 + (k + 1)) * B = (q + 1) * B + B * (k + 1) := by ring
  29. have e4 : 2 * B * B = B * (2 * B) := by ring
  30. have hg : (q + 1) * B ≤ n := by omega
  31. have hh : (q + 1) * p ≤ (q + 1) * B := Nat.mul_le_mul_left _ (by omega)
  32. omega
  33. /-- **Counting bound.** For an exception `(n, k)` and `B ≤ k` with `2 B² ≤ n + B (k + 1)`, the number of
  34. primes in `(k/2, B]` is at most `⌊2n/k⌋ + 1 - ⌊n/B⌋`. -/
  35. theorem exc_card_primes_le {n k B : ℕ} (he : (n, k) ∈ Exc) (hB : B ≤ k)
  36. (hn : B * (2 * B) ≤ n + B * (k + 1)) :
  37. ((Finset.Ioc (k / 2) B).filter Nat.Prime).card ≤ 2 * n / k + 1 - n / B := by
  38. have hk : 0 < k := he.1
  39. have hcard : (Finset.Icc (n / B) (2 * n / k)).card = 2 * n / k + 1 - n / B := Nat.card_Icc _ _
  40. rw [← hcard]
  41. apply Finset.card_le_card_of_injOn (fun p => n / p)
  42. · intro p hp
  43. simp only [Finset.coe_filter, Finset.mem_Ioc, Set.mem_ofPred_eq] at hp
  44. obtain ⟨⟨h1, h2⟩, hpr⟩ := hp
  45. have hp0 : 0 < p := hpr.pos
  46. simp only [Finset.coe_Icc, Set.mem_Icc]
  47. constructor
  48. · exact Nat.div_le_div_left h2 hp0
  49. · rw [Nat.le_div_iff_mul_le hk]
  50. have h3 : n / p * p ≤ n := Nat.div_mul_le_self n p
  51. have h4 : n / p * k ≤ n / p * (2 * p) := Nat.mul_le_mul_left _ (by omega)
  52. have e : n / p * (2 * p) = 2 * (n / p * p) := by ring
  53. omega
  54. · intro p hp p' hp' heq
  55. simp only [Finset.coe_filter, Finset.mem_Ioc, Set.mem_ofPred_eq] at hp hp'
  56. obtain ⟨⟨h1, h2⟩, hpr⟩ := hp
  57. obtain ⟨⟨h1', h2'⟩, hpr'⟩ := hp'
  58. have heq' : n / p = n / p' := heq
  59. by_contra hne
  60. rcases Nat.lt_or_gt_of_ne hne with hlt | hlt
  61. · exact exc_div_ne he hn hpr hpr' (by omega) hlt h2' hB heq'
  62. · exact exc_div_ne he hn hpr' hpr (by omega) hlt h2 hB heq'.symm
  63. /-- **Counting criterion.** If `B ≤ k`, `2 B² ≤ n + B (k + 1)` and the interval `(k/2, B]` contains more
  64. than `⌊2n/k⌋ + 1 - ⌊n/B⌋` primes, then `(n, k)` is not an exception of Erdős problem 1094. -/
  65. theorem not_exc_of_count {n k B : ℕ} (hB : B ≤ k) (hn : B * (2 * B) ≤ n + B * (k + 1))
  66. (hcount : 2 * n / k + 1 - n / B < ((Finset.Ioc (k / 2) B).filter Nat.Prime).card) :
  67. (n, k) ∉ Exc := by
  68. intro he
  69. have := exc_card_primes_le he hB hn
  70. omega
  71. /-- Two primes of `(k/2, B]` with the same quotient `n / p` are at distance at most `W` when `(n, k)` is an
  72. exception and `2 B ≤ k + 1 + (W + 1) ⌊n/B⌋`. -/
  73. theorem exc_same_quot_close {n k B W p p' : ℕ} (he : (n, k) ∈ Exc)
  74. (hW : 2 * B ≤ k + 1 + (W + 1) * (n / B)) (hp : p.Prime) (hp' : p'.Prime)
  75. (h1 : k < 2 * p) (hlt : p < p') (h2' : p' ≤ B) (hB : B ≤ k) (heq : n / p = n / p') :
  76. p' ≤ p + W := by
  77. have hq1 : n / p' * p' ≤ n := Nat.div_mul_le_self n p'
  78. have hq2 : n < (n / p + 1) * p := by
  79. have := Nat.lt_succ_iff.2 (le_refl (n / p))
  80. exact (Nat.div_lt_iff_lt_mul hp.pos).1 this
  81. rw [heq] at hq2
  82. have hbad : n / p' * p' + k ≤ n + p' := by
  83. by_contra h
  84. push Not at h
  85. exact exc_no_window (N := n / p') he hp' (by omega) (by omega) hq1 h
  86. have hq0 : n / B ≤ n / p' := Nat.div_le_div_left h2' hp'.pos
  87. generalize n / p' = q at hq1 hq2 hbad hq0
  88. generalize n / B = q0 at hW hq0
  89. by_contra hcon
  90. push Not at hcon
  91. obtain ⟨t, rfl⟩ : ∃ t, p' = p + W + 1 + t := ⟨p' - p - W - 1, by omega⟩
  92. have e1 : q * (p + W + 1 + t) = q * p + (W + 1) * q + q * t := by ring
  93. have e2 : (q + 1) * p = q * p + p := by ring
  94. have h3 : (W + 1) * q0 ≤ (W + 1) * q := Nat.mul_le_mul_left _ hq0
  95. omega
  96. /-- **Counting bound, general form.** For an exception `(n, k)`, `B ≤ k` and `W` with
  97. `2 B ≤ k + 1 + (W + 1) ⌊n/B⌋`, the number of primes in `(k/2, B]` is at most
  98. `(W + 1) (⌊2n/k⌋ + 1 - ⌊n/B⌋)`. -/
  99. theorem exc_card_primes_le_mul {n k B W : ℕ} (he : (n, k) ∈ Exc) (hB : B ≤ k)
  100. (hW : 2 * B ≤ k + 1 + (W + 1) * (n / B)) :
  101. ((Finset.Ioc (k / 2) B).filter Nat.Prime).card ≤ (W + 1) * (2 * n / k + 1 - n / B) := by
  102. have hk : 0 < k := he.1
  103. have hcard : (Finset.Icc (n / B) (2 * n / k)).card = 2 * n / k + 1 - n / B := Nat.card_Icc _ _
  104. rw [← hcard]
  105. set S := (Finset.Ioc (k / 2) B).filter Nat.Prime with hS
  106. have hmem : ∀ p ∈ S, k < 2 * p ∧ p ≤ B ∧ p.Prime := by
  107. intro p hp
  108. rw [hS, Finset.mem_filter, Finset.mem_Ioc] at hp
  109. exact ⟨by omega, hp.1.2, hp.2⟩
  110. have h1 : S.card ≤ (W + 1) * (S.image fun p => n / p).card := by
  111. apply Finset.card_le_mul_card_image
  112. intro q _
  113. rcases (S.filter fun p => n / p = q).eq_empty_or_nonempty with h0 | hne
  114. · rw [h0]; simp
  115. · have hsub : (S.filter fun p => n / p = q) ⊆
  116. Finset.Icc ((S.filter fun p => n / p = q).min' hne)
  117. ((S.filter fun p => n / p = q).min' hne + W) := by
  118. intro x hx
  119. have hmin := Finset.min'_le _ x hx
  120. have hamem := Finset.min'_mem (S.filter fun p => n / p = q) hne
  121. rw [Finset.mem_Icc]
  122. refine ⟨hmin, ?_⟩
  123. rcases Nat.eq_or_lt_of_le hmin with heq | hlt
  124. · omega
  125. · rw [Finset.mem_filter] at hx hamem
  126. obtain ⟨ha1, ha2, ha3⟩ := hmem _ hamem.1
  127. obtain ⟨hx1, hx2, hx3⟩ := hmem _ hx.1
  128. exact exc_same_quot_close he hW ha3 hx3 ha1 hlt hx2 hB (hamem.2.trans hx.2.symm)
  129. calc (S.filter fun p => n / p = q).card
  130. ≤ (Finset.Icc ((S.filter fun p => n / p = q).min' hne)
  131. ((S.filter fun p => n / p = q).min' hne + W)).card := Finset.card_le_card hsub
  132. _ = W + 1 := by rw [Nat.card_Icc]; omega
  133. have h2 : (S.image fun p => n / p).card ≤ (Finset.Icc (n / B) (2 * n / k)).card := by
  134. apply Finset.card_le_card
  135. intro q hq
  136. rw [Finset.mem_image] at hq
  137. obtain ⟨p, hp, rfl⟩ := hq
  138. obtain ⟨hp1, hp2, hp3⟩ := hmem p hp
  139. rw [Finset.mem_Icc]
  140. constructor
  141. · exact Nat.div_le_div_left hp2 hp3.pos
  142. · rw [Nat.le_div_iff_mul_le hk]
  143. have h3 : n / p * p ≤ n := Nat.div_mul_le_self n p
  144. have h4 : n / p * k ≤ n / p * (2 * p) := Nat.mul_le_mul_left _ (by omega)
  145. have e : n / p * (2 * p) = 2 * (n / p * p) := by ring
  146. omega
  147. calc S.card ≤ (W + 1) * (S.image fun p => n / p).card := h1
  148. _ ≤ (W + 1) * (Finset.Icc (n / B) (2 * n / k)).card := Nat.mul_le_mul_left _ h2
  149. /-- **Counting criterion, general form.** If `B ≤ k`, `2 B ≤ k + 1 + (W + 1) ⌊n/B⌋` and `(k/2, B]` contains
  150. more than `(W + 1) (⌊2n/k⌋ + 1 - ⌊n/B⌋)` primes, then `(n, k)` is not an exception of Erdős problem 1094. -/
  151. theorem not_exc_of_count_mul {n k B W : ℕ} (hB : B ≤ k) (hW : 2 * B ≤ k + 1 + (W + 1) * (n / B))
  152. (hcount : (W + 1) * (2 * n / k + 1 - n / B) < ((Finset.Ioc (k / 2) B).filter Nat.Prime).card) :
  153. (n, k) ∉ Exc := by
  154. intro he
  155. have := exc_card_primes_le_mul he hB hW
  156. omega
  157. /-- **Exact form.** For an exception `(n, k)` every prime `p` of `(k/2, k]` lies in the interval
  158. `(n/(q+1), (n-k)/(q-1)]` with `q = ⌊n/p⌋` (the complement of the windows of `exc_no_window`). -/
  159. theorem exc_prime_mem_bad {n k p : ℕ} (he : (n, k) ∈ Exc) (hp : p.Prime) (h1 : k < 2 * p) (h2 : p ≤ k) :
  160. 2 ≤ n / p ∧ n / (n / p + 1) < p ∧ p ≤ (n - k) / (n / p - 1) := by
  161. have hk : 0 < k := he.1
  162. have h2k : 2 * k ≤ n := he.2.1
  163. have hq1 : n / p * p ≤ n := Nat.div_mul_le_self n p
  164. have hq2 : n < (n / p + 1) * p := by
  165. have := Nat.lt_succ_iff.2 (le_refl (n / p))
  166. exact (Nat.div_lt_iff_lt_mul hp.pos).1 this
  167. have hbad : n / p * p + k ≤ n + p := by
  168. by_contra h
  169. push Not at h
  170. exact exc_no_window (N := n / p) he hp h1 h2 hq1 h
  171. have hq : 2 ≤ n / p := by
  172. rw [Nat.le_div_iff_mul_le hp.pos]; omega
  173. refine ⟨hq, ?_, ?_⟩
  174. · rw [Nat.div_lt_iff_lt_mul (by omega)]
  175. calc n < (n / p + 1) * p := hq2
  176. _ = p * (n / p + 1) := Nat.mul_comm _ _
  177. · rw [Nat.le_div_iff_mul_le (by omega)]
  178. generalize n / p = q at hq1 hq2 hbad hq
  179. obtain ⟨r, rfl⟩ : ∃ r, q = r + 2 := ⟨q - 2, by omega⟩
  180. have e1 : (r + 2) * p = p * (r + 2 - 1) + p := by
  181. have : r + 2 - 1 = r + 1 := by omega
  182. rw [this]; ring
  183. omega
  184. /-- **Counting bound, exact form.** For an exception `(n, k)` and `B ≤ k`, the number of primes in
  185. `(k/2, B]` is at most the number of integers in the intervals `(n/(q+1), (n-k)/(q-1)]`,
  186. `⌊n/B⌋ ≤ q ≤ ⌊2n/k⌋`. -/
  187. theorem exc_card_primes_le_sum {n k B : ℕ} (he : (n, k) ∈ Exc) (hB : B ≤ k) :
  188. ((Finset.Ioc (k / 2) B).filter Nat.Prime).card ≤
  189. ∑ q ∈ Finset.Icc (n / B) (2 * n / k), ((n - k) / (q - 1) - n / (q + 1)) := by
  190. have hk : 0 < k := he.1
  191. have hsub : (Finset.Ioc (k / 2) B).filter Nat.Prime ⊆
  192. (Finset.Icc (n / B) (2 * n / k)).biUnion fun q => Finset.Ioc (n / (q + 1)) ((n - k) / (q - 1)) := by
  193. intro p hp
  194. rw [Finset.mem_filter, Finset.mem_Ioc] at hp
  195. obtain ⟨⟨h1, h2⟩, hpr⟩ := hp
  196. obtain ⟨-, hb1, hb2⟩ := exc_prime_mem_bad he hpr (by omega) (by omega)
  197. rw [Finset.mem_biUnion]
  198. refine ⟨n / p, ?_, Finset.mem_Ioc.2 ⟨hb1, hb2⟩⟩
  199. rw [Finset.mem_Icc]
  200. constructor
  201. · exact Nat.div_le_div_left h2 hpr.pos
  202. · rw [Nat.le_div_iff_mul_le hk]
  203. have h3 : n / p * p ≤ n := Nat.div_mul_le_self n p
  204. have h4 : n / p * k ≤ n / p * (2 * p) := Nat.mul_le_mul_left _ (by omega)
  205. have e : n / p * (2 * p) = 2 * (n / p * p) := by ring
  206. omega
  207. calc ((Finset.Ioc (k / 2) B).filter Nat.Prime).card
  208. ≤ ((Finset.Icc (n / B) (2 * n / k)).biUnion fun q =>
  209. Finset.Ioc (n / (q + 1)) ((n - k) / (q - 1))).card := Finset.card_le_card hsub
  210. _ ≤ ∑ q ∈ Finset.Icc (n / B) (2 * n / k), (Finset.Ioc (n / (q + 1)) ((n - k) / (q - 1))).card :=
  211. Finset.card_biUnion_le
  212. _ = ∑ q ∈ Finset.Icc (n / B) (2 * n / k), ((n - k) / (q - 1) - n / (q + 1)) := by
  213. simp only [Nat.card_Ioc]
  214. /-- **Counting criterion, exact form.** If `B ≤ k` and `(k/2, B]` contains more primes than there are
  215. integers in the intervals `(n/(q+1), (n-k)/(q-1)]`, `⌊n/B⌋ ≤ q ≤ ⌊2n/k⌋`, then `(n, k)` is not an exception. -/
  216. theorem not_exc_of_count_sum {n k B : ℕ} (hB : B ≤ k)
  217. (hcount : ∑ q ∈ Finset.Icc (n / B) (2 * n / k), ((n - k) / (q - 1) - n / (q + 1)) <
  218. ((Finset.Ioc (k / 2) B).filter Nat.Prime).card) :
  219. (n, k) ∉ Exc := by
  220. intro he
  221. have := exc_card_primes_le_sum he hB
  222. omega
  223. end Erdos1093
  224. #print axioms Erdos1093.exc_card_primes_le
  225. #print axioms Erdos1093.not_exc_of_count
  226. #print axioms Erdos1093.exc_card_primes_le_mul
  227. #print axioms Erdos1093.not_exc_of_count_mul
  228. #print axioms Erdos1093.exc_prime_mem_bad
  229. #print axioms Erdos1093.exc_card_primes_le_sum
  230. #print axioms Erdos1093.not_exc_of_count_sum