Lean / Reader
Lean reader

Erdos317.lean

225 lines38 declarationsno sorryimports Mathlib
  1. import Mathlib
  2. open Finset
  3. namespace Erdos317Density
  4. def scale (n : ℕ) := (Icc 1 n).lcm id
  5. def signed (a : ℤ) := a = 0 ∨ a = 1 ∨ a = -1
  6. def weight (n k : ℕ) : ℕ := scale n / k
  7. def num (n : ℕ) (f : ℕ → ℤ) : ℤ := ∑ k ∈ Icc 1 n, f k*(weight n k : ℕ)
  8. def value (n : ℕ) (f : ℕ → ℤ) : ℚ := ∑ k ∈ Icc 1 n, (f k : ℚ)/k
  9. theorem scale_pos (n : ℕ) : 0 < scale n := by
  10. apply Nat.pos_of_ne_zero
  11. simp [scale, Finset.lcm_eq_zero_iff]
  12. theorem scale_mul (n : ℕ) (f : ℕ → ℤ) :
  13. (num n f : ℚ) = (scale n : ℚ) * value n f := by
  14. unfold num value
  15. simp only [Int.cast_sum, Int.cast_mul, Int.cast_natCast, weight]
  16. rw [Finset.mul_sum]
  17. apply Finset.sum_congr rfl
  18. intro k hk
  19. have hd : k ∣ scale n := Finset.dvd_lcm (f:=id) hk
  20. have hk0 : (k : ℚ) ≠ 0 := by exact_mod_cast (show k ≠ 0 by have := (Finset.mem_Icc.mp hk).1; omega)
  21. have he : (↑(scale n/k) : ℚ)*k = scale n := by exact_mod_cast Nat.div_mul_cancel hd
  22. field_simp
  23. linear_combination (f k : ℚ) * he
  24. theorem single {n p : ℕ} (hp : p.Prime) (hle : p ≤ n) (hlt : n < 2*p) (f : ℕ → ℤ) :
  25. (num n f : ZMod p) = (f p : ZMod p) * (↑(scale n/p) : ZMod p) := by
  26. unfold num
  27. simp only [Int.cast_sum, Int.cast_mul, Int.cast_natCast, weight]
  28. apply Finset.sum_eq_single p
  29. · intro j hj hjp
  30. obtain ⟨hj0,hjn⟩ := Finset.mem_Icc.mp hj
  31. have hnd : ¬p ∣ j := by
  32. rintro ⟨a,ha⟩
  33. rcases a with _|_|a
  34. · simp at ha; omega
  35. · simp at ha; contradiction
  36. · have : 2*p ≤ j := by rw [ha]; nlinarith
  37. omega
  38. have hd : j ∣ scale n := Finset.dvd_lcm (f:=id) hj
  39. have hpd : p ∣ scale n := Finset.dvd_lcm (f:=id) (Finset.mem_Icc.mpr ⟨hp.one_le,hle⟩)
  40. rw [← Nat.div_mul_cancel hd] at hpd
  41. rw [(ZMod.natCast_eq_zero_iff ..).mpr ((hp.dvd_mul.mp hpd).resolve_right hnd),mul_zero]
  42. · intro h
  43. exact (h (Finset.mem_Icc.mpr ⟨hp.one_le,hle⟩)).elim
  44. /-- Two projections onto the same three residues put a prime into a product of four factors. -/
  45. theorem collision {K : Type*} [CommRing K] (r u v w i j : K)
  46. (hi : i=0 ∨ i=1 ∨ i=-1) (hj : j=0 ∨ j=1 ∨ j=-1)
  47. (ha : u=i*w) (hb : v=j*(r*w)) : u*v*((r*u)^2-v^2)=0 := by
  48. subst u v
  49. rcases hi with h|h|h <;> rcases hj with h'|h'|h' <;> rw [h,h'] <;> ring
  50. theorem compare_num {n m p R : ℕ} (hp : p.Prime) (hn : p ≤ n) (hnm : n ≤ m)
  51. (hm : m < 2*p) (hR : scale m = R * scale n) (f g : ℕ → ℤ)
  52. (hf : signed (f p)) (hg : signed (g p)) :
  53. (p : ℤ) ∣ (num n f)*(num m g)*(((R : ℤ)*num n f)^2-(num m g)^2) := by
  54. apply (ZMod.intCast_zmod_eq_zero_iff_dvd ..).mp
  55. push_cast
  56. have hpn : p ∣ scale n := Finset.dvd_lcm (f:=id) (Finset.mem_Icc.mpr ⟨hp.one_le,hn⟩)
  57. have he : scale m/p=R*(scale n/p) := by
  58. rw [hR,Nat.mul_div_assoc _ hpn]
  59. apply collision (r:=(R : ZMod p)) (w:=(↑(scale n/p) : ZMod p)) (i:=(f p : ZMod p)) (j:=(g p : ZMod p))
  60. · rcases hf with h|h|h <;> simp [h]
  61. · rcases hg with h|h|h <;> simp [h]
  62. · exact single hp hn (by omega) f
  63. · rw [single hp (hn.trans hnm) hm g,he,Nat.cast_mul]
  64. def upperPrimes (n m : ℕ) := (Icc (m/2+1) n).filter Nat.Prime
  65. theorem compare_product {n m R : ℕ} (hnm : n ≤ m) (hR : scale m = R * scale n)
  66. (f g : ℕ → ℤ) (hf : ∀ k ∈ Icc 1 n, signed (f k)) (hg : ∀ k ∈ Icc 1 m, signed (g k)) :
  67. ((∏ p ∈ upperPrimes n m, p : ℕ) : ℤ) ∣
  68. (num n f)*(num m g)*(((R : ℤ)*num n f)^2-(num m g)^2) := by
  69. apply Int.natCast_dvd.mpr
  70. apply Finset.prod_primes_dvd
  71. · intro p hp
  72. exact (Finset.mem_filter.mp hp).2.prime
  73. · intro p hp
  74. obtain ⟨hb,hp⟩ := Finset.mem_filter.mp hp
  75. obtain ⟨hlo,hhi⟩ := Finset.mem_Icc.mp hb
  76. exact Int.natCast_dvd.mp (compare_num hp hhi hnm (by omega) hR f g
  77. (hf p (Finset.mem_Icc.mpr ⟨hp.one_le,hhi⟩))
  78. (hg p (Finset.mem_Icc.mpr ⟨hp.one_le,hhi.trans hnm⟩)))
  79. theorem strict_of_prime {n p : ℕ} (hp : p.Prime) (hle : p ≤ n) (hlt : n < 2*p)
  80. (hw : (↑(scale n/p) : ZMod p) ≠ 1 ∧ (↑(scale n/p) : ZMod p) ≠ -1)
  81. (f : ℕ → ℤ) (hf : signed (f p)) (hnz : value n f ≠ 0) : 1/(scale n : ℚ) < |value n f| := by
  82. letI : Fact p.Prime := ⟨hp⟩
  83. have he := single hp hle hlt f
  84. have hex : num n f ≠ 1 ∧ num n f ≠ -1 := by
  85. constructor <;> intro H <;> rw [H] at he <;>
  86. rcases hf with h|h|h <;> simp_all [eq_comm,neg_eq_iff_eq_neg]
  87. have hpos : (0 : ℚ) < scale n := by exact_mod_cast scale_pos n
  88. have hz : num n f ≠ 0 := by
  89. intro h
  90. have := scale_mul n f
  91. rw [h,Int.cast_zero] at this
  92. exact (mul_ne_zero hpos.ne' hnz) this.symm
  93. have htwo : 1 < |num n f| := by
  94. rcases le_total 0 (num n f) with h|h
  95. · rw [abs_of_nonneg h]; omega
  96. · rw [abs_of_nonpos h]; omega
  97. apply (div_lt_iff₀ hpos).mpr
  98. rw [← abs_of_pos hpos,← abs_mul,mul_comm,← scale_mul]
  99. exact_mod_cast htwo
  100. def fastScale : ℕ → ℕ
  101. | 0 => 1
  102. | n+1 => (n+1).lcm (fastScale n)
  103. theorem fastScale_eq (n : ℕ) : fastScale n = scale n := by
  104. induction n with
  105. | zero => simp [fastScale,scale]
  106. | succ n ih =>
  107. have h : Icc 1 (n+1) = insert (n+1) (Icc 1 n) := by
  108. ext k
  109. simp only [Finset.mem_Icc,Finset.mem_insert]
  110. omega
  111. unfold scale at ih ⊢
  112. rw [fastScale,ih,h,Finset.lcm_insert]
  113. rfl
  114. def hasCheck (n : ℕ) : Bool :=
  115. let L := fastScale n
  116. (List.range' (n/2+1) (n-n/2)).any fun p =>
  117. decide (p.Prime ∧ p ≤ n ∧ n < 2*p ∧ (L/p)%p ≠ 1 ∧ (L/p)%p ≠ p-1)
  118. set_option maxHeartbeats 0
  119. set_option maxRecDepth 200000
  120. private theorem block_5 : ∀ n ∈ Icc 5 29, hasCheck n = true := by decide +kernel
  121. private theorem block_30 : ∀ n ∈ Icc 30 54, hasCheck n = true := by decide +kernel
  122. private theorem block_55 : ∀ n ∈ Icc 55 79, hasCheck n = true := by decide +kernel
  123. private theorem block_80 : ∀ n ∈ Icc 80 104, hasCheck n = true := by decide +kernel
  124. private theorem block_105 : ∀ n ∈ Icc 105 129, hasCheck n = true := by decide +kernel
  125. private theorem block_130 : ∀ n ∈ Icc 130 154, hasCheck n = true := by decide +kernel
  126. private theorem block_155 : ∀ n ∈ Icc 155 179, hasCheck n = true := by decide +kernel
  127. private theorem block_180 : ∀ n ∈ Icc 180 204, hasCheck n = true := by decide +kernel
  128. private theorem block_205 : ∀ n ∈ Icc 205 229, hasCheck n = true := by decide +kernel
  129. private theorem block_230 : ∀ n ∈ Icc 230 254, hasCheck n = true := by decide +kernel
  130. private theorem block_255 : ∀ n ∈ Icc 255 279, hasCheck n = true := by decide +kernel
  131. private theorem block_280 : ∀ n ∈ Icc 280 304, hasCheck n = true := by decide +kernel
  132. private theorem block_305 : ∀ n ∈ Icc 305 329, hasCheck n = true := by decide +kernel
  133. private theorem block_330 : ∀ n ∈ Icc 330 354, hasCheck n = true := by decide +kernel
  134. private theorem block_355 : ∀ n ∈ Icc 355 379, hasCheck n = true := by decide +kernel
  135. private theorem block_380 : ∀ n ∈ Icc 380 404, hasCheck n = true := by decide +kernel
  136. private theorem block_405 : ∀ n ∈ Icc 405 429, hasCheck n = true := by decide +kernel
  137. private theorem block_430 : ∀ n ∈ Icc 430 454, hasCheck n = true := by decide +kernel
  138. private theorem block_455 : ∀ n ∈ Icc 455 479, hasCheck n = true := by decide +kernel
  139. private theorem block_480 : ∀ n ∈ Icc 480 500, hasCheck n = true := by decide +kernel
  140. theorem check_500 : ∀ n ∈ Icc 5 500, hasCheck n = true := by
  141. intro n hn
  142. obtain ⟨hlo,hhi⟩ := Finset.mem_Icc.mp hn
  143. by_cases h_29 : n ≤ 29
  144. · exact block_5 n (Finset.mem_Icc.mpr ⟨by omega,h_29⟩)
  145. by_cases h_54 : n ≤ 54
  146. · exact block_30 n (Finset.mem_Icc.mpr ⟨by omega,h_54⟩)
  147. by_cases h_79 : n ≤ 79
  148. · exact block_55 n (Finset.mem_Icc.mpr ⟨by omega,h_79⟩)
  149. by_cases h_104 : n ≤ 104
  150. · exact block_80 n (Finset.mem_Icc.mpr ⟨by omega,h_104⟩)
  151. by_cases h_129 : n ≤ 129
  152. · exact block_105 n (Finset.mem_Icc.mpr ⟨by omega,h_129⟩)
  153. by_cases h_154 : n ≤ 154
  154. · exact block_130 n (Finset.mem_Icc.mpr ⟨by omega,h_154⟩)
  155. by_cases h_179 : n ≤ 179
  156. · exact block_155 n (Finset.mem_Icc.mpr ⟨by omega,h_179⟩)
  157. by_cases h_204 : n ≤ 204
  158. · exact block_180 n (Finset.mem_Icc.mpr ⟨by omega,h_204⟩)
  159. by_cases h_229 : n ≤ 229
  160. · exact block_205 n (Finset.mem_Icc.mpr ⟨by omega,h_229⟩)
  161. by_cases h_254 : n ≤ 254
  162. · exact block_230 n (Finset.mem_Icc.mpr ⟨by omega,h_254⟩)
  163. by_cases h_279 : n ≤ 279
  164. · exact block_255 n (Finset.mem_Icc.mpr ⟨by omega,h_279⟩)
  165. by_cases h_304 : n ≤ 304
  166. · exact block_280 n (Finset.mem_Icc.mpr ⟨by omega,h_304⟩)
  167. by_cases h_329 : n ≤ 329
  168. · exact block_305 n (Finset.mem_Icc.mpr ⟨by omega,h_329⟩)
  169. by_cases h_354 : n ≤ 354
  170. · exact block_330 n (Finset.mem_Icc.mpr ⟨by omega,h_354⟩)
  171. by_cases h_379 : n ≤ 379
  172. · exact block_355 n (Finset.mem_Icc.mpr ⟨by omega,h_379⟩)
  173. by_cases h_404 : n ≤ 404
  174. · exact block_380 n (Finset.mem_Icc.mpr ⟨by omega,h_404⟩)
  175. by_cases h_429 : n ≤ 429
  176. · exact block_405 n (Finset.mem_Icc.mpr ⟨by omega,h_429⟩)
  177. by_cases h_454 : n ≤ 454
  178. · exact block_430 n (Finset.mem_Icc.mpr ⟨by omega,h_454⟩)
  179. by_cases h_479 : n ≤ 479
  180. · exact block_455 n (Finset.mem_Icc.mpr ⟨by omega,h_479⟩)
  181. exact block_480 n (Finset.mem_Icc.mpr ⟨by omega,hhi⟩)
  182. theorem strict_500 (n : ℕ) (hn : n ∈ Icc 5 500) (f : ℕ → ℤ)
  183. (hf : ∀ k ∈ Icc 1 n, signed (f k)) (hnz : value n f ≠ 0) : 1/(scale n : ℚ) < |value n f| := by
  184. have hc := check_500 n hn
  185. unfold hasCheck at hc
  186. obtain ⟨p,_,h⟩ := List.any_eq_true.mp hc
  187. obtain ⟨hp,hle,hlt,hplus,hminus⟩ := of_decide_eq_true h
  188. rw [fastScale_eq] at hplus hminus
  189. letI : Fact p.Prime := ⟨hp⟩
  190. apply strict_of_prime hp hle hlt _ f (hf p (Finset.mem_Icc.mpr ⟨hp.one_le,hle⟩)) hnz
  191. constructor
  192. · intro he
  193. apply hplus
  194. simpa [ZMod.val_natCast,ZMod.val_one'' hp.ne_one] using congrArg ZMod.val he
  195. · intro he
  196. apply hminus
  197. cases p with
  198. | zero => exact (Nat.not_prime_zero hp).elim
  199. | succ k => simpa [ZMod.val_natCast,ZMod.val_neg_one] using congrArg ZMod.val he
  200. #print axioms compare_num
  201. #print axioms compare_product
  202. #print axioms strict_500
  203. end Erdos317Density