Lean / Reader
Lean reader

Main.lean

4,334 lines149 declarationsno sorry
  1. import Mathlib
  2. /-!
  3. # Practical numbers and Egyptian fractions
  4. Let `c₀ = 14 / log 2`. We then have the following three results:
  5. * There are infinitely many `n` such that every `1 ≤ m ≤ n` is a sum of at most
  6. `c₀ (log log n)²` distinct divisors of `n`;
  7. * Every rational `a / b ∈ (0,1)` with `b` sufficiently large is the sum of at
  8. most `2 c₀ (log log b)²` distinct unit fractions;
  9. * For every sufficiently large `k` and every integer `b` with
  10. `2 ≤ b ≤ exp (exp (√(k / (2 c₀))))` there is a `k`-element set `A` of positive
  11. integers containing `b` with `∑_{n ∈ A} 1/n = 1`.
  12. These results provide bounds Erdős Problems 18, 293 and 304 respectively. The
  13. proofs were written down by ChatGPT and inspired by an earlier write-up of
  14. ChatGPT and posted by Liam Price, on the first of the problems mentioned above.
  15. https://www.overleaf.com/project/6a6209b061d71464999ab0e6
  16. All results have been formalized by Aristotle (aristotle-harmonic@harmonic.fun)
  17. https://github.com/Woett/ChatGPT-s-note-on-Erdos18/tree/main
  18. Lean version: leanprover/lean4:v4.28.0
  19. -/
  20. open Finset
  21. namespace SDS
  22. /-! ## The constant `c₀` and elementary `log log` facts -/
  23. /-- `c₀ = 14 / log 2`. -/
  24. noncomputable def c0 : ℝ := 14 / Real.log 2
  25. theorem c0_pos : 0 < c0 := by
  26. have : 0 < Real.log 2 := Real.log_pos (by norm_num)
  27. unfold c0; positivity
  28. /-- For `x > e^e` one has `log (log x) > 1`. -/
  29. theorem one_lt_loglog {x : ℝ} (hx : Real.exp (Real.exp 1) < x) : 1 < Real.log (Real.log x) := by
  30. have hx0 : 0 < Real.exp (Real.exp 1) := Real.exp_pos _
  31. have hlog : Real.exp 1 < Real.log x := by
  32. have := Real.log_lt_log hx0 hx
  33. rwa [Real.log_exp] at this
  34. have h1 : Real.log (Real.exp 1) < Real.log (Real.log x) :=
  35. Real.log_lt_log (Real.exp_pos 1) hlog
  36. rwa [Real.log_exp] at h1
  37. /-- `log ∘ log` is monotone above `e^e`. -/
  38. theorem loglog_le_loglog {x y : ℝ} (hx : Real.exp (Real.exp 1) < x) (hxy : x ≤ y) :
  39. Real.log (Real.log x) ≤ Real.log (Real.log y) := by
  40. have hx0 : (0:ℝ) < x := lt_trans (Real.exp_pos _) hx
  41. have hlogx : Real.exp 1 < Real.log x := by
  42. have := Real.log_lt_log (Real.exp_pos (Real.exp 1)) hx
  43. rwa [Real.log_exp] at this
  44. have h1 : Real.log x ≤ Real.log y := Real.log_le_log hx0 hxy
  45. exact Real.log_le_log (lt_trans (Real.exp_pos 1) hlogx) h1
  46. /-! ## A Chebyshev-type lower bound and primes in `(q, 4q]`
  47. Mathlib provides the upper bound `θ x ≤ x log 4` and the comparison
  48. `|ψ x - θ x| ≤ 2 √x log x`. Here we prove the matching lower bound
  49. `log (centralBinom m) ≤ ψ (2m)` and deduce that the interval `(q, 4q]`
  50. contains at least `q / log (4q)` primes for all large `q`.
  51. -/
  52. section Chebyshev
  53. open Filter Asymptotics ArithmeticFunction
  54. /-- `log` is little-o of `√·`. -/
  55. theorem log_isLittleO_sqrt : (fun x : ℝ => Real.log x) =o[atTop] Real.sqrt := by
  56. have h := isLittleO_log_rpow_atTop (r := 1/2) (by norm_num)
  57. refine h.congr' (by rfl) ?_
  58. filter_upwards with x
  59. rw [Real.sqrt_eq_rpow]
  60. theorem sqrt_mul_log_isLittleO :
  61. (fun x : ℝ => Real.sqrt x * Real.log (4 * x)) =o[atTop] (fun x => x) := by
  62. have h1 : (fun x : ℝ => Real.log (4 * x)) =o[atTop] Real.sqrt := by
  63. have h2 : (fun x : ℝ => Real.log (4 * x)) =ᶠ[atTop] (fun x => Real.log 4 + Real.log x) := by
  64. filter_upwards [eventually_gt_atTop (0:ℝ)] with x hx
  65. rw [Real.log_mul (by norm_num) (ne_of_gt hx)]
  66. refine Asymptotics.IsLittleO.congr' ?_ h2.symm (by rfl)
  67. refine Asymptotics.IsLittleO.add ?_ log_isLittleO_sqrt
  68. exact Asymptotics.isLittleO_const_left.mpr (Or.inr (by
  69. simpa using Filter.tendsto_abs_atTop_atTop.comp Real.tendsto_sqrt_atTop))
  70. have := (Asymptotics.isBigO_refl Real.sqrt atTop).mul_isLittleO h1
  71. refine this.congr' (by rfl) ?_
  72. filter_upwards [eventually_ge_atTop (0:ℝ)] with x hx
  73. simp [Real.mul_self_sqrt hx]
  74. theorem half_lt_log_two : (1:ℝ)/2 < Real.log 2 := by
  75. have hsq : Real.exp (1/2) * Real.exp (1/2) = Real.exp 1 := by
  76. rw [← Real.exp_add]; norm_num
  77. have he : Real.exp 1 < 4 := by linarith [Real.exp_one_lt_d9]
  78. have hpos : 0 < Real.exp (1/2 : ℝ) := Real.exp_pos _
  79. have h : Real.exp (1/2 : ℝ) < 2 := by nlinarith
  80. have := Real.log_lt_log hpos h
  81. rwa [Real.log_exp] at this
  82. theorem one_lt_log_four : (1:ℝ) < Real.log 4 := by
  83. have h4 : Real.log 4 = 2 * Real.log 2 := by
  84. rw [show (4:ℝ) = 2 ^ 2 by norm_num, Real.log_pow]; push_cast; ring
  85. have := half_lt_log_two
  86. linarith
  87. /-- The error terms in the Chebyshev bounds are eventually small. -/
  88. theorem eventually_error_small : ∀ᶠ x : ℝ in atTop,
  89. Real.log (2 * x) + 4 * Real.sqrt x * Real.log (4 * x) ≤ (Real.log 4 - 1) * x := by
  90. have hc : 0 < (Real.log 4 - 1) / 2 := by linarith [one_lt_log_four]
  91. have h1 : (fun x : ℝ => Real.log (2 * x)) =o[atTop] (fun x => x) := by
  92. have h2 : (fun x : ℝ => Real.log (2 * x)) =ᶠ[atTop] (fun x => Real.log 2 + Real.log x) := by
  93. filter_upwards [eventually_gt_atTop (0:ℝ)] with x hx
  94. rw [Real.log_mul (by norm_num) (ne_of_gt hx)]
  95. refine Asymptotics.IsLittleO.congr' ?_ h2.symm (by rfl)
  96. refine Asymptotics.IsLittleO.add ?_ Real.isLittleO_log_id_atTop
  97. exact Asymptotics.isLittleO_const_left.mpr (Or.inr (by
  98. simpa using Filter.tendsto_abs_atTop_atTop))
  99. have b1 := h1.bound hc
  100. have b2 := (sqrt_mul_log_isLittleO.const_mul_left (4:ℝ)).bound hc
  101. filter_upwards [b1, b2, eventually_ge_atTop (0:ℝ)] with x hx1 hx2 hx0
  102. rw [Real.norm_eq_abs, Real.norm_eq_abs, abs_of_nonneg hx0] at hx1
  103. rw [Real.norm_eq_abs, Real.norm_eq_abs, abs_of_nonneg hx0] at hx2
  104. have e1 : Real.log (2 * x) ≤ (Real.log 4 - 1) / 2 * x := le_trans (le_abs_self _) hx1
  105. have e2 : 4 * (Real.sqrt x * Real.log (4 * x)) ≤ (Real.log 4 - 1) / 2 * x :=
  106. le_trans (le_abs_self _) hx2
  107. linarith
  108. /-- The central binomial coefficient is at most `exp (ψ (2m))`. -/
  109. theorem log_centralBinom_le_psi {m : ℕ} (hm : 1 ≤ m) :
  110. Real.log (Nat.centralBinom m) ≤ Chebyshev.psi ((2 * m : ℕ) : ℝ) := by
  111. classical
  112. set N := Nat.centralBinom m with hNdef
  113. have hNpos : 0 < N := Nat.centralBinom_pos m
  114. have hchoose : N = (2 * m).choose m := rfl
  115. have hlog : Real.log N = ∑ p ∈ N.primeFactors, (N.factorization p : ℝ) * Real.log p := by
  116. conv_lhs => rw [show N = ∏ p ∈ N.primeFactors, p ^ N.factorization p from by
  117. rw [← Nat.support_factorization]; exact (Nat.factorization_prod_pow_eq_self hNpos.ne').symm]
  118. push_cast
  119. rw [Real.log_prod]
  120. · exact Finset.sum_congr rfl fun p hp => by rw [Real.log_pow]
  121. · intro p hp
  122. have : 0 < p := (Nat.prime_of_mem_primeFactors hp).pos
  123. positivity
  124. set S : Finset ℕ := N.primeFactors.biUnion
  125. (fun p => (Finset.Icc 1 (N.factorization p)).image (fun j => p ^ j)) with hSdef
  126. have hple : ∀ p ∈ N.primeFactors, p ^ N.factorization p ≤ 2 * m := by
  127. intro p hp
  128. rw [hchoose]
  129. exact Nat.pow_factorization_choose_le (by omega)
  130. have hsub : S ⊆ Finset.Ioc 0 (2 * m) := by
  131. intro n hn
  132. rw [hSdef, Finset.mem_biUnion] at hn
  133. obtain ⟨p, hp, hn⟩ := hn
  134. obtain ⟨j, hj, rfl⟩ := Finset.mem_image.mp hn
  135. rw [Finset.mem_Icc] at hj
  136. have hprime := Nat.prime_of_mem_primeFactors hp
  137. refine Finset.mem_Ioc.mpr ⟨Nat.pow_pos hprime.pos, ?_⟩
  138. calc p ^ j ≤ p ^ N.factorization p := Nat.pow_le_pow_right hprime.pos hj.2
  139. _ ≤ 2 * m := hple p hp
  140. have hdisj : (N.primeFactors : Set ℕ).PairwiseDisjoint
  141. (fun p => (Finset.Icc 1 (N.factorization p)).image (fun j => p ^ j)) := by
  142. intro p hp q hq hpq
  143. simp only [Function.onFun, Finset.disjoint_left]
  144. intro x hx hx'
  145. obtain ⟨i, hi, rfl⟩ := Finset.mem_image.mp hx
  146. obtain ⟨j, hj, hji⟩ := Finset.mem_image.mp hx'
  147. rw [Finset.mem_Icc] at hi hj
  148. have hpp := Nat.prime_of_mem_primeFactors hp
  149. have hqp := Nat.prime_of_mem_primeFactors hq
  150. have hdvd : p ∣ q ^ j := by
  151. rw [hji]
  152. exact dvd_pow_self p (by omega)
  153. exact hpq ((Nat.prime_dvd_prime_iff_eq hpp hqp).mp (hpp.dvd_of_dvd_pow hdvd))
  154. have hsum : ∑ n ∈ S, Λ n = Real.log N := by
  155. rw [hSdef, Finset.sum_biUnion hdisj, hlog]
  156. refine Finset.sum_congr rfl fun p hp => ?_
  157. have hprime := Nat.prime_of_mem_primeFactors hp
  158. rw [Finset.sum_image (by
  159. intro x _ y _ h
  160. exact Nat.pow_right_injective hprime.two_le h)]
  161. have hval : ∀ j ∈ Finset.Icc 1 (N.factorization p), Λ (p ^ j) = Real.log p := by
  162. intro j hj
  163. rw [Finset.mem_Icc] at hj
  164. rw [vonMangoldt_apply_pow (by omega), vonMangoldt_apply_prime hprime]
  165. rw [Finset.sum_congr rfl hval, Finset.sum_const, Nat.card_Icc]
  166. simp
  167. have hpsi : Chebyshev.psi ((2 * m : ℕ) : ℝ) = ∑ n ∈ Finset.Ioc 0 (2 * m), Λ n := by
  168. unfold Chebyshev.psi
  169. rw [Nat.floor_natCast]
  170. rw [hpsi, ← hsum]
  171. exact Finset.sum_le_sum_of_subset_of_nonneg hsub (fun i _ _ => vonMangoldt_nonneg)
  172. /-- Chebyshev's lower bound for `ψ`. -/
  173. theorem psi_lower {m : ℕ} (hm : 4 ≤ m) :
  174. (m : ℝ) * Real.log 4 - Real.log m ≤ Chebyshev.psi ((2 * m : ℕ) : ℝ) := by
  175. have h := Nat.four_pow_lt_mul_centralBinom m hm
  176. have hm0 : (0:ℝ) < (m:ℝ) := by positivity
  177. have hc0 : (0:ℝ) < (Nat.centralBinom m : ℝ) := by
  178. exact_mod_cast Nat.centralBinom_pos m
  179. have hcast : ((4:ℝ)) ^ m < (m:ℝ) * (Nat.centralBinom m : ℝ) := by exact_mod_cast h
  180. have hlog : Real.log ((4:ℝ) ^ m) ≤ Real.log ((m:ℝ) * (Nat.centralBinom m : ℝ)) :=
  181. Real.log_le_log (by positivity) hcast.le
  182. rw [Real.log_pow, Real.log_mul (ne_of_gt hm0) (ne_of_gt hc0)] at hlog
  183. have hkey := log_centralBinom_le_psi (show 1 ≤ m by omega)
  184. push_cast at hlog hkey ⊢
  185. linarith
  186. /-- Chebyshev's lower bound for `θ`. -/
  187. theorem theta_lower {m : ℕ} (hm : 4 ≤ m) :
  188. (m : ℝ) * Real.log 4 - Real.log m
  189. - 2 * Real.sqrt ((2 * m : ℕ) : ℝ) * Real.log ((2 * m : ℕ) : ℝ)
  190. ≤ Chebyshev.theta ((2 * m : ℕ) : ℝ) := by
  191. have h1 : (1:ℝ) ≤ ((2 * m : ℕ) : ℝ) := by
  192. have : (8:ℕ) ≤ 2 * m := by omega
  193. have : (8:ℝ) ≤ ((2*m : ℕ) : ℝ) := by exact_mod_cast this
  194. linarith
  195. have h2 := Chebyshev.abs_psi_sub_theta_le_sqrt_mul_log h1
  196. have h3 := psi_lower hm
  197. have h4 : Chebyshev.psi ((2*m:ℕ):ℝ) - Chebyshev.theta ((2*m:ℕ):ℝ)
  198. ≤ 2 * Real.sqrt ((2*m:ℕ):ℝ) * Real.log ((2*m:ℕ):ℝ) :=
  199. le_trans (le_abs_self _) h2
  200. linarith
  201. /-- **Primes in an interval.** -/
  202. theorem exists_q0 : ∃ q₀ : ℕ, 2 ≤ q₀ ∧ ∀ q : ℕ, q₀ ≤ q →
  203. (q : ℝ) / Real.log (4 * q) ≤ (((Finset.Ioc q (4 * q)).filter Nat.Prime).card : ℝ) := by
  204. classical
  205. obtain ⟨X, hX⟩ := Filter.eventually_atTop.mp eventually_error_small
  206. refine ⟨max 2 ⌈X⌉₊, le_max_left _ _, ?_⟩
  207. intro q hq
  208. have hq2 : 2 ≤ q := le_trans (le_max_left _ _) hq
  209. have hqX : X ≤ (q : ℝ) := by
  210. have h1 : ⌈X⌉₊ ≤ q := le_trans (le_max_right _ _) hq
  211. exact le_trans (Nat.le_ceil X) (by exact_mod_cast h1)
  212. have hq0 : (0:ℝ) < (q:ℝ) := by positivity
  213. have hlog4q : 0 < Real.log (4 * (q:ℝ)) := by
  214. refine Real.log_pos ?_
  215. have : (2:ℝ) ≤ (q:ℝ) := by exact_mod_cast hq2
  216. linarith
  217. -- the Chebyshev bounds
  218. have hthlow := theta_lower (m := 2 * q) (by omega)
  219. have hcast4 : ((2 * (2 * q) : ℕ) : ℝ) = 4 * (q:ℝ) := by push_cast; ring
  220. have hcast2 : ((2 * q : ℕ) : ℝ) = 2 * (q:ℝ) := by push_cast; ring
  221. rw [hcast4, hcast2] at hthlow
  222. have hsqrt : Real.sqrt (4 * (q:ℝ)) = 2 * Real.sqrt (q:ℝ) := by
  223. rw [show (4:ℝ) * (q:ℝ) = 2 ^ 2 * (q:ℝ) by ring, Real.sqrt_mul (by positivity),
  224. Real.sqrt_sq (by norm_num)]
  225. rw [hsqrt] at hthlow
  226. have hthup : Chebyshev.theta ((q : ℕ) : ℝ) ≤ Real.log 4 * (q:ℝ) :=
  227. Chebyshev.theta_le_log4_mul_x (by positivity)
  228. have herr := hX (q:ℝ) hqX
  229. have hdiff : (q:ℝ) ≤ Chebyshev.theta (4 * (q:ℝ)) - Chebyshev.theta ((q:ℕ):ℝ) := by
  230. nlinarith [hthlow, hthup, herr]
  231. -- identify the difference as a sum over primes in the interval
  232. set f : ℕ → ℝ := fun p => if p.Prime then Real.log p else 0 with hf
  233. have hth1 : Chebyshev.theta ((q:ℕ):ℝ) = ∑ p ∈ Finset.Ioc 0 q, f p := by
  234. unfold Chebyshev.theta
  235. rw [Nat.floor_natCast, Finset.sum_filter]
  236. have hth2 : Chebyshev.theta (4 * (q:ℝ)) = ∑ p ∈ Finset.Ioc 0 (4 * q), f p := by
  237. have : (4 : ℝ) * (q:ℝ) = ((4 * q : ℕ) : ℝ) := by push_cast; ring
  238. rw [this]
  239. unfold Chebyshev.theta
  240. rw [Nat.floor_natCast, Finset.sum_filter]
  241. have hsplit : (∑ p ∈ Finset.Ioc 0 q, f p) + (∑ p ∈ Finset.Ioc q (4 * q), f p)
  242. = ∑ p ∈ Finset.Ioc 0 (4 * q), f p :=
  243. Finset.sum_Ioc_consecutive f (Nat.zero_le q) (by omega)
  244. have hsum : Chebyshev.theta (4 * (q:ℝ)) - Chebyshev.theta ((q:ℕ):ℝ)
  245. = ∑ p ∈ Finset.Ioc q (4 * q), f p := by
  246. rw [hth1, hth2, ← hsplit]; ring
  247. -- bound the sum by the number of primes times log (4q)
  248. have hbound : (∑ p ∈ Finset.Ioc q (4 * q), f p)
  249. ≤ (((Finset.Ioc q (4 * q)).filter Nat.Prime).card : ℝ) * Real.log (4 * (q:ℝ)) := by
  250. have hrw : ∑ p ∈ Finset.Ioc q (4 * q), f p
  251. = ∑ p ∈ (Finset.Ioc q (4 * q)).filter Nat.Prime, Real.log p := by
  252. rw [Finset.sum_filter]
  253. rw [hrw]
  254. have hle : ∀ p ∈ (Finset.Ioc q (4 * q)).filter Nat.Prime,
  255. Real.log p ≤ Real.log (4 * (q:ℝ)) := by
  256. intro p hp
  257. rw [Finset.mem_filter, Finset.mem_Ioc] at hp
  258. have hp1 : (p:ℝ) ≤ 4 * (q:ℝ) := by
  259. have : (p : ℝ) ≤ ((4 * q : ℕ) : ℝ) := by exact_mod_cast hp.1.2
  260. rwa [show ((4 * q : ℕ) : ℝ) = 4 * (q:ℝ) by push_cast; ring] at this
  261. have hp0 : (0:ℝ) < p := by
  262. have := hp.2.pos
  263. exact_mod_cast this
  264. exact Real.log_le_log hp0 hp1
  265. calc ∑ p ∈ (Finset.Ioc q (4 * q)).filter Nat.Prime, Real.log p
  266. ≤ ∑ _p ∈ (Finset.Ioc q (4 * q)).filter Nat.Prime, Real.log (4 * (q:ℝ)) :=
  267. Finset.sum_le_sum hle
  268. _ = (((Finset.Ioc q (4 * q)).filter Nat.Prime).card : ℝ) * Real.log (4 * (q:ℝ)) := by
  269. rw [Finset.sum_const, nsmul_eq_mul]
  270. rw [div_le_iff₀ hlog4q]
  271. calc (q:ℝ) ≤ Chebyshev.theta (4 * (q:ℝ)) - Chebyshev.theta ((q:ℕ):ℝ) := hdiff
  272. _ = ∑ p ∈ Finset.Ioc q (4 * q), f p := hsum
  273. _ ≤ _ := hbound
  274. end Chebyshev
  275. /-! ## The parameters `Q(k)`, `t(k)` and the threshold `K₁` -/
  276. /-- `λ = log 2`. -/
  277. noncomputable def lam : ℝ := Real.log 2
  278. theorem lam_pos : 0 < lam := Real.log_pos (by norm_num)
  279. theorem lam_lt_one : lam < 1 := by
  280. have h2 : (2:ℝ) < Real.exp 1 := by linarith [Real.exp_one_gt_d9]
  281. have := Real.log_lt_log (by norm_num : (0:ℝ) < 2) h2
  282. rwa [Real.log_exp] at this
  283. theorem half_lt_lam : 1/2 < lam := half_lt_log_two
  284. /-- `Q(k) = ⌈k⁶ log k⌉` for `k ≥ 3`, and `1` otherwise. -/
  285. noncomputable def Qp (k : ℕ) : ℕ := if 3 ≤ k then ⌈(k:ℝ)^6 * Real.log k⌉₊ else 1
  286. /-- `a(k) = λ / (7 log k + 3 log log k)`. -/
  287. noncomputable def ap (k : ℕ) : ℝ := lam / (7 * Real.log k + 3 * Real.log (Real.log k))
  288. /-- `t(k) = ⌊a(k) k⌋` for `k ≥ 3`, and `0` otherwise. -/
  289. noncomputable def tp (k : ℕ) : ℕ := if 3 ≤ k then ⌊ap k * k⌋₊ else 0
  290. theorem Qp_pos (k : ℕ) : 0 < Qp k := by
  291. unfold Qp
  292. split
  293. · rename_i h
  294. have hk : (3:ℝ) ≤ (k:ℝ) := by exact_mod_cast h
  295. have : (1:ℝ) ≤ (k:ℝ)^6 * Real.log k := by
  296. have h1 : (1:ℝ) ≤ (k:ℝ)^6 := one_le_pow₀ (by linarith)
  297. have h2 : (1:ℝ) ≤ Real.log k := by
  298. have : Real.log 3 ≤ Real.log k := Real.log_le_log (by norm_num) hk
  299. have h3 : (1:ℝ) ≤ Real.log 3 := by
  300. have : Real.exp 1 < 3 := by
  301. have := Real.exp_one_lt_d9
  302. linarith
  303. have := Real.log_le_log (Real.exp_pos 1) (le_of_lt this)
  304. rwa [Real.log_exp] at this
  305. linarith
  306. nlinarith
  307. exact Nat.lt_of_lt_of_le Nat.zero_lt_one (Nat.one_le_ceil_iff.mpr (by linarith))
  308. · norm_num
  309. theorem one_le_log_of_three_le {k : ℕ} (hk : 3 ≤ k) : 1 ≤ Real.log k := by
  310. have hk3 : (3:ℝ) ≤ (k:ℝ) := by exact_mod_cast hk
  311. have h3 : (1:ℝ) ≤ Real.log 3 := by
  312. have he : Real.exp 1 < 3 := by linarith [Real.exp_one_lt_d9]
  313. have := Real.log_le_log (Real.exp_pos 1) (le_of_lt he)
  314. rwa [Real.log_exp] at this
  315. exact le_trans h3 (Real.log_le_log (by norm_num) hk3)
  316. theorem ap_pos {k : ℕ} (hk : 3 ≤ k) : 0 < ap k := by
  317. have h1 : 1 ≤ Real.log k := one_le_log_of_three_le hk
  318. have h2 : 0 ≤ Real.log (Real.log k) := Real.log_nonneg h1
  319. have hden : 0 < 7 * Real.log k + 3 * Real.log (Real.log k) := by linarith
  320. exact div_pos lam_pos hden
  321. theorem ap_mul_den {k : ℕ} (hk : 3 ≤ k) :
  322. ap k * (7 * Real.log k + 3 * Real.log (Real.log k)) = lam := by
  323. have h1 : 1 ≤ Real.log k := one_le_log_of_three_le hk
  324. have h2 : 0 ≤ Real.log (Real.log k) := Real.log_nonneg h1
  325. have hden : 0 < 7 * Real.log k + 3 * Real.log (Real.log k) := by linarith
  326. unfold ap
  327. field_simp
  328. theorem tp_le_ap_mul {k : ℕ} (hk : 3 ≤ k) : (tp k : ℝ) ≤ ap k * k := by
  329. have hap := ap_pos hk
  330. have hk0 : (0:ℝ) ≤ (k:ℝ) := by positivity
  331. unfold tp
  332. rw [if_pos hk]
  333. exact Nat.floor_le (by positivity)
  334. /-- `Q` is nondecreasing on integers `≥ 3`. -/
  335. theorem Qp_mono {j k : ℕ} (hj : 3 ≤ j) (hjk : j ≤ k) : Qp j ≤ Qp k := by
  336. have hk : 3 ≤ k := le_trans hj hjk
  337. unfold Qp
  338. rw [if_pos hj, if_pos hk]
  339. apply Nat.ceil_le_ceil
  340. have hlj : 1 ≤ Real.log j := one_le_log_of_three_le hj
  341. have hlk : Real.log j ≤ Real.log k := by
  342. have hj0 : (0:ℝ) < (j:ℝ) := by positivity
  343. exact Real.log_le_log hj0 (by exact_mod_cast hjk)
  344. have hpow : (j:ℝ) ^ 6 ≤ (k:ℝ) ^ 6 := by
  345. have : (j:ℝ) ≤ (k:ℝ) := by exact_mod_cast hjk
  346. have hj0 : (0:ℝ) ≤ (j:ℝ) := by positivity
  347. exact pow_le_pow_left₀ hj0 this 6
  348. have hj6 : (0:ℝ) ≤ (j:ℝ) ^ 6 := by positivity
  349. nlinarith
  350. theorem log_ge_three {k : ℕ} (hk : 21 ≤ k) : 3 ≤ Real.log k := by
  351. have he3 : Real.exp 3 < 21 := by
  352. have h1 : Real.exp 3 = Real.exp 1 * (Real.exp 1 * Real.exp 1) := by
  353. rw [← Real.exp_add, ← Real.exp_add]; norm_num
  354. have h2 : Real.exp 1 < 2.7182818286 := Real.exp_one_lt_d9
  355. have h3 : 0 < Real.exp 1 := Real.exp_pos 1
  356. rw [h1]
  357. nlinarith
  358. have hk21 : (21:ℝ) ≤ (k:ℝ) := by exact_mod_cast hk
  359. have : Real.exp 3 ≤ (k:ℝ) := le_of_lt (lt_of_lt_of_le he3 hk21)
  360. have := Real.log_le_log (Real.exp_pos 3) this
  361. rwa [Real.log_exp] at this
  362. theorem log_three_lt_log_eight : Real.log 8 ≤ 3 := by
  363. have h8 : Real.log 8 = 3 * Real.log 2 := by
  364. rw [show (8:ℝ) = 2 ^ 3 by norm_num, Real.log_pow]; push_cast; ring
  365. have := lam_lt_one
  366. unfold lam at this
  367. linarith
  368. /-- The package of parameter bounds valid for all large `k`. -/
  369. def K1P (k : ℕ) : Prop :=
  370. 3 ≤ Real.log k ∧
  371. 800 * (Real.log k) ^ 2 ≤ (k:ℝ) ∧
  372. 16 * ((k:ℝ) + 1) ≤ (k:ℝ) ^ 6 ∧
  373. 32 * (k:ℝ) ≤ (k:ℝ) ^ 6 ∧
  374. (k:ℝ) ^ 6 * Real.log k ≤ (Qp k : ℝ) ∧
  375. (Qp k : ℝ) ≤ 2 * (k:ℝ) ^ 6 * Real.log k ∧
  376. Real.log (4 * Qp k) ≤ 8 * Real.log k ∧
  377. Real.log (4 * Qp k) ≤ 7 * Real.log k + 3 * Real.log (Real.log k) ∧
  378. 1 / (20 * Real.log k) ≤ ap k ∧
  379. ap k ≤ 1 / (7 * Real.log k) ∧
  380. ap k ≤ 1 / 2 ∧
  381. 1 / (k:ℝ) ≤ (ap k) ^ 2 / 2 ∧
  382. 1 ≤ tp k ∧ tp k ≤ k
  383. /-- **Bounds for the parameters.** -/
  384. theorem exists_K1 : ∃ K₁ : ℕ, 3 ≤ K₁ ∧ ∀ k : ℕ, K₁ ≤ k → K1P k := by
  385. unfold K1P
  386. have hlogsqrt := log_isLittleO_sqrt
  387. have hlittle : (fun x : ℝ => Real.log x ^ 2) =o[Filter.atTop] (fun x : ℝ => x) := by
  388. have h := hlogsqrt.mul hlogsqrt
  389. refine h.congr' ?_ ?_
  390. · filter_upwards with x; ring
  391. · filter_upwards [Filter.eventually_ge_atTop (0:ℝ)] with x hx
  392. simp [Real.mul_self_sqrt hx]
  393. have hb := hlittle.bound (show (0:ℝ) < 1/800 by norm_num)
  394. obtain ⟨X, hX⟩ := Filter.eventually_atTop.mp hb
  395. refine ⟨max 21 ⌈X⌉₊, le_trans (by norm_num) (le_max_left _ _), ?_⟩
  396. intro k hk
  397. have hk21 : 21 ≤ k := le_trans (le_max_left _ _) hk
  398. have hk3 : 3 ≤ k := by omega
  399. have hkX : X ≤ (k:ℝ) := by
  400. have h1 : ⌈X⌉₊ ≤ k := le_trans (le_max_right _ _) hk
  401. exact le_trans (Nat.le_ceil X) (by exact_mod_cast h1)
  402. have hkR : (21:ℝ) ≤ (k:ℝ) := by exact_mod_cast hk21
  403. have hk0 : (0:ℝ) < (k:ℝ) := by linarith
  404. set u := Real.log k with hu
  405. have hu3 : 3 ≤ u := log_ge_three hk21
  406. have hu0 : (0:ℝ) < u := by linarith
  407. have hlogu0 : 0 ≤ Real.log u := Real.log_nonneg (by linarith)
  408. have hlogu : Real.log u ≤ u := by
  409. have := Real.log_le_sub_one_of_pos hu0
  410. linarith
  411. have hlog8 : Real.log 8 ≤ 3 := log_three_lt_log_eight
  412. -- a quadratic bound for `log k`
  413. have hii : 800 * u ^ 2 ≤ (k:ℝ) := by
  414. have hb1 := hX (k:ℝ) hkX
  415. rw [Real.norm_eq_abs, Real.norm_eq_abs, abs_of_nonneg (le_of_lt hk0)] at hb1
  416. have h2 : u ^ 2 ≤ |u ^ 2| := le_abs_self _
  417. have h3 : u ^ 2 ≤ 1/800 * (k:ℝ) := le_trans h2 hb1
  418. linarith
  419. -- powers of `k`
  420. have h5 : (32:ℝ) ≤ (k:ℝ) ^ 5 := by
  421. have h := pow_le_pow_left₀ (by norm_num : (0:ℝ) ≤ 2) (by linarith : (2:ℝ) ≤ (k:ℝ)) 5
  422. norm_num at h
  423. linarith
  424. have hk6 : 32 * (k:ℝ) ≤ (k:ℝ) ^ 6 := by
  425. have hrw : (k:ℝ) ^ 6 = (k:ℝ) ^ 5 * (k:ℝ) := by ring
  426. rw [hrw]
  427. exact mul_le_mul_of_nonneg_right h5 (by linarith)
  428. have hiii : 16 * ((k:ℝ) + 1) ≤ (k:ℝ) ^ 6 := by linarith
  429. -- two-sided bounds for `Q k`
  430. have hk6one : (1:ℝ) ≤ (k:ℝ) ^ 6 := one_le_pow₀ (by linarith)
  431. have hxge : (1:ℝ) ≤ (k:ℝ) ^ 6 * u := by nlinarith
  432. have hQdef : (Qp k : ℝ) = (⌈(k:ℝ) ^ 6 * u⌉₊ : ℝ) := by
  433. unfold Qp; rw [if_pos hk3]
  434. have hQlow : (k:ℝ) ^ 6 * u ≤ (Qp k : ℝ) := by
  435. rw [hQdef]; exact Nat.le_ceil _
  436. have hQhigh : (Qp k : ℝ) ≤ 2 * (k:ℝ) ^ 6 * u := by
  437. rw [hQdef]
  438. have h := Nat.ceil_lt_add_one (show (0:ℝ) ≤ (k:ℝ) ^ 6 * u by positivity)
  439. linarith
  440. -- logs of Q
  441. have hQpos : (0:ℝ) < (Qp k : ℝ) := by
  442. have := Qp_pos k
  443. exact_mod_cast this
  444. have hlogQ : Real.log (4 * (Qp k : ℝ)) ≤ Real.log 8 + 6 * u + Real.log u := by
  445. have h1 : (4:ℝ) * (Qp k : ℝ) ≤ 8 * (k:ℝ) ^ 6 * u := by linarith
  446. have h2 : Real.log ((4 : ℝ) * (Qp k : ℝ)) ≤ Real.log (8 * (k:ℝ) ^ 6 * u) :=
  447. Real.log_le_log (by positivity) h1
  448. have h3 : Real.log (8 * (k:ℝ) ^ 6 * u) = Real.log 8 + 6 * u + Real.log u := by
  449. rw [Real.log_mul (by positivity) (by linarith), Real.log_mul (by norm_num) (by positivity),
  450. Real.log_pow]
  451. push_cast
  452. ring
  453. linarith [h3 ▸ h2]
  454. have hvi : Real.log (4 * (Qp k : ℝ)) ≤ 8 * u := by linarith
  455. have hvii : Real.log (4 * (Qp k : ℝ)) ≤ 7 * u + 3 * Real.log u := by linarith
  456. -- the parameter a
  457. have hden2 : 7 * u + 3 * Real.log u ≤ 10 * u := by linarith
  458. have hdenpos : (0:ℝ) < 7 * u + 3 * Real.log u := by linarith
  459. have hapdef : ap k = lam / (7 * u + 3 * Real.log u) := rfl
  460. have hlam1 : (1:ℝ)/2 < lam := half_lt_lam
  461. have hlam2 : lam < 1 := lam_lt_one
  462. have hviii1 : 1 / (20 * u) ≤ ap k := by
  463. rw [hapdef, div_le_div_iff₀ (by positivity) hdenpos]
  464. nlinarith [mul_pos (sub_pos.mpr hlam1) hu0]
  465. have hviii2 : ap k ≤ 1 / (7 * u) := by
  466. rw [hapdef, div_le_div_iff₀ hdenpos (by positivity)]
  467. nlinarith [mul_pos (sub_pos.mpr hlam2) hu0]
  468. have hviii3 : ap k ≤ 1 / 2 := by
  469. have hstep : 1 / (7 * u) ≤ 1 / 2 := by
  470. rw [div_le_div_iff₀ (by positivity) (by norm_num)]
  471. linarith
  472. linarith
  473. -- a lower bound for `a k ^ 2`
  474. have hix : 1 / (k:ℝ) ≤ (ap k) ^ 2 / 2 := by
  475. have hpos20 : (0:ℝ) < 1 / (20 * u) := by positivity
  476. have h2 : (1 / (20 * u)) ^ 2 ≤ (ap k) ^ 2 := pow_le_pow_left₀ hpos20.le hviii1 2
  477. have h3 : (1 / (20 * u)) ^ 2 / 2 = 1 / (800 * u ^ 2) := by
  478. field_simp
  479. ring
  480. have h4 : 1 / (k:ℝ) ≤ 1 / (800 * u ^ 2) :=
  481. one_div_le_one_div_of_le (by positivity) hii
  482. linarith [h3 ▸ h4]
  483. -- `t k` lies between `1` and `k`
  484. have h40 : 40 * u ≤ (k:ℝ) := by nlinarith
  485. have hak2 : (2:ℝ) ≤ ap k * (k:ℝ) := by
  486. have h1 : (1 / (20 * u)) * (k:ℝ) ≤ ap k * (k:ℝ) :=
  487. mul_le_mul_of_nonneg_right hviii1 hk0.le
  488. have h2 : (2:ℝ) ≤ (1 / (20 * u)) * (k:ℝ) := by
  489. rw [div_mul_eq_mul_div, one_mul, le_div_iff₀ (by positivity)]
  490. linarith
  491. linarith
  492. have hapk_le : ap k * (k:ℝ) ≤ (k:ℝ) := by
  493. have h1 : ap k * (k:ℝ) ≤ (1/2) * (k:ℝ) := mul_le_mul_of_nonneg_right hviii3 hk0.le
  494. linarith
  495. have hx1 : 1 ≤ tp k := by
  496. have hfl : (1:ℕ) ≤ ⌊ap k * (k:ℝ)⌋₊ := (Nat.one_le_floor_iff _).mpr (by linarith)
  497. unfold tp; rw [if_pos hk3]; exact hfl
  498. have hx2 : tp k ≤ k := by
  499. have hfl : ⌊ap k * (k:ℝ)⌋₊ ≤ ⌊((k:ℕ):ℝ)⌋₊ := Nat.floor_le_floor hapk_le
  500. rw [Nat.floor_natCast] at hfl
  501. unfold tp; rw [if_pos hk3]; exact hfl
  502. exact ⟨hu3, hii, hiii, hk6, hQlow, hQhigh, hvi, hvii, hviii1, hviii2, hviii3, hix, hx1, hx2⟩
  503. /-! ## The potential function
  504. `F(u) = (7/(2λ)) u² + (3/λ)(u log u - u)` is convex with `F'(u) = (7u + 3 log u)/λ`,
  505. and each step of the recursion increases `F(u) + 2u` by at least `1`.
  506. -/
  507. /-- The potential `F`. -/
  508. noncomputable def Fpot (u : ℝ) : ℝ := (7 / (2 * lam)) * u ^ 2 + (3 / lam) * (u * Real.log u - u)
  509. theorem Fpot_nonneg {u : ℝ} (hu : 1 ≤ u) : 0 ≤ Fpot u := by
  510. have hl : 0 < lam := lam_pos
  511. have hlog : 0 ≤ Real.log u := Real.log_nonneg hu
  512. have h1 : 0 ≤ (7 / (2 * lam)) * u ^ 2 - (3 / lam) * u := by
  513. have h2 : (7 / (2 * lam)) * u ^ 2 - (3 / lam) * u = (u / lam) * (7 * u / 2 - 3) := by
  514. field_simp
  515. rw [h2]
  516. have h4 : (0:ℝ) ≤ 7 * u / 2 - 3 := by linarith
  517. have h5 : (0:ℝ) ≤ u / lam := by positivity
  518. exact mul_nonneg h5 h4
  519. have h3 : 0 ≤ (3 / lam) * (u * Real.log u) := by positivity
  520. unfold Fpot
  521. nlinarith
  522. /-- Convexity of `F`, in the form of the tangent-line inequality. -/
  523. theorem Fpot_tangent {u u' : ℝ} (hu : 1 ≤ u) (hle : u ≤ u') :
  524. (u' - u) * (7 * u + 3 * Real.log u) / lam ≤ Fpot u' - Fpot u := by
  525. have hl : 0 < lam := lam_pos
  526. have hu0 : 0 < u := by linarith
  527. have hu'0 : 0 < u' := by linarith
  528. -- key: u' * log (u'/u) ≥ u' - u
  529. have hkey : u' - u ≤ u' * (Real.log u' - Real.log u) := by
  530. rcases eq_or_lt_of_le hle with rfl | hlt
  531. · simp
  532. · have hr : 0 < u / u' := by positivity
  533. have h1 : Real.log (u / u') ≤ u / u' - 1 := Real.log_le_sub_one_of_pos hr
  534. rw [Real.log_div (ne_of_gt hu0) (ne_of_gt hu'0)] at h1
  535. have h2 : Real.log u' - Real.log u ≥ 1 - u / u' := by linarith
  536. have h3 : u' * (1 - u / u') = u' - u := by field_simp
  537. nlinarith
  538. have hexp : Fpot u' - Fpot u - (u' - u) * (7 * u + 3 * Real.log u) / lam
  539. = ((7 / 2) * (u' - u) ^ 2 + 3 * (u' * (Real.log u' - Real.log u) - (u' - u))) / lam := by
  540. unfold Fpot
  541. field_simp
  542. ring
  543. have hnn : 0 ≤ ((7:ℝ) / 2) * (u' - u) ^ 2 + 3 * (u' * (Real.log u' - Real.log u) - (u' - u)) := by
  544. nlinarith [sq_nonneg (u' - u)]
  545. have : 0 ≤ Fpot u' - Fpot u - (u' - u) * (7 * u + 3 * Real.log u) / lam := by
  546. rw [hexp]
  547. positivity
  548. linarith
  549. /-- `log (1 + x) ≥ x - x²` for `x ≥ 0`. -/
  550. theorem log_one_add_ge {x : ℝ} (hx : 0 ≤ x) : x - x ^ 2 ≤ Real.log (1 + x) := by
  551. have hpos : (0:ℝ) < 1 + x := by linarith
  552. have h1 : Real.log (1 / (1 + x)) ≤ 1 / (1 + x) - 1 :=
  553. Real.log_le_sub_one_of_pos (by positivity)
  554. rw [Real.log_div (by norm_num) (ne_of_gt hpos), Real.log_one] at h1
  555. have h2 : 1 - 1 / (1 + x) ≤ Real.log (1 + x) := by linarith
  556. have h3 : x - x ^ 2 ≤ 1 - 1 / (1 + x) := by
  557. have key : (1 - 1 / (1 + x)) - (x - x ^ 2) = x ^ 3 / (1 + x) := by
  558. field_simp
  559. ring
  560. have hnn : 0 ≤ x ^ 3 / (1 + x) := by positivity
  561. linarith
  562. linarith
  563. /-- One logarithmic increment. -/
  564. theorem delta_lower {k : ℕ} (hk : K1P k) :
  565. ap k - (3/2) * (ap k) ^ 2 ≤ Real.log ((k : ℝ) + tp k) - Real.log k := by
  566. obtain ⟨hu3, h800, -, -, -, -, -, -, hap1, hap2, hap3, hkinv, ht1, ht2⟩ := hk
  567. have hk0 : (0:ℝ) < (k:ℝ) := by nlinarith
  568. have hap0 : 0 < ap k := by
  569. have : (0:ℝ) < 1 / (20 * Real.log k) := by positivity
  570. linarith
  571. set x : ℝ := (tp k : ℝ) / (k:ℝ) with hx
  572. have hx0 : 0 ≤ x := by positivity
  573. have htfloor : (tp k : ℝ) ≤ ap k * (k:ℝ) := by
  574. unfold tp
  575. have hk3 : 3 ≤ k := by
  576. by_contra hcon
  577. push_neg at hcon
  578. have : (k:ℝ) < 3 := by exact_mod_cast hcon
  579. nlinarith
  580. rw [if_pos hk3]
  581. exact Nat.floor_le (by positivity)
  582. have htfloor2 : ap k * (k:ℝ) < (tp k : ℝ) + 1 := by
  583. unfold tp
  584. have hk3 : 3 ≤ k := by
  585. by_contra hcon
  586. push_neg at hcon
  587. have : (k:ℝ) < 3 := by exact_mod_cast hcon
  588. nlinarith
  589. rw [if_pos hk3]
  590. exact Nat.lt_floor_add_one _
  591. have hxle : x ≤ ap k := by
  592. rw [hx, div_le_iff₀ hk0]
  593. linarith
  594. have hxge : ap k - 1 / (k:ℝ) ≤ x := by
  595. rw [hx, le_div_iff₀ hk0]
  596. have : (ap k - 1 / (k:ℝ)) * (k:ℝ) = ap k * (k:ℝ) - 1 := by
  597. field_simp
  598. rw [this]
  599. linarith
  600. have hlogsplit : Real.log ((k:ℝ) + tp k) - Real.log k = Real.log (1 + x) := by
  601. rw [hx]
  602. rw [show (1:ℝ) + (tp k : ℝ) / (k:ℝ) = ((k:ℝ) + tp k) / (k:ℝ) by field_simp]
  603. rw [Real.log_div (by positivity) (ne_of_gt hk0)]
  604. rw [hlogsplit]
  605. have hmain : x - x ^ 2 ≤ Real.log (1 + x) := log_one_add_ge hx0
  606. have hx2 : x ^ 2 ≤ (ap k) ^ 2 := by
  607. apply pow_le_pow_left₀ hx0 hxle
  608. linarith
  609. /-- Each step increases the potential `F(u) + 2u` by at least `1`. -/
  610. theorem potential_step {k : ℕ} (hk : K1P k) :
  611. Fpot (Real.log k) + 2 * Real.log k + 1
  612. ≤ Fpot (Real.log ((k:ℝ) + tp k)) + 2 * Real.log ((k:ℝ) + tp k) := by
  613. have hkc := hk
  614. obtain ⟨hu3, h800, -, -, -, -, -, -, hap1, hap2, hap3, hkinv, ht1, ht2⟩ := hk
  615. have hk0 : (0:ℝ) < (k:ℝ) := by nlinarith
  616. set u := Real.log k with hu
  617. set u' := Real.log ((k:ℝ) + tp k) with hu'
  618. have hap0 : 0 < ap k := by
  619. have : (0:ℝ) < 1 / (20 * Real.log k) := by positivity
  620. linarith
  621. have hapsmall : ap k ≤ 1 / 21 := by
  622. have h1 : 1 / (7 * u) ≤ 1 / 21 := by
  623. rw [div_le_div_iff₀ (by positivity) (by norm_num)]
  624. linarith
  625. linarith
  626. have hδ := delta_lower hkc
  627. have huu' : u ≤ u' := by
  628. rw [hu, hu']
  629. apply Real.log_le_log hk0
  630. have : (1:ℝ) ≤ (tp k : ℝ) := by exact_mod_cast ht1
  631. linarith
  632. have htangent := Fpot_tangent (u := u) (u' := u') (by linarith) huu'
  633. -- rewrite the tangent slope in terms of `ap k`
  634. have hlogu0 : 0 ≤ Real.log u := Real.log_nonneg (by linarith)
  635. have hdenpos : (0:ℝ) < 7 * u + 3 * Real.log u := by linarith
  636. have hslope : (7 * u + 3 * Real.log u) / lam = 1 / ap k := by
  637. unfold ap
  638. rw [hu]
  639. field_simp
  640. have hδa : 1 - (3/2) * ap k ≤ (u' - u) / ap k := by
  641. rw [le_div_iff₀ hap0]
  642. nlinarith
  643. have hkey : (u' - u) * (7 * u + 3 * Real.log u) / lam = (u' - u) / ap k := by
  644. rw [mul_div_assoc, hslope]
  645. field_simp
  646. rw [hkey] at htangent
  647. have h2δ : 2 * ap k - 3 * (ap k) ^ 2 ≤ 2 * (u' - u) := by linarith
  648. nlinarith [sq_nonneg (ap k)]
  649. /-- **Counting the steps.** -/
  650. theorem step_count {k₀ : ℕ} (hK1 : ∀ k : ℕ, k₀ ≤ k → K1P k)
  651. (kseq : ℕ → ℕ) (h0 : k₀ ≤ kseq 0)
  652. (hrec : ∀ j, kseq (j + 1) = kseq j + tp (kseq j)) :
  653. ∀ j : ℕ, (j:ℝ) ≤ Fpot (Real.log (kseq j)) + 2 * Real.log (kseq j) := by
  654. have hmono : ∀ j, k₀ ≤ kseq j := by
  655. intro j
  656. induction j with
  657. | zero => exact h0
  658. | succ j ih =>
  659. rw [hrec j]
  660. omega
  661. intro j
  662. induction j with
  663. | zero =>
  664. have hk := hK1 (kseq 0) (hmono 0)
  665. have hu3 : 3 ≤ Real.log (kseq 0) := hk.1
  666. have := Fpot_nonneg (u := Real.log (kseq 0)) (by linarith)
  667. push_cast
  668. linarith
  669. | succ j ih =>
  670. have hk := hK1 (kseq j) (hmono j)
  671. have hstep := potential_step hk
  672. have hcast : ((kseq j : ℝ) + tp (kseq j)) = ((kseq (j+1) : ℕ) : ℝ) := by
  673. rw [hrec j]; push_cast; ring
  674. rw [hcast] at hstep
  675. push_cast
  676. push_cast at ih
  677. linarith
  678. /-- **Absorbing the lower-order terms.** -/
  679. theorem absorb (c D L : ℝ) (hc : 0 < c) (hL : 0 ≤ L) :
  680. ∃ U : ℝ, 1 ≤ U ∧ ∀ u H : ℝ, U ≤ u → u + Real.log u - L ≤ H →
  681. c * u ^ 2 + (6 * c / 7) * u * Real.log u + D * u + 1 ≤ c * H ^ 2 := by
  682. have hpos : 0 < 8 * c / 7 := by positivity
  683. set M : ℝ := max L ((2 * c * L + D + 1) / (8 * c / 7)) with hM
  684. have hM0 : 0 ≤ M := le_trans hL (le_max_left _ _)
  685. refine ⟨Real.exp M, Real.one_le_exp hM0, ?_⟩
  686. intro u H hu hH
  687. have hlogU : Real.log (Real.exp M) = M := Real.log_exp M
  688. have hu1 : 1 ≤ u := le_trans (Real.one_le_exp hM0) hu
  689. have hu0 : (0:ℝ) < u := by linarith
  690. have hMlogu : M ≤ Real.log u := by
  691. have := Real.log_le_log (Real.exp_pos M) hu
  692. rwa [hlogU] at this
  693. have hlogu : L ≤ Real.log u := le_trans (le_max_left _ _) hMlogu
  694. have hbracket : 2 * c * L + D + 1 ≤ (8 * c / 7) * Real.log u := by
  695. have h1 : (2 * c * L + D + 1) / (8 * c / 7) ≤ M := le_max_right _ _
  696. rw [div_le_iff₀ hpos] at h1
  697. nlinarith
  698. have h1 : 0 ≤ u + Real.log u - L := by linarith
  699. have hHu : u ≤ H := by linarith
  700. have hsq : (u + Real.log u - L) ^ 2 ≤ H ^ 2 := by nlinarith
  701. have hstep1 : c * (u + Real.log u - L) ^ 2 ≤ c * H ^ 2 := by nlinarith
  702. have hstep2 : c * u ^ 2 + 2 * c * u * (Real.log u - L) ≤ c * (u + Real.log u - L) ^ 2 := by
  703. nlinarith [mul_nonneg hc.le (sq_nonneg (Real.log u - L))]
  704. have hstep3 : c * u ^ 2 + (6 * c / 7) * u * Real.log u + D * u + 1
  705. ≤ c * u ^ 2 + 2 * c * u * (Real.log u - L) := by
  706. nlinarith [mul_nonneg (sub_nonneg.mpr hu1)
  707. (sub_nonneg.mpr (show (1:ℝ) ≤ (8 * c / 7) * Real.log u - 2 * c * L - D by linarith))]
  708. linarith
  709. /-! ## Squarefree integers, their divisors and differences
  710. The number of divisors, the splitting of the prime factors into two halves, and the
  711. bound for the number of large primes dividing the difference of two divisors.
  712. -/
  713. theorem three_le_of_mem_primeFactors {V p : ℕ} (hodd : Odd V) (hp : p ∈ V.primeFactors) :
  714. 3 ≤ p := by
  715. have hprime := Nat.prime_of_mem_primeFactors hp
  716. have hdvd := Nat.dvd_of_mem_primeFactors hp
  717. have hne2 : p ≠ 2 := by
  718. intro h
  719. rw [h] at hdvd
  720. rw [Nat.odd_iff] at hodd
  721. omega
  722. have := hprime.two_le
  723. omega
  724. theorem three_pow_le_self {V : ℕ} (hodd : Odd V) (hsq : Squarefree V) :
  725. 3 ^ V.primeFactors.card ≤ V := by
  726. conv_rhs => rw [← Nat.prod_primeFactors_of_squarefree hsq]
  727. calc 3 ^ V.primeFactors.card = ∏ _p ∈ V.primeFactors, 3 := by rw [Finset.prod_const]
  728. _ ≤ ∏ p ∈ V.primeFactors, p :=
  729. Finset.prod_le_prod' (fun p hp => three_le_of_mem_primeFactors hodd hp)
  730. theorem self_le_pow_of_primeFactors_le {V M : ℕ} (hsq : Squarefree V)
  731. (hle : ∀ p ∈ V.primeFactors, p ≤ M) : V ≤ M ^ V.primeFactors.card := by
  732. conv_lhs => rw [← Nat.prod_primeFactors_of_squarefree hsq]
  733. calc ∏ p ∈ V.primeFactors, p ≤ ∏ _p ∈ V.primeFactors, M := Finset.prod_le_prod' hle
  734. _ = M ^ V.primeFactors.card := by rw [Finset.prod_const]
  735. /-- A squarefree integer has exactly `2^ω` divisors. -/
  736. theorem card_divisors_squarefree {n : ℕ} (hn : n ≠ 0) (h : Squarefree n) :
  737. n.divisors.card = 2 ^ n.primeFactors.card := by
  738. rw [Nat.card_divisors hn]
  739. have hc : ∀ p ∈ n.primeFactors, n.factorization p + 1 = 2 := by
  740. intro p hp
  741. have h1 : n.factorization p ≤ 1 := Squarefree.natFactorization_le_one p h
  742. have h2 : 0 < n.factorization p := Nat.Prime.factorization_pos_of_dvd
  743. (Nat.prime_of_mem_primeFactors hp) hn (Nat.dvd_of_mem_primeFactors hp)
  744. omega
  745. rw [Finset.prod_congr rfl hc, Finset.prod_const]
  746. /-- **Splitting the prime factors.** -/
  747. theorem factor_split {V : ℕ} (hsq : Squarefree V) {k : ℕ}
  748. (hcard : V.primeFactors.card = k) :
  749. ∃ V₁ V₂ : ℕ, 0 < V₁ ∧ 0 < V₂ ∧ V₁ * V₂ = V ∧ Nat.Coprime V₁ V₂ ∧
  750. V₁.primeFactors.card = k/2 ∧ V₂.primeFactors.card = k - k/2 := by
  751. classical
  752. obtain ⟨S, hS, hScard⟩ : ∃ S ⊆ V.primeFactors, S.card = k/2 :=
  753. Finset.le_card_iff_exists_subset_card.mp (by omega)
  754. refine ⟨∏ p ∈ S, p, ∏ p ∈ V.primeFactors \ S, p, ?_, ?_, ?_, ?_, ?_, ?_⟩
  755. · exact Finset.prod_pos (fun p hp => (Nat.prime_of_mem_primeFactors (hS hp)).pos)
  756. · exact Finset.prod_pos (fun p hp =>
  757. (Nat.prime_of_mem_primeFactors (Finset.mem_sdiff.mp hp).1).pos)
  758. · rw [mul_comm, Finset.prod_sdiff hS, Nat.prod_primeFactors_of_squarefree hsq]
  759. · refine Nat.Coprime.prod_left (fun p hp => Nat.Coprime.prod_right (fun q hq => ?_))
  760. have hpp := Nat.prime_of_mem_primeFactors (hS hp)
  761. have hqq := Nat.prime_of_mem_primeFactors (Finset.mem_sdiff.mp hq).1
  762. have hne : p ≠ q := by
  763. rintro rfl
  764. exact (Finset.mem_sdiff.mp hq).2 hp
  765. exact (Nat.coprime_primes hpp hqq).mpr hne
  766. · rw [Nat.primeFactors_prod (fun p hp => Nat.prime_of_mem_primeFactors (hS hp)), hScard]
  767. · rw [Nat.primeFactors_prod (fun p hp =>
  768. Nat.prime_of_mem_primeFactors (Finset.mem_sdiff.mp hp).1),
  769. Finset.card_sdiff_of_subset hS, hScard, hcard]
  770. /-- **Differences of divisors**: at most `2k` primes larger than `Q` divide the difference
  771. of two distinct divisors of `V`. -/
  772. theorem prime_divisors_of_diff {V Q k : ℕ} (hsq : Squarefree V)
  773. (hcard : V.primeFactors.card = k) (hle : ∀ p ∈ V.primeFactors, p ≤ 4*Q) (hQ4 : 4 ≤ Q)
  774. {x y : ℕ} (hx : x ∈ V.divisors) (hy : y ∈ V.divisors) (hxy : x ≠ y)
  775. {P : Finset ℕ} (hP : ∀ p ∈ P, p.Prime ∧ Q < p) :
  776. (P.filter (fun p : ℕ => (p:ℤ) ∣ (x:ℤ) - (y:ℤ))).card ≤ 2*k := by
  777. classical
  778. set T := P.filter (fun p : ℕ => (p:ℤ) ∣ (x:ℤ) - (y:ℤ)) with hT
  779. have hV0 : V ≠ 0 := (Nat.mem_divisors.mp hx).2
  780. have hxdvd := (Nat.mem_divisors.mp hx).1
  781. have hydvd := (Nat.mem_divisors.mp hy).1
  782. have hxle : x ≤ V := Nat.le_of_dvd (Nat.pos_of_ne_zero hV0) hxdvd
  783. have hyle : y ≤ V := Nat.le_of_dvd (Nat.pos_of_ne_zero hV0) hydvd
  784. have hx0 : 0 < x := Nat.pos_of_dvd_of_pos hxdvd (Nat.pos_of_ne_zero hV0)
  785. have hy0 : 0 < y := Nat.pos_of_dvd_of_pos hydvd (Nat.pos_of_ne_zero hV0)
  786. set M := ((x:ℤ) - (y:ℤ)).natAbs with hM
  787. have hM0 : 0 < M := by
  788. rw [hM, Int.natAbs_pos]
  789. omega
  790. have hMlt : M < V := by
  791. rw [hM]
  792. omega
  793. have hdvd : ∀ p ∈ T, p ∣ M := by
  794. intro p hp
  795. have h := (Finset.mem_filter.mp hp).2
  796. have h2 := Int.natAbs_dvd_natAbs.mpr h
  797. simpa using h2
  798. have hprod : (∏ p ∈ T, p) ∣ M :=
  799. Finset.prod_primes_dvd M (fun p hp => ((hP p (Finset.mem_filter.mp hp).1).1).prime) hdvd
  800. have hQpow : Q ^ T.card ≤ ∏ p ∈ T, p := by
  801. calc Q ^ T.card = ∏ _p ∈ T, Q := by rw [Finset.prod_const]
  802. _ ≤ ∏ p ∈ T, p := Finset.prod_le_prod' (fun p hp =>
  803. le_of_lt (hP p (Finset.mem_filter.mp hp).1).2)
  804. have hVle : V ≤ (4*Q) ^ k := by
  805. have h := self_le_pow_of_primeFactors_le hsq hle
  806. rwa [hcard] at h
  807. have h4Q : (4*Q) ^ k ≤ Q ^ (2*k) := by
  808. have h1 : 4*Q ≤ Q * Q := by nlinarith
  809. calc (4*Q)^k ≤ (Q*Q)^k := Nat.pow_le_pow_left h1 k
  810. _ = Q ^ (2*k) := by rw [← pow_two, ← pow_mul]
  811. have hchain : Q ^ T.card < Q ^ (2*k) := by
  812. have h1 : ∏ p ∈ T, p ≤ M := Nat.le_of_dvd hM0 hprod
  813. omega
  814. have hQ1 : 1 < Q := by omega
  815. exact le_of_lt ((Nat.pow_lt_pow_iff_right hQ1).mp hchain)
  816. /-! ## Finite character sums
  817. Additive characters modulo `d`, the Fourier coefficients of a finite set of natural
  818. numbers, and the collision moments `M_d(X)`. The main result is `coverage`: a moment
  819. condition guaranteeing that every residue class modulo an odd `A > 1` is of the form
  820. `z₀ + 2 z₁ + 4 z₂ + 8 z₃` with `zᵢ` divisors of `V`.
  821. -/
  822. /-! ### Additive characters -/
  823. /-- A primitive `d`-th root of unity. -/
  824. noncomputable def zet (d : ℕ) : ℂ := Complex.exp (2 * Real.pi * Complex.I / d)
  825. /-- The additive character `z ↦ exp (2πi z / d)`. -/
  826. noncomputable def ee (d : ℕ) (z : ℤ) : ℂ := (zet d) ^ z
  827. theorem zet_ne_zero (d : ℕ) : zet d ≠ 0 := Complex.exp_ne_zero _
  828. theorem zet_norm (d : ℕ) : ‖zet d‖ = 1 := by
  829. rw [zet, Complex.norm_exp]; norm_num
  830. theorem ee_norm (d : ℕ) (z : ℤ) : ‖ee d z‖ = 1 := by
  831. simp [ee, norm_zpow, zet_norm]
  832. theorem ee_conj (d : ℕ) (z : ℤ) : (starRingEnd ℂ) (ee d z) = ee d (-z) := by
  833. have h1 : (starRingEnd ℂ) (zet d) = (zet d)⁻¹ := by rw [← Complex.inv_eq_conj (zet_norm d)]
  834. simp [ee, map_zpow₀, h1, zpow_neg]
  835. theorem ee_add (d : ℕ) (z w : ℤ) : ee d (z + w) = ee d z * ee d w :=
  836. zpow_add₀ (zet_ne_zero d) z w
  837. theorem ee_eq_one_iff {d : ℕ} (hd : d ≠ 0) (z : ℤ) : ee d z = 1 ↔ (d:ℤ) ∣ z :=
  838. (Complex.isPrimitiveRoot_exp d hd).zpow_eq_one_iff_dvd z
  839. /-- **Orthogonality.** -/
  840. theorem sum_ee {d : ℕ} (hd : d ≠ 0) (z : ℤ) :
  841. ∑ h ∈ range d, ee d (h * z) = if (d:ℤ) ∣ z then (d:ℂ) else 0 := by
  842. have hpow : ∀ h : ℕ, ee d (h * z) = (ee d z) ^ h := by
  843. intro h
  844. rw [ee, ee, ← zpow_natCast (zet d ^ z) h, ← zpow_mul]; ring_nf
  845. simp only [hpow]
  846. by_cases hdvd : (d:ℤ) ∣ z
  847. · have h1 : ee d z = 1 := (ee_eq_one_iff hd z).mpr hdvd
  848. simp [h1, hdvd]
  849. · have h1 : ee d z ≠ 1 := fun h => hdvd ((ee_eq_one_iff hd z).mp h)
  850. have h2 : (ee d z) ^ d = 1 := by
  851. rw [← ee_eq_one_iff hd ((d : ℤ) * z) |>.mpr ⟨z, rfl⟩, ee, ee,
  852. ← zpow_natCast (zet d ^ z) d, ← zpow_mul]
  853. ring_nf
  854. rw [geom_sum_eq h1, h2]; simp [hdvd]
  855. /-! ### Moments and Fourier coefficients -/
  856. /-- The collision moment `M_d(X)`. -/
  857. noncomputable def Mmom (X : Finset ℕ) (d : ℕ) : ℝ :=
  858. (((X ×ˢ X).filter (fun q => (d:ℤ) ∣ (q.1:ℤ) - (q.2:ℤ))).card : ℝ) / (X.card : ℝ) ^ 2
  859. /-- The Fourier coefficient `X̂_d(h)`. -/
  860. noncomputable def Xhat (X : Finset ℕ) (d : ℕ) (h : ℤ) : ℂ :=
  861. (X.card : ℂ)⁻¹ * ∑ x ∈ X, ee d (h * x)
  862. theorem Mmom_nonneg (X : Finset ℕ) (d : ℕ) : 0 ≤ Mmom X d := by
  863. unfold Mmom; positivity
  864. theorem norm_sq_Xhat (X : Finset ℕ) (d : ℕ) (h : ℤ) :
  865. ((‖Xhat X d h‖ : ℝ) : ℂ) ^ 2
  866. = (X.card : ℂ)⁻¹ * (X.card : ℂ)⁻¹ * ∑ q ∈ X ×ˢ X, ee d (h * ((q.1 : ℤ) - (q.2 : ℤ))) := by
  867. rw [← Complex.mul_conj' (Xhat X d h)]
  868. simp only [Xhat, map_mul, map_inv₀, Complex.conj_natCast, map_sum, ee_conj]
  869. rw [mul_mul_mul_comm, Finset.sum_product]
  870. congr 1
  871. rw [Finset.sum_mul_sum]
  872. refine Finset.sum_congr rfl fun x hx => Finset.sum_congr rfl fun y hy => ?_
  873. rw [← ee_add]
  874. ring_nf
  875. /-- **Plancherel identity.** -/
  876. theorem plancherel {X : Finset ℕ} (hX : X.Nonempty) {d : ℕ} (hd : d ≠ 0) :
  877. ∑ h ∈ range d, ‖Xhat X d h‖ ^ 2 = d * Mmom X d := by
  878. have hT0 : (X.card : ℝ) ≠ 0 := by
  879. have : 0 < X.card := Finset.card_pos.mpr hX
  880. positivity
  881. have hTC : (X.card : ℂ) ≠ 0 := by
  882. simpa using (Nat.cast_ne_zero (R := ℂ)).mpr (Finset.card_ne_zero.mpr hX)
  883. have key : ((∑ h ∈ range d, ‖Xhat X d h‖ ^ 2 : ℝ) : ℂ) = ((d * Mmom X d : ℝ) : ℂ) := by
  884. push_cast
  885. rw [Finset.sum_congr rfl (fun h (_ : h ∈ range d) => norm_sq_Xhat X d h), ← Finset.mul_sum,
  886. Finset.sum_comm]
  887. have hinner : ∀ q ∈ X ×ˢ X, (∑ h ∈ range d, ee d (h * ((q.1:ℤ) - (q.2:ℤ))))
  888. = if (d:ℤ) ∣ (q.1:ℤ) - (q.2:ℤ) then (d:ℂ) else 0 := fun q _ => sum_ee hd _
  889. rw [Finset.sum_congr rfl hinner, Finset.sum_ite, Finset.sum_const, Finset.sum_const_zero,
  890. Mmom]
  891. push_cast
  892. field_simp
  893. ring
  894. exact_mod_cast key
  895. /-! ### Residue classes -/
  896. /-- The number of elements of `X` in the residue class `b` modulo `d`. -/
  897. def resCount (X : Finset ℕ) (d b : ℕ) : ℕ := (X.filter (fun x => x % d = b)).card
  898. theorem sum_resCount_smul {M : Type*} [AddCommMonoid M] (X : Finset ℕ) {d : ℕ} (hd : d ≠ 0)
  899. (f : ℕ → M) : ∑ b ∈ range d, (resCount X d b) • f b = ∑ x ∈ X, f (x % d) := by
  900. classical
  901. rw [← Finset.sum_fiberwise_of_maps_to (t := range d) (g := fun x => x % d)
  902. (fun x _ => Finset.mem_range.mpr (Nat.mod_lt _ (Nat.pos_of_ne_zero hd)))
  903. (fun x => f (x % d))]
  904. refine Finset.sum_congr rfl fun b hb => ?_
  905. have : ∀ x ∈ X.filter (fun x => x % d = b), f (x % d) = f b := by
  906. intro x hx
  907. rw [(Finset.mem_filter.mp hx).2]
  908. rw [Finset.sum_congr rfl this, Finset.sum_const, resCount]
  909. theorem dvd_sub_iff_mod_eq {d x y : ℕ} : ((d:ℤ) ∣ (x:ℤ) - (y:ℤ)) ↔ x % d = y % d := by
  910. constructor
  911. · intro h
  912. have : (y:ℕ) ≡ x [MOD d] := (Nat.modEq_iff_dvd (n := d) (a := y) (b := x)).mpr (by
  913. simpa using h)
  914. exact this.symm
  915. · intro h
  916. have : (y:ℕ) ≡ x [MOD d] := h.symm
  917. have := (Nat.modEq_iff_dvd (n := d) (a := y) (b := x)).mp this
  918. simpa using this
  919. theorem card_collision_eq (X : Finset ℕ) {d : ℕ} (hd : d ≠ 0) :
  920. ((X ×ˢ X).filter (fun q => (d:ℤ) ∣ (q.1:ℤ) - (q.2:ℤ))).card
  921. = ∑ b ∈ range d, resCount X d b * resCount X d b := by
  922. classical
  923. have h1 : ((X ×ˢ X).filter (fun q => (d:ℤ) ∣ (q.1:ℤ) - (q.2:ℤ))).card
  924. = ∑ x ∈ X, ∑ y ∈ X, if x % d = y % d then 1 else 0 := by
  925. rw [Finset.card_filter, Finset.sum_product]
  926. refine Finset.sum_congr rfl fun x hx => Finset.sum_congr rfl fun y hy => ?_
  927. by_cases h : x % d = y % d
  928. · rw [if_pos h, if_pos (dvd_sub_iff_mod_eq.mpr h)]
  929. · rw [if_neg h, if_neg (fun hc => h (dvd_sub_iff_mod_eq.mp hc))]
  930. have h2 : ∀ x : ℕ, (∑ y ∈ X, if x % d = y % d then 1 else 0) = resCount X d (x % d) := by
  931. intro x
  932. rw [resCount, Finset.card_filter]
  933. refine Finset.sum_congr rfl fun y hy => ?_
  934. by_cases h : x % d = y % d
  935. · rw [if_pos h, if_pos h.symm]
  936. · rw [if_neg h, if_neg (fun hc => h hc.symm)]
  937. rw [h1, Finset.sum_congr rfl (fun x _ => h2 x)]
  938. rw [← sum_resCount_smul X hd (fun b => resCount X d b)]
  939. exact Finset.sum_congr rfl fun b _ => by simp [smul_eq_mul]
  940. theorem Mmom_eq_sum_sq (X : Finset ℕ) {d : ℕ} (hd : d ≠ 0) :
  941. Mmom X d = ∑ b ∈ range d, ((resCount X d b : ℝ) / X.card) ^ 2 := by
  942. rw [Mmom, card_collision_eq X hd]
  943. push_cast
  944. rw [Finset.sum_div]
  945. exact Finset.sum_congr rfl fun b _ => by ring
  946. /-! ### Invariance and permutations -/
  947. theorem ee_congr {d : ℕ} {z w : ℤ} (h : (d:ℤ) ∣ z - w) : ee d z = ee d w := by
  948. rcases Nat.eq_zero_or_pos d with rfl | hd
  949. · simp only [Nat.cast_zero, zero_dvd_iff, sub_eq_zero] at h
  950. rw [h]
  951. · have hd0 : d ≠ 0 := by omega
  952. have h1 : ee d (z - w) = 1 := (ee_eq_one_iff hd0 _).mpr h
  953. have h2 : ee d z = ee d (z - w) * ee d w := by rw [← ee_add]; ring_nf
  954. rw [h2, h1, one_mul]
  955. theorem Xhat_congr (X : Finset ℕ) {d : ℕ} {z w : ℤ} (h : (d:ℤ) ∣ z - w) :
  956. Xhat X d z = Xhat X d w := by
  957. unfold Xhat
  958. congr 1
  959. refine Finset.sum_congr rfl fun x hx => ee_congr ?_
  960. have : z * x - w * x = (z - w) * x := by ring
  961. rw [this]
  962. exact Dvd.dvd.mul_right h _
  963. theorem sum_range_unit_perm {M : Type*} [AddCommMonoid M] {d u : ℕ} (hd : d ≠ 0)
  964. (hu : Nat.Coprime u d) (G : ℕ → M) :
  965. ∑ b ∈ range d, G ((u * b) % d) = ∑ b ∈ range d, G b := by
  966. classical
  967. have hinj : Set.InjOn (fun b => (u * b) % d) ((range d : Finset ℕ) : Set ℕ) := by
  968. intro x hx y hy hxy
  969. simp only [Finset.coe_range, Set.mem_Iio] at hx hy
  970. have h1 : (u * x) ≡ (u * y) [MOD d] := hxy
  971. have h2 : x ≡ y [MOD d] :=
  972. Nat.ModEq.cancel_left_of_coprime (by simpa [Nat.Coprime, Nat.gcd_comm] using hu) h1
  973. exact Nat.ModEq.eq_of_lt_of_lt h2 hx hy
  974. have himage : (range d).image (fun b => (u * b) % d) = range d := by
  975. refine Finset.eq_of_subset_of_card_le ?_ ?_
  976. · intro y hy
  977. rw [Finset.mem_image] at hy
  978. obtain ⟨b, hb, rfl⟩ := hy
  979. exact Finset.mem_range.mpr (Nat.mod_lt _ (Nat.pos_of_ne_zero hd))
  980. · rw [Finset.card_image_of_injOn hinj]
  981. calc ∑ b ∈ range d, G ((u * b) % d)
  982. = ∑ y ∈ (range d).image (fun b => (u * b) % d), G y := (Finset.sum_image hinj).symm
  983. _ = ∑ b ∈ range d, G b := by rw [himage]
  984. /-- Plancherel along a dilated frequency. -/
  985. theorem plancherel_unit {X : Finset ℕ} (hX : X.Nonempty) {d u : ℕ} (hd : d ≠ 0)
  986. (hu : Nat.Coprime u d) :
  987. ∑ b ∈ range d, ‖Xhat X d (u * b)‖ ^ 2 = d * Mmom X d := by
  988. have hstep : ∀ b : ℕ, ‖Xhat X d (u * b)‖ ^ 2 = ‖Xhat X d (((u * b) % d : ℕ) : ℤ)‖ ^ 2 := by
  989. intro b
  990. congr 2
  991. refine Xhat_congr X ?_
  992. have hz : ((u : ℤ) * (b : ℤ)) = ((u * b : ℕ) : ℤ) := by push_cast; ring
  993. rw [hz]
  994. exact dvd_sub_iff_mod_eq.mpr (by simp)
  995. rw [Finset.sum_congr rfl (fun b _ => hstep b)]
  996. rw [sum_range_unit_perm hd hu (fun b => ‖Xhat X d b‖ ^ 2)]
  997. exact plancherel hX hd
  998. /-! ### The bilinear estimate -/
  999. theorem sum_divisors_mul_coprime {M : Type*} [AddCommMonoid M] {m n : ℕ} (hm : 0 < m) (hn : 0 < n)
  1000. (h : Nat.Coprime m n) (f : ℕ → M) :
  1001. ∑ q ∈ m.divisors ×ˢ n.divisors, f (q.1 * q.2) = ∑ x ∈ (m * n).divisors, f x := by
  1002. refine Finset.sum_nbij' (i := fun q => q.1 * q.2) (j := fun x => (Nat.gcd x m, Nat.gcd x n))
  1003. ?_ ?_ ?_ ?_ ?_
  1004. · rintro ⟨a, b⟩ hab
  1005. simp only [Finset.mem_product, Nat.mem_divisors] at hab ⊢
  1006. exact ⟨mul_dvd_mul hab.1.1 hab.2.1, by positivity⟩
  1007. · intro x hx
  1008. simp only [Nat.mem_divisors, Finset.mem_product] at hx ⊢
  1009. exact ⟨⟨Nat.gcd_dvd_right x m, hm.ne'⟩, ⟨Nat.gcd_dvd_right x n, hn.ne'⟩⟩
  1010. · rintro ⟨a, b⟩ hab
  1011. simp only [Finset.mem_product, Nat.mem_divisors] at hab
  1012. have h1 : Nat.gcd (a * b) m = a := by
  1013. have hb : Nat.Coprime b m := Nat.Coprime.coprime_dvd_left hab.2.1 h.symm
  1014. rw [Nat.Coprime.gcd_mul_right_cancel a hb]
  1015. exact Nat.gcd_eq_left hab.1.1
  1016. have h2 : Nat.gcd (a * b) n = b := by
  1017. have ha : Nat.Coprime a n := Nat.Coprime.coprime_dvd_left hab.1.1 h
  1018. rw [Nat.Coprime.gcd_mul_left_cancel b ha]
  1019. exact Nat.gcd_eq_left hab.2.1
  1020. simp [h1, h2]
  1021. · intro x hx
  1022. simp only [Nat.mem_divisors] at hx
  1023. show Nat.gcd x m * Nat.gcd x n = x
  1024. rw [← Nat.Coprime.gcd_mul x h]
  1025. exact Nat.gcd_eq_left hx.1
  1026. · intro q hq
  1027. rfl
  1028. theorem Xhat_mod (X : Finset ℕ) (d xi a : ℕ) :
  1029. Xhat X d ((xi : ℤ) * (a : ℤ)) = Xhat X d ((xi : ℤ) * ((a % d : ℕ) : ℤ)) := by
  1030. refine Xhat_congr X ?_
  1031. have hrw : (xi:ℤ) * (a:ℤ) - (xi:ℤ) * ((a % d : ℕ):ℤ)
  1032. = (xi:ℤ) * ((a:ℤ) - ((a % d : ℕ):ℤ)) := by ring
  1033. rw [hrw]
  1034. exact Dvd.dvd.mul_left (dvd_sub_iff_mod_eq.mpr (by simp)) _
  1035. /-- **Bilinear estimate.** -/
  1036. theorem bilinear {V₁ V₂ : ℕ} (hV₁ : 0 < V₁) (hV₂ : 0 < V₂) (hcop : Nat.Coprime V₁ V₂)
  1037. {d : ℕ} (hd : d ≠ 0) {xi : ℕ} (hxi : Nat.Coprime xi d) :
  1038. ‖Xhat (V₁ * V₂).divisors d xi‖ ^ 2
  1039. ≤ d * (Mmom V₁.divisors d * Mmom V₂.divisors d) := by
  1040. classical
  1041. set X₁ := V₁.divisors with hX₁
  1042. set X₂ := V₂.divisors with hX₂
  1043. have hX₁ne : X₁.Nonempty := ⟨1, Nat.one_mem_divisors.mpr hV₁.ne'⟩
  1044. have hX₂ne : X₂.Nonempty := ⟨1, Nat.one_mem_divisors.mpr hV₂.ne'⟩
  1045. have hT₁ : (0:ℝ) < (X₁.card : ℝ) := by
  1046. have := Finset.card_pos.mpr hX₁ne; exact_mod_cast this
  1047. have hT₂ : (0:ℝ) < (X₂.card : ℝ) := by
  1048. have := Finset.card_pos.mpr hX₂ne; exact_mod_cast this
  1049. have hT₁C : (X₁.card : ℂ) ≠ 0 := by
  1050. simpa using (Nat.cast_ne_zero (R := ℂ)).mpr (Finset.card_ne_zero.mpr hX₁ne)
  1051. have hT₂C : (X₂.card : ℂ) ≠ 0 := by
  1052. simpa using (Nat.cast_ne_zero (R := ℂ)).mpr (Finset.card_ne_zero.mpr hX₂ne)
  1053. -- the Fourier coefficient factors as an average
  1054. have hcard : ((V₁ * V₂).divisors.card : ℂ) = (X₁.card : ℂ) * (X₂.card : ℂ) := by
  1055. rw [hX₁, hX₂, ← Nat.cast_mul, Nat.Coprime.card_divisors_mul hcop]
  1056. have h1 : Xhat (V₁ * V₂).divisors d xi
  1057. = (X₁.card : ℂ)⁻¹ * ∑ a ∈ X₁, Xhat X₂ d ((xi : ℤ) * (a : ℤ)) := by
  1058. unfold Xhat
  1059. rw [hcard, ← sum_divisors_mul_coprime hV₁ hV₂ hcop (fun x => ee d ((xi:ℤ) * (x:ℤ))),
  1060. Finset.sum_product, Finset.mul_sum, Finset.mul_sum]
  1061. refine Finset.sum_congr rfl fun a ha => ?_
  1062. rw [Finset.mul_sum, Finset.mul_sum, Finset.mul_sum]
  1063. refine Finset.sum_congr rfl fun b hb => ?_
  1064. have hee : ee d ((xi:ℤ) * ((a * b : ℕ):ℤ)) = ee d ((xi:ℤ) * (a:ℤ) * (b:ℤ)) := by
  1065. congr 1; push_cast; ring
  1066. rw [hee, mul_inv]
  1067. ring
  1068. -- triangle inequality and grouping into residue classes
  1069. have h2 : ‖Xhat (V₁ * V₂).divisors d xi‖
  1070. ≤ ∑ b ∈ range d, ((resCount X₁ d b : ℝ) / X₁.card) * ‖Xhat X₂ d ((xi : ℤ) * (b : ℤ))‖ := by
  1071. rw [h1, norm_mul, norm_inv, Complex.norm_natCast]
  1072. have hstep : ‖∑ a ∈ X₁, Xhat X₂ d ((xi : ℤ) * (a : ℤ))‖
  1073. ≤ ∑ a ∈ X₁, ‖Xhat X₂ d ((xi : ℤ) * ((a % d : ℕ) : ℤ))‖ := by
  1074. refine le_trans (norm_sum_le _ _) (le_of_eq ?_)
  1075. exact Finset.sum_congr rfl fun a _ => by rw [Xhat_mod]
  1076. rw [← sum_resCount_smul X₁ hd (fun b => ‖Xhat X₂ d ((xi : ℤ) * (b : ℤ))‖)] at hstep
  1077. have hrw : ∑ b ∈ range d, ((resCount X₁ d b : ℝ) / X₁.card)
  1078. * ‖Xhat X₂ d ((xi : ℤ) * (b : ℤ))‖
  1079. = (X₁.card : ℝ)⁻¹ * ∑ b ∈ range d,
  1080. (resCount X₁ d b) • ‖Xhat X₂ d ((xi : ℤ) * (b : ℤ))‖ := by
  1081. rw [Finset.mul_sum]
  1082. refine Finset.sum_congr rfl fun b _ => ?_
  1083. rw [nsmul_eq_mul, div_eq_inv_mul, mul_assoc]
  1084. rw [hrw]
  1085. exact mul_le_mul_of_nonneg_left hstep (by positivity)
  1086. -- Cauchy-Schwarz
  1087. have hCS : (∑ b ∈ range d, ((resCount X₁ d b : ℝ) / X₁.card)
  1088. * ‖Xhat X₂ d ((xi : ℤ) * (b : ℤ))‖) ^ 2
  1089. ≤ (∑ b ∈ range d, ((resCount X₁ d b : ℝ) / X₁.card) ^ 2)
  1090. * ∑ b ∈ range d, ‖Xhat X₂ d ((xi : ℤ) * (b : ℤ))‖ ^ 2 :=
  1091. Finset.sum_mul_sq_le_sq_mul_sq _ _ _
  1092. have hM₁ : ∑ b ∈ range d, ((resCount X₁ d b : ℝ) / X₁.card) ^ 2 = Mmom X₁ d :=
  1093. (Mmom_eq_sum_sq X₁ hd).symm
  1094. have hM₂ : ∑ b ∈ range d, ‖Xhat X₂ d ((xi : ℤ) * (b : ℤ))‖ ^ 2 = d * Mmom X₂ d := by
  1095. have := plancherel_unit hX₂ne hd hxi
  1096. simpa using this
  1097. have hnn : 0 ≤ ∑ b ∈ range d, ((resCount X₁ d b : ℝ) / X₁.card)
  1098. * ‖Xhat X₂ d ((xi : ℤ) * (b : ℤ))‖ :=
  1099. Finset.sum_nonneg fun b _ => by positivity
  1100. calc ‖Xhat (V₁ * V₂).divisors d xi‖ ^ 2
  1101. ≤ (∑ b ∈ range d, ((resCount X₁ d b : ℝ) / X₁.card)
  1102. * ‖Xhat X₂ d ((xi : ℤ) * (b : ℤ))‖) ^ 2 := by
  1103. apply pow_le_pow_left₀ (norm_nonneg _) h2
  1104. _ ≤ Mmom X₁ d * ((d : ℝ) * Mmom X₂ d) := by rw [← hM₁, ← hM₂]; exact hCS
  1105. _ = d * (Mmom X₁ d * Mmom X₂ d) := by ring
  1106. /-! ### Four factors at one modulus -/
  1107. theorem bilinear' {V₁ V₂ : ℕ} (hV₁ : 0 < V₁) (hV₂ : 0 < V₂) (hcop : Nat.Coprime V₁ V₂)
  1108. {d : ℕ} (hd : d ≠ 0) {m xi : ℕ} (hxi : Nat.Coprime (m * xi) d) :
  1109. ‖Xhat (V₁ * V₂).divisors d ((m : ℤ) * (xi : ℤ))‖ ^ 2
  1110. ≤ d * (Mmom V₁.divisors d * Mmom V₂.divisors d) := by
  1111. have h := bilinear hV₁ hV₂ hcop hd hxi
  1112. have hcast : ((m * xi : ℕ) : ℤ) = (m : ℤ) * (xi : ℤ) := by push_cast; ring
  1113. rwa [hcast] at h
  1114. /-- **Four factors at one modulus.** -/
  1115. theorem four_factors {V₁ V₂ : ℕ} (hV₁ : 0 < V₁) (hV₂ : 0 < V₂) (hcop : Nat.Coprime V₁ V₂)
  1116. {d : ℕ} (hd1 : 1 < d) (hdodd : Odd d) :
  1117. ∑ xi ∈ (range d).filter (fun xi => Nat.Coprime xi d),
  1118. ‖Xhat (V₁ * V₂).divisors d ((1:ℤ) * (xi:ℤ))‖
  1119. * ‖Xhat (V₁ * V₂).divisors d ((2:ℤ) * (xi:ℤ))‖
  1120. * (‖Xhat (V₁ * V₂).divisors d ((4:ℤ) * (xi:ℤ))‖
  1121. * ‖Xhat (V₁ * V₂).divisors d ((8:ℤ) * (xi:ℤ))‖)
  1122. ≤ (d:ℝ) ^ 2 * (Mmom V₁.divisors d * Mmom V₂.divisors d
  1123. * Mmom (V₁ * V₂).divisors d) := by
  1124. classical
  1125. have hd : d ≠ 0 := by omega
  1126. set X := (V₁ * V₂).divisors with hX
  1127. have hXne : X.Nonempty := ⟨1, Nat.one_mem_divisors.mpr (by positivity)⟩
  1128. set M₁ := Mmom V₁.divisors d with hM₁
  1129. set M₂ := Mmom V₂.divisors d with hM₂
  1130. set M := Mmom X d with hM
  1131. have hM₁0 : 0 ≤ M₁ := Mmom_nonneg _ _
  1132. have hM₂0 : 0 ≤ M₂ := Mmom_nonneg _ _
  1133. have hM0 : 0 ≤ M := Mmom_nonneg _ _
  1134. have hd0 : (0:ℝ) ≤ (d:ℝ) := by positivity
  1135. have h2cop : Nat.Coprime 2 d := Nat.coprime_two_left.mpr hdodd
  1136. -- the first two factors are uniformly bounded
  1137. have hpair : ∀ xi ∈ (range d).filter (fun xi => Nat.Coprime xi d),
  1138. ‖Xhat X d ((1:ℤ) * (xi:ℤ))‖ * ‖Xhat X d ((2:ℤ) * (xi:ℤ))‖ ≤ (d:ℝ) * (M₁ * M₂) := by
  1139. intro xi hxi
  1140. have hxic : Nat.Coprime xi d := (Finset.mem_filter.mp hxi).2
  1141. have hb1 : ‖Xhat X d ((1:ℤ) * (xi:ℤ))‖ ^ 2 ≤ (d:ℝ) * (M₁ * M₂) :=
  1142. bilinear' hV₁ hV₂ hcop hd (m := 1) (by simpa using hxic)
  1143. have hb2 : ‖Xhat X d ((2:ℤ) * (xi:ℤ))‖ ^ 2 ≤ (d:ℝ) * (M₁ * M₂) :=
  1144. bilinear' hV₁ hV₂ hcop hd (m := 2) (Nat.Coprime.mul_left h2cop hxic)
  1145. have hnn1 : (0:ℝ) ≤ ‖Xhat X d ((1:ℤ) * (xi:ℤ))‖ := norm_nonneg _
  1146. have hnn2 : (0:ℝ) ≤ ‖Xhat X d ((2:ℤ) * (xi:ℤ))‖ := norm_nonneg _
  1147. nlinarith [sq_nonneg (‖Xhat X d ((1:ℤ) * (xi:ℤ))‖ - ‖Xhat X d ((2:ℤ) * (xi:ℤ))‖)]
  1148. -- Cauchy-Schwarz for the last two factors
  1149. have hCS : ∑ xi ∈ range d, ‖Xhat X d ((4:ℤ) * (xi:ℤ))‖ * ‖Xhat X d ((8:ℤ) * (xi:ℤ))‖
  1150. ≤ (d:ℝ) * M := by
  1151. have h1 := Finset.sum_mul_sq_le_sq_mul_sq (range d)
  1152. (fun xi => ‖Xhat X d ((4:ℤ) * (xi:ℤ))‖) (fun xi => ‖Xhat X d ((8:ℤ) * (xi:ℤ))‖)
  1153. have h4 : ∑ xi ∈ range d, ‖Xhat X d ((4:ℤ) * (xi:ℤ))‖ ^ 2 = (d:ℝ) * M := by
  1154. have := plancherel_unit (X := X) hXne hd (u := 4) (Nat.Coprime.mul_left h2cop h2cop)
  1155. simpa using this
  1156. have h8 : ∑ xi ∈ range d, ‖Xhat X d ((8:ℤ) * (xi:ℤ))‖ ^ 2 = (d:ℝ) * M := by
  1157. have := plancherel_unit (X := X) hXne hd (u := 8)
  1158. (Nat.Coprime.mul_left h2cop (Nat.Coprime.mul_left h2cop h2cop))
  1159. simpa using this
  1160. rw [h4, h8] at h1
  1161. have hnn : 0 ≤ ∑ xi ∈ range d, ‖Xhat X d ((4:ℤ) * (xi:ℤ))‖ * ‖Xhat X d ((8:ℤ) * (xi:ℤ))‖ :=
  1162. Finset.sum_nonneg fun xi _ => by positivity
  1163. nlinarith [mul_nonneg hd0 hM0]
  1164. calc ∑ xi ∈ (range d).filter (fun xi => Nat.Coprime xi d),
  1165. ‖Xhat X d ((1:ℤ) * (xi:ℤ))‖ * ‖Xhat X d ((2:ℤ) * (xi:ℤ))‖
  1166. * (‖Xhat X d ((4:ℤ) * (xi:ℤ))‖ * ‖Xhat X d ((8:ℤ) * (xi:ℤ))‖)
  1167. ≤ ∑ xi ∈ (range d).filter (fun xi => Nat.Coprime xi d),
  1168. ((d:ℝ) * (M₁ * M₂)) * (‖Xhat X d ((4:ℤ) * (xi:ℤ))‖ * ‖Xhat X d ((8:ℤ) * (xi:ℤ))‖) := by
  1169. refine Finset.sum_le_sum fun xi hxi => ?_
  1170. exact mul_le_mul_of_nonneg_right (hpair xi hxi) (by positivity)
  1171. _ = ((d:ℝ) * (M₁ * M₂)) * ∑ xi ∈ (range d).filter (fun xi => Nat.Coprime xi d),
  1172. (‖Xhat X d ((4:ℤ) * (xi:ℤ))‖ * ‖Xhat X d ((8:ℤ) * (xi:ℤ))‖) := by
  1173. rw [Finset.mul_sum]
  1174. _ ≤ ((d:ℝ) * (M₁ * M₂)) * ∑ xi ∈ range d,
  1175. (‖Xhat X d ((4:ℤ) * (xi:ℤ))‖ * ‖Xhat X d ((8:ℤ) * (xi:ℤ))‖) := by
  1176. refine mul_le_mul_of_nonneg_left ?_ (by positivity)
  1177. exact Finset.sum_le_sum_of_subset_of_nonneg (Finset.filter_subset _ _)
  1178. (fun i _ _ => by positivity)
  1179. _ ≤ ((d:ℝ) * (M₁ * M₂)) * ((d:ℝ) * M) := by
  1180. exact mul_le_mul_of_nonneg_left hCS (by positivity)
  1181. _ = (d:ℝ) ^ 2 * (M₁ * M₂ * M) := by ring
  1182. /-! ### Counting four-term representations -/
  1183. /-- The number of four-term representations of `c` modulo `A` by elements of `X`. -/
  1184. def Ncount (X : Finset ℕ) (A c : ℕ) : ℕ :=
  1185. ((X ×ˢ X ×ˢ X ×ˢ X).filter
  1186. (fun z => (z.1 + 2 * z.2.1 + 4 * z.2.2.1 + 8 * z.2.2.2) % A = c % A)).card
  1187. theorem sum_mul_sum_product {R α β : Type*} [CommRing R] (s : Finset α) (t : Finset β)
  1188. (f : α → R) (g : β → R) :
  1189. (∑ i ∈ s, f i) * (∑ j ∈ t, g j) = ∑ q ∈ s ×ˢ t, f q.1 * g q.2 := by
  1190. rw [Finset.sum_product, Finset.sum_mul_sum]
  1191. theorem sum_prod4 {R : Type*} [CommRing R] (X : Finset ℕ) (f₀ f₁ f₂ f₃ : ℕ → R) :
  1192. (∑ z ∈ X, f₀ z) * (∑ z ∈ X, f₁ z) * (∑ z ∈ X, f₂ z) * (∑ z ∈ X, f₃ z)
  1193. = ∑ q ∈ X ×ˢ X ×ˢ X ×ˢ X, f₀ q.1 * f₁ q.2.1 * f₂ q.2.2.1 * f₃ q.2.2.2 := by
  1194. calc (∑ z ∈ X, f₀ z) * (∑ z ∈ X, f₁ z) * (∑ z ∈ X, f₂ z) * (∑ z ∈ X, f₃ z)
  1195. = (∑ z ∈ X, f₀ z) * ((∑ z ∈ X, f₁ z) * ((∑ z ∈ X, f₂ z) * (∑ z ∈ X, f₃ z))) := by ring
  1196. _ = (∑ z ∈ X, f₀ z) * ((∑ z ∈ X, f₁ z) * ∑ r ∈ X ×ˢ X, f₂ r.1 * f₃ r.2) := by
  1197. rw [sum_mul_sum_product]
  1198. _ = (∑ z ∈ X, f₀ z) * ∑ p ∈ X ×ˢ X ×ˢ X, f₁ p.1 * (f₂ p.2.1 * f₃ p.2.2) := by
  1199. rw [sum_mul_sum_product]
  1200. _ = ∑ q ∈ X ×ˢ X ×ˢ X ×ˢ X, f₀ q.1 * (f₁ q.2.1 * (f₂ q.2.2.1 * f₃ q.2.2.2)) := by
  1201. rw [sum_mul_sum_product]
  1202. _ = ∑ q ∈ X ×ˢ X ×ˢ X ×ˢ X, f₀ q.1 * f₁ q.2.1 * f₂ q.2.2.1 * f₃ q.2.2.2 :=
  1203. Finset.sum_congr rfl fun q _ => by ring
  1204. /-- **Counting four-term representations.** -/
  1205. theorem four_count {X : Finset ℕ} (hX : X.Nonempty) {A : ℕ} (hA : A ≠ 0) (c : ℕ) :
  1206. ∑ h ∈ range A, ee A (-((h : ℤ) * (c : ℤ)))
  1207. * (Xhat X A ((1:ℤ) * (h:ℤ)) * Xhat X A ((2:ℤ) * (h:ℤ))
  1208. * Xhat X A ((4:ℤ) * (h:ℤ)) * Xhat X A ((8:ℤ) * (h:ℤ)))
  1209. = (A : ℂ) * (Ncount X A c : ℂ) / (X.card : ℂ) ^ 4 := by
  1210. classical
  1211. have hT : (X.card : ℂ) ≠ 0 := by
  1212. simpa using (Nat.cast_ne_zero (R := ℂ)).mpr (Finset.card_ne_zero.mpr hX)
  1213. have hterm : ∀ h ∈ range A, ee A (-((h : ℤ) * (c : ℤ)))
  1214. * (Xhat X A ((1:ℤ) * (h:ℤ)) * Xhat X A ((2:ℤ) * (h:ℤ))
  1215. * Xhat X A ((4:ℤ) * (h:ℤ)) * Xhat X A ((8:ℤ) * (h:ℤ)))
  1216. = ((X.card : ℂ)⁻¹) ^ 4 * ∑ q ∈ X ×ˢ X ×ˢ X ×ˢ X,
  1217. ee A ((h:ℤ) * (((q.1 + 2 * q.2.1 + 4 * q.2.2.1 + 8 * q.2.2.2 : ℕ) : ℤ) - (c:ℤ))) := by
  1218. intro h _
  1219. unfold Xhat
  1220. have hS : (∑ z ∈ X, ee A ((1:ℤ) * (h:ℤ) * (z:ℤ))) * (∑ z ∈ X, ee A ((2:ℤ) * (h:ℤ) * (z:ℤ)))
  1221. * (∑ z ∈ X, ee A ((4:ℤ) * (h:ℤ) * (z:ℤ))) * (∑ z ∈ X, ee A ((8:ℤ) * (h:ℤ) * (z:ℤ)))
  1222. = ∑ q ∈ X ×ˢ X ×ˢ X ×ˢ X,
  1223. ee A ((h:ℤ) * ((q.1 + 2 * q.2.1 + 4 * q.2.2.1 + 8 * q.2.2.2 : ℕ) : ℤ)) := by
  1224. rw [sum_prod4]
  1225. refine Finset.sum_congr rfl fun q _ => ?_
  1226. rw [← ee_add, ← ee_add, ← ee_add]
  1227. congr 1
  1228. push_cast
  1229. ring
  1230. calc ee A (-((h : ℤ) * (c : ℤ)))
  1231. * (((X.card : ℂ)⁻¹ * ∑ z ∈ X, ee A ((1:ℤ) * (h:ℤ) * (z:ℤ)))
  1232. * ((X.card : ℂ)⁻¹ * ∑ z ∈ X, ee A ((2:ℤ) * (h:ℤ) * (z:ℤ)))
  1233. * ((X.card : ℂ)⁻¹ * ∑ z ∈ X, ee A ((4:ℤ) * (h:ℤ) * (z:ℤ)))
  1234. * ((X.card : ℂ)⁻¹ * ∑ z ∈ X, ee A ((8:ℤ) * (h:ℤ) * (z:ℤ))))
  1235. = ((X.card : ℂ)⁻¹) ^ 4 * (ee A (-((h : ℤ) * (c : ℤ)))
  1236. * ((∑ z ∈ X, ee A ((1:ℤ) * (h:ℤ) * (z:ℤ)))
  1237. * (∑ z ∈ X, ee A ((2:ℤ) * (h:ℤ) * (z:ℤ)))
  1238. * (∑ z ∈ X, ee A ((4:ℤ) * (h:ℤ) * (z:ℤ)))
  1239. * (∑ z ∈ X, ee A ((8:ℤ) * (h:ℤ) * (z:ℤ))))) := by ring
  1240. _ = ((X.card : ℂ)⁻¹) ^ 4 * ∑ q ∈ X ×ˢ X ×ˢ X ×ˢ X,
  1241. ee A ((h:ℤ) * (((q.1 + 2 * q.2.1 + 4 * q.2.2.1 + 8 * q.2.2.2 : ℕ) : ℤ) - (c:ℤ))) := by
  1242. rw [hS, Finset.mul_sum]
  1243. congr 1
  1244. refine Finset.sum_congr rfl fun q _ => ?_
  1245. rw [← ee_add]
  1246. congr 1
  1247. ring
  1248. rw [Finset.sum_congr rfl hterm, ← Finset.mul_sum, Finset.sum_comm]
  1249. have hinner : ∀ q ∈ X ×ˢ X ×ˢ X ×ˢ X,
  1250. (∑ h ∈ range A,
  1251. ee A ((h:ℤ) * (((q.1 + 2 * q.2.1 + 4 * q.2.2.1 + 8 * q.2.2.2 : ℕ) : ℤ) - (c:ℤ))))
  1252. = if (A:ℤ) ∣ (((q.1 + 2 * q.2.1 + 4 * q.2.2.1 + 8 * q.2.2.2 : ℕ) : ℤ) - (c:ℤ))
  1253. then (A:ℂ) else 0 := fun q _ => sum_ee hA _
  1254. rw [Finset.sum_congr rfl hinner]
  1255. have hfilter : ((X ×ˢ X ×ˢ X ×ˢ X).filter
  1256. (fun q => (A:ℤ) ∣ (((q.1 + 2 * q.2.1 + 4 * q.2.2.1 + 8 * q.2.2.2 : ℕ) : ℤ) - (c:ℤ))))
  1257. = ((X ×ˢ X ×ˢ X ×ˢ X).filter
  1258. (fun z => (z.1 + 2 * z.2.1 + 4 * z.2.2.1 + 8 * z.2.2.2) % A = c % A)) := by
  1259. refine Finset.filter_congr fun q _ => ?_
  1260. constructor
  1261. · intro h; exact dvd_sub_iff_mod_eq.mp h
  1262. · intro h; exact dvd_sub_iff_mod_eq.mpr h
  1263. rw [Finset.sum_ite, Finset.sum_const, Finset.sum_const_zero, hfilter, ← Ncount]
  1264. field_simp
  1265. ring
  1266. /-! ### Grouping the frequencies by their conductor -/
  1267. theorem ee_conductor {A d g : ℕ} (hg : g ≠ 0) (hd : d ≠ 0) (hA : A = g * d) (z : ℤ) :
  1268. ee A ((g : ℤ) * z) = ee d z := by
  1269. have hzet : zet A ^ (g : ℕ) = zet d := by
  1270. rw [zet, zet, ← Complex.exp_nat_mul]
  1271. congr 1
  1272. have hg0 : (g : ℂ) ≠ 0 := Nat.cast_ne_zero.mpr hg
  1273. have hd0 : (d : ℂ) ≠ 0 := Nat.cast_ne_zero.mpr hd
  1274. rw [hA]
  1275. push_cast
  1276. field_simp
  1277. rw [ee, ee, ← hzet, ← zpow_natCast (zet A) g, ← zpow_mul]
  1278. theorem Xhat_conductor (X : Finset ℕ) {A d g : ℕ} (hg : g ≠ 0) (hd : d ≠ 0) (hA : A = g * d)
  1279. (m : ℤ) (xi : ℕ) : Xhat X A (m * ((g * xi : ℕ) : ℤ)) = Xhat X d (m * (xi:ℤ)) := by
  1280. unfold Xhat
  1281. congr 1
  1282. refine Finset.sum_congr rfl fun x hx => ?_
  1283. have hrw : m * ((g * xi : ℕ):ℤ) * (x:ℤ) = (g:ℤ) * (m * (xi:ℤ) * (x:ℤ)) := by
  1284. push_cast; ring
  1285. rw [hrw, ee_conductor hg hd hA]
  1286. /-- The four-factor sum over all nonzero frequencies, grouped by conductor. -/
  1287. theorem sum_by_conductor {V₁ V₂ : ℕ} (hV₁ : 0 < V₁) (hV₂ : 0 < V₂) (hcop : Nat.Coprime V₁ V₂)
  1288. {A : ℕ} (hA1 : 1 < A) (hAodd : Odd A) :
  1289. ∑ h ∈ (range A).erase 0,
  1290. (‖Xhat (V₁ * V₂).divisors A ((1:ℤ) * (h:ℤ))‖
  1291. * ‖Xhat (V₁ * V₂).divisors A ((2:ℤ) * (h:ℤ))‖
  1292. * (‖Xhat (V₁ * V₂).divisors A ((4:ℤ) * (h:ℤ))‖
  1293. * ‖Xhat (V₁ * V₂).divisors A ((8:ℤ) * (h:ℤ))‖))
  1294. ≤ ∑ d ∈ A.divisors.filter (fun d => 1 < d),
  1295. (d:ℝ) ^ 2 * (Mmom V₁.divisors d * Mmom V₂.divisors d
  1296. * Mmom (V₁ * V₂).divisors d) := by
  1297. classical
  1298. have hA0 : A ≠ 0 := by omega
  1299. set X := (V₁ * V₂).divisors with hXdef
  1300. set F : ℕ → ℝ := fun h => ‖Xhat X A ((1:ℤ) * (h:ℤ))‖ * ‖Xhat X A ((2:ℤ) * (h:ℤ))‖
  1301. * (‖Xhat X A ((4:ℤ) * (h:ℤ))‖ * ‖Xhat X A ((8:ℤ) * (h:ℤ))‖) with hF
  1302. have hmaps : ∀ h ∈ (range A).erase 0, A / Nat.gcd A h ∈ A.divisors.filter (fun d => 1 < d) := by
  1303. intro h hh
  1304. have hh0 : h ≠ 0 := (Finset.mem_erase.mp hh).1
  1305. have hhA : h < A := Finset.mem_range.mp (Finset.mem_erase.mp hh).2
  1306. have hgcddvd : Nat.gcd A h ∣ A := Nat.gcd_dvd_left A h
  1307. have hgcdpos : 0 < Nat.gcd A h := Nat.gcd_pos_of_pos_left _ (by omega)
  1308. have hgcdle : Nat.gcd A h ≤ h := Nat.le_of_dvd (by omega) (Nat.gcd_dvd_right A h)
  1309. refine Finset.mem_filter.mpr ⟨Nat.mem_divisors.mpr ⟨Nat.div_dvd_of_dvd hgcddvd, hA0⟩, ?_⟩
  1310. by_contra hcon
  1311. push_neg at hcon
  1312. have heq : A / Nat.gcd A h * Nat.gcd A h = A := Nat.div_mul_cancel hgcddvd
  1313. have hle : A ≤ Nat.gcd A h := by
  1314. calc A = A / Nat.gcd A h * Nat.gcd A h := heq.symm
  1315. _ ≤ 1 * Nat.gcd A h := Nat.mul_le_mul_right _ hcon
  1316. _ = Nat.gcd A h := one_mul _
  1317. omega
  1318. rw [← Finset.sum_fiberwise_of_maps_to hmaps F]
  1319. refine Finset.sum_le_sum fun d hd => ?_
  1320. have hdmem := Finset.mem_filter.mp hd
  1321. have hdA : d ∣ A := (Nat.mem_divisors.mp hdmem.1).1
  1322. have hd1 : 1 < d := hdmem.2
  1323. have hd0 : d ≠ 0 := by omega
  1324. have hdodd : Odd d := by
  1325. obtain ⟨k, hk⟩ := hdA
  1326. rw [hk, Nat.odd_mul] at hAodd
  1327. exact hAodd.1
  1328. set g := A / d with hgdef
  1329. have hgd : A = g * d := by
  1330. rw [hgdef]
  1331. exact (Nat.div_mul_cancel hdA).symm
  1332. have hg0 : g ≠ 0 := by
  1333. intro h0
  1334. rw [h0, zero_mul] at hgd
  1335. omega
  1336. -- every frequency in the fibre is `g` times a reduced frequency
  1337. have hfib : ∀ h ∈ (range A).erase 0, A / Nat.gcd A h = d →
  1338. (h / g ∈ (range d).filter (fun xi => Nat.Coprime xi d) ∧ h = g * (h / g)) := by
  1339. intro h hh hgc
  1340. have hh0 : h ≠ 0 := (Finset.mem_erase.mp hh).1
  1341. have hhA : h < A := Finset.mem_range.mp (Finset.mem_erase.mp hh).2
  1342. have hgcddvd : Nat.gcd A h ∣ A := Nat.gcd_dvd_left A h
  1343. have hgcdpos : 0 < Nat.gcd A h := Nat.gcd_pos_of_pos_left _ (by omega)
  1344. have hgg : Nat.gcd A h = g := by
  1345. rw [hgdef, ← hgc]
  1346. exact (Nat.div_div_self hgcddvd hA0).symm
  1347. have hdvdh : g ∣ h := hgg ▸ Nat.gcd_dvd_right A h
  1348. have hhg : h = g * (h / g) := (Nat.mul_div_cancel' hdvdh).symm
  1349. refine ⟨Finset.mem_filter.mpr ⟨Finset.mem_range.mpr ?_, ?_⟩, hhg⟩
  1350. · have hlt : g * (h / g) < g * d := by rw [← hhg, ← hgd]; exact hhA
  1351. exact lt_of_mul_lt_mul_left hlt (Nat.zero_le g)
  1352. · have hcopd := Nat.coprime_div_gcd_div_gcd (m := A) (n := h) hgcdpos
  1353. rw [hgc, hgg] at hcopd
  1354. exact hcopd.symm
  1355. -- the injection into the reduced frequencies
  1356. have hinj : Set.InjOn (fun h => h / g)
  1357. (((range A).erase 0).filter (fun h => A / Nat.gcd A h = d) : Finset ℕ) := by
  1358. intro x hx y hy hxy
  1359. simp only [Finset.coe_filter, Set.mem_setOf_eq] at hx hy
  1360. have hx' := hfib x hx.1 hx.2
  1361. have hy' := hfib y hy.1 hy.2
  1362. have hxy' : x / g = y / g := hxy
  1363. rw [hx'.2, hy'.2, hxy']
  1364. have hterm : ∀ h ∈ ((range A).erase 0).filter (fun h => A / Nat.gcd A h = d),
  1365. F h = ‖Xhat X d ((1:ℤ) * ((h / g : ℕ):ℤ))‖ * ‖Xhat X d ((2:ℤ) * ((h / g : ℕ):ℤ))‖
  1366. * (‖Xhat X d ((4:ℤ) * ((h / g : ℕ):ℤ))‖ * ‖Xhat X d ((8:ℤ) * ((h / g : ℕ):ℤ))‖) := by
  1367. intro h hh
  1368. have hmem := Finset.mem_filter.mp hh
  1369. obtain ⟨-, hhg⟩ := hfib h hmem.1 hmem.2
  1370. have hconv : ∀ m : ℤ, Xhat X A (m * (h:ℤ)) = Xhat X d (m * ((h / g : ℕ):ℤ)) := by
  1371. intro m
  1372. have hcast : ((h : ℕ) : ℤ) = ((g * (h / g) : ℕ) : ℤ) := by rw [← hhg]
  1373. rw [hcast, Xhat_conductor X hg0 hd0 hgd]
  1374. simp only [hF]
  1375. rw [hconv 1, hconv 2, hconv 4, hconv 8]
  1376. calc ∑ h ∈ ((range A).erase 0).filter (fun h => A / Nat.gcd A h = d), F h
  1377. = ∑ h ∈ ((range A).erase 0).filter (fun h => A / Nat.gcd A h = d),
  1378. (‖Xhat X d ((1:ℤ) * ((h / g : ℕ):ℤ))‖ * ‖Xhat X d ((2:ℤ) * ((h / g : ℕ):ℤ))‖
  1379. * (‖Xhat X d ((4:ℤ) * ((h / g : ℕ):ℤ))‖
  1380. * ‖Xhat X d ((8:ℤ) * ((h / g : ℕ):ℤ))‖)) := Finset.sum_congr rfl hterm
  1381. _ = ∑ xi ∈ (((range A).erase 0).filter (fun h => A / Nat.gcd A h = d)).image
  1382. (fun h => h / g),
  1383. (‖Xhat X d ((1:ℤ) * (xi:ℤ))‖ * ‖Xhat X d ((2:ℤ) * (xi:ℤ))‖
  1384. * (‖Xhat X d ((4:ℤ) * (xi:ℤ))‖ * ‖Xhat X d ((8:ℤ) * (xi:ℤ))‖)) := by
  1385. rw [Finset.sum_image hinj]
  1386. _ ≤ ∑ xi ∈ (range d).filter (fun xi => Nat.Coprime xi d),
  1387. (‖Xhat X d ((1:ℤ) * (xi:ℤ))‖ * ‖Xhat X d ((2:ℤ) * (xi:ℤ))‖
  1388. * (‖Xhat X d ((4:ℤ) * (xi:ℤ))‖ * ‖Xhat X d ((8:ℤ) * (xi:ℤ))‖)) := by
  1389. refine Finset.sum_le_sum_of_subset_of_nonneg ?_ (fun i _ _ => by positivity)
  1390. intro xi hxi
  1391. rw [Finset.mem_image] at hxi
  1392. obtain ⟨h, hh, rfl⟩ := hxi
  1393. exact (hfib h (Finset.mem_filter.mp hh).1 (Finset.mem_filter.mp hh).2).1
  1394. _ ≤ (d:ℝ) ^ 2 * (Mmom V₁.divisors d * Mmom V₂.divisors d * Mmom X d) :=
  1395. four_factors hV₁ hV₂ hcop hd1 hdodd
  1396. /-! ### The moment condition -/
  1397. theorem cube_rpow {x y : ℝ} (hx : 0 ≤ x) (hy : 0 ≤ y) :
  1398. (x ^ ((2:ℝ)/3) * y ^ ((1:ℝ)/3)) ^ (3:ℕ) = x ^ (2:ℕ) * y := by
  1399. rw [mul_pow, ← Real.rpow_natCast (x ^ ((2:ℝ)/3)) 3, ← Real.rpow_natCast (y ^ ((1:ℝ)/3)) 3,
  1400. ← Real.rpow_mul hx, ← Real.rpow_mul hy]
  1401. norm_num
  1402. theorem sum_cube_le {s : Finset ℕ} {a : ℕ → ℝ} (ha : ∀ i ∈ s, 0 ≤ a i) :
  1403. ∑ i ∈ s, (a i) ^ (3:ℕ) ≤ (∑ i ∈ s, a i) ^ (3:ℕ) := by
  1404. set S := ∑ i ∈ s, a i with hS
  1405. have hS0 : 0 ≤ S := Finset.sum_nonneg ha
  1406. have h1 : ∀ i ∈ s, (a i) ^ (3:ℕ) ≤ a i * S ^ 2 := by
  1407. intro i hi
  1408. have hai : a i ≤ S := Finset.single_le_sum ha hi
  1409. have h2 : 0 ≤ a i * (S - a i) * (S + a i) :=
  1410. mul_nonneg (mul_nonneg (ha i hi) (by linarith)) (by linarith [ha i hi])
  1411. nlinarith
  1412. calc ∑ i ∈ s, (a i) ^ (3:ℕ) ≤ ∑ i ∈ s, a i * S ^ 2 := Finset.sum_le_sum h1
  1413. _ = S * S ^ 2 := by rw [← Finset.sum_mul]
  1414. _ = S ^ (3:ℕ) := by ring
  1415. theorem Xhat_zero {X : Finset ℕ} (hX : X.Nonempty) (d : ℕ) : Xhat X d 0 = 1 := by
  1416. have hT : (X.card : ℂ) ≠ 0 := by
  1417. simpa using (Nat.cast_ne_zero (R := ℂ)).mpr (Finset.card_ne_zero.mpr hX)
  1418. unfold Xhat
  1419. have hone : ∀ x ∈ X, ee d ((0:ℤ) * (x:ℤ)) = 1 := by
  1420. intro x _
  1421. simp [ee]
  1422. rw [Finset.sum_congr rfl hone, Finset.sum_const, nsmul_eq_mul, mul_one, inv_mul_cancel₀ hT]
  1423. /-- **A sufficient moment condition.** -/
  1424. theorem coverage {V₁ V₂ : ℕ} (hV₁ : 0 < V₁) (hV₂ : 0 < V₂) (hcop : Nat.Coprime V₁ V₂)
  1425. {A : ℕ} (hA1 : 1 < A) (hAodd : Odd A)
  1426. (hS : ∑ d ∈ A.divisors.filter (fun d => 1 < d),
  1427. (d:ℝ) ^ ((2:ℝ)/3) * (Mmom V₁.divisors d * Mmom V₂.divisors d
  1428. * Mmom (V₁ * V₂).divisors d) ^ ((1:ℝ)/3) < 1) (c : ℕ) :
  1429. 0 < Ncount (V₁ * V₂).divisors A c := by
  1430. classical
  1431. set X := (V₁ * V₂).divisors with hXdef
  1432. have hXne : X.Nonempty := ⟨1, Nat.one_mem_divisors.mpr (by positivity)⟩
  1433. have hA0 : A ≠ 0 := by omega
  1434. by_contra hcon
  1435. push_neg at hcon
  1436. have hN0 : Ncount X A c = 0 := Nat.le_zero.mp hcon
  1437. have hid := four_count hXne hA0 c
  1438. rw [hN0] at hid
  1439. simp only [Nat.cast_zero, mul_zero, zero_div] at hid
  1440. set G : ℕ → ℂ := fun h => ee A (-((h : ℤ) * (c : ℤ)))
  1441. * (Xhat X A ((1:ℤ) * (h:ℤ)) * Xhat X A ((2:ℤ) * (h:ℤ))
  1442. * Xhat X A ((4:ℤ) * (h:ℤ)) * Xhat X A ((8:ℤ) * (h:ℤ))) with hG
  1443. have h0mem : (0:ℕ) ∈ range A := Finset.mem_range.mpr (by omega)
  1444. have hsplit : ∑ h ∈ (range A).erase 0, G h + G 0 = ∑ h ∈ range A, G h :=
  1445. Finset.sum_erase_add _ _ h0mem
  1446. have hG0 : G 0 = 1 := by
  1447. simp only [hG]
  1448. rw [show ((0:ℕ) : ℤ) = 0 by norm_num]
  1449. simp only [mul_zero, zero_mul, neg_zero]
  1450. rw [show ee A 0 = 1 by simp [ee], Xhat_zero hXne]
  1451. ring
  1452. rw [hG0, hid] at hsplit
  1453. have hnorm1 : ‖∑ h ∈ (range A).erase 0, G h‖ = 1 := by
  1454. have : ∑ h ∈ (range A).erase 0, G h = -1 := by linear_combination (norm := ring_nf) hsplit
  1455. rw [this]
  1456. simp
  1457. -- the triangle inequality
  1458. have htri : (1:ℝ) ≤ ∑ h ∈ (range A).erase 0,
  1459. (‖Xhat X A ((1:ℤ) * (h:ℤ))‖ * ‖Xhat X A ((2:ℤ) * (h:ℤ))‖
  1460. * (‖Xhat X A ((4:ℤ) * (h:ℤ))‖ * ‖Xhat X A ((8:ℤ) * (h:ℤ))‖)) := by
  1461. have h1 : ‖∑ h ∈ (range A).erase 0, G h‖ ≤ ∑ h ∈ (range A).erase 0, ‖G h‖ :=
  1462. norm_sum_le _ _
  1463. have h2 : ∀ h : ℕ, ‖G h‖ = ‖Xhat X A ((1:ℤ) * (h:ℤ))‖ * ‖Xhat X A ((2:ℤ) * (h:ℤ))‖
  1464. * (‖Xhat X A ((4:ℤ) * (h:ℤ))‖ * ‖Xhat X A ((8:ℤ) * (h:ℤ))‖) := by
  1465. intro h
  1466. simp only [hG, norm_mul, ee_norm, one_mul]
  1467. ring
  1468. rw [hnorm1] at h1
  1469. calc (1:ℝ) ≤ ∑ h ∈ (range A).erase 0, ‖G h‖ := h1
  1470. _ = _ := Finset.sum_congr rfl fun h _ => h2 h
  1471. -- the conductor bound
  1472. have hgroup := sum_by_conductor hV₁ hV₂ hcop hA1 hAodd
  1473. -- the moment condition
  1474. have hcube : ∑ d ∈ A.divisors.filter (fun d => 1 < d),
  1475. (d:ℝ) ^ (2:ℕ) * (Mmom V₁.divisors d * Mmom V₂.divisors d * Mmom X d)
  1476. ≤ (∑ d ∈ A.divisors.filter (fun d => 1 < d),
  1477. (d:ℝ) ^ ((2:ℝ)/3) * (Mmom V₁.divisors d * Mmom V₂.divisors d
  1478. * Mmom X d) ^ ((1:ℝ)/3)) ^ (3:ℕ) := by
  1479. have hterm : ∀ d ∈ A.divisors.filter (fun d => 1 < d),
  1480. (d:ℝ) ^ (2:ℕ) * (Mmom V₁.divisors d * Mmom V₂.divisors d * Mmom X d)
  1481. = ((d:ℝ) ^ ((2:ℝ)/3) * (Mmom V₁.divisors d * Mmom V₂.divisors d
  1482. * Mmom X d) ^ ((1:ℝ)/3)) ^ (3:ℕ) := by
  1483. intro d _
  1484. rw [cube_rpow (by positivity)
  1485. (mul_nonneg (mul_nonneg (Mmom_nonneg _ _) (Mmom_nonneg _ _)) (Mmom_nonneg _ _))]
  1486. rw [Finset.sum_congr rfl hterm]
  1487. refine sum_cube_le (fun d _ => ?_)
  1488. exact mul_nonneg (Real.rpow_nonneg (by positivity) _)
  1489. (Real.rpow_nonneg (mul_nonneg (mul_nonneg (Mmom_nonneg _ _) (Mmom_nonneg _ _))
  1490. (Mmom_nonneg _ _)) _)
  1491. have hlt : (∑ d ∈ A.divisors.filter (fun d => 1 < d),
  1492. (d:ℝ) ^ ((2:ℝ)/3) * (Mmom V₁.divisors d * Mmom V₂.divisors d
  1493. * Mmom X d) ^ ((1:ℝ)/3)) ^ (3:ℕ) < 1 := by
  1494. have hnn : 0 ≤ ∑ d ∈ A.divisors.filter (fun d => 1 < d),
  1495. (d:ℝ) ^ ((2:ℝ)/3) * (Mmom V₁.divisors d * Mmom V₂.divisors d
  1496. * Mmom X d) ^ ((1:ℝ)/3) := Finset.sum_nonneg fun d _ =>
  1497. mul_nonneg (Real.rpow_nonneg (by positivity) _)
  1498. (Real.rpow_nonneg (mul_nonneg (mul_nonneg (Mmom_nonneg _ _) (Mmom_nonneg _ _))
  1499. (Mmom_nonneg _ _)) _)
  1500. exact pow_lt_one₀ hnn hS (by norm_num)
  1501. have hsq : ∑ d ∈ A.divisors.filter (fun d => 1 < d),
  1502. (d:ℝ) ^ 2 * (Mmom V₁.divisors d * Mmom V₂.divisors d * Mmom X d)
  1503. = ∑ d ∈ A.divisors.filter (fun d => 1 < d),
  1504. (d:ℝ) ^ (2:ℕ) * (Mmom V₁.divisors d * Mmom V₂.divisors d * Mmom X d) := rfl
  1505. rw [hsq] at hgroup
  1506. linarith [htri, hgroup, hcube, hlt]
  1507. /-! ## Finite averaging
  1508. The combinatorial heart of the selection argument. We average the moment quantity
  1509. `∑_{d ∣ A, d > 1} d^{2/3} (M_d(X₁) M_d(X₂) M_d(X))^{1/3}` over all moduli `A = ∏_{p ∈ I} p`
  1510. obtained from `t`-element subsets `I` of a fixed set `P` of primes, and deduce from a
  1511. numerical inequality that some modulus satisfies the moment condition of `coverage`.
  1512. -/
  1513. /-! ### Products of distinct primes -/
  1514. theorem squarefree_prod_primes {s : Finset ℕ} (hs : ∀ p ∈ s, p.Prime) :
  1515. Squarefree (∏ p ∈ s, p) := by
  1516. classical
  1517. induction s using Finset.induction with
  1518. | empty => simp
  1519. | insert a s ha ih =>
  1520. rw [Finset.prod_insert ha]
  1521. have hap := hs a (Finset.mem_insert_self a s)
  1522. have hsq := ih (fun p hp => hs p (Finset.mem_insert_of_mem hp))
  1523. refine (Nat.squarefree_mul_iff).mpr ⟨?_, hap.squarefree, hsq⟩
  1524. have hnd : ¬ (a ∣ ∏ p ∈ s, p) := by
  1525. intro hdvd
  1526. obtain ⟨p, hp, hpa⟩ := Prime.exists_mem_finset_dvd hap.prime hdvd
  1527. have := (Nat.prime_dvd_prime_iff_eq hap (hs p (Finset.mem_insert_of_mem hp))).mp hpa
  1528. exact ha (this ▸ hp)
  1529. exact (Nat.Prime.coprime_iff_not_dvd hap).mpr hnd
  1530. /-- The divisors of a product of distinct primes are exactly the subproducts. -/
  1531. theorem sum_divisors_prod_primes {I : Finset ℕ} (hI : ∀ p ∈ I, p.Prime) (g : ℕ → ℝ) :
  1532. ∑ d ∈ (∏ p ∈ I, p).divisors, g d = ∑ J ∈ I.powerset, g (∏ p ∈ J, p) := by
  1533. classical
  1534. have hsq : Squarefree (∏ p ∈ I, p) := squarefree_prod_primes hI
  1535. have hA0 : (∏ p ∈ I, p) ≠ 0 := hsq.ne_zero
  1536. have hpf : (∏ p ∈ I, p).primeFactors = I := Nat.primeFactors_prod hI
  1537. refine (Finset.sum_nbij' (i := fun J => ∏ p ∈ J, p) (j := fun d => d.primeFactors)
  1538. ?_ ?_ ?_ ?_ ?_).symm
  1539. · intro J hJ
  1540. rw [Finset.mem_powerset] at hJ
  1541. exact Nat.mem_divisors.mpr ⟨Finset.prod_dvd_prod_of_subset _ _ _ hJ, hA0⟩
  1542. · intro d hd
  1543. rw [Nat.mem_divisors] at hd
  1544. rw [Finset.mem_powerset, ← hpf]
  1545. exact Nat.primeFactors_mono hd.1 hA0
  1546. · intro J hJ
  1547. rw [Finset.mem_powerset] at hJ
  1548. exact Nat.primeFactors_prod (fun p hp => hI p (hJ hp))
  1549. · intro d hd
  1550. rw [Nat.mem_divisors] at hd
  1551. exact Nat.prod_primeFactors_of_squarefree (hsq.squarefree_of_dvd hd.1)
  1552. · intro J hJ
  1553. rfl
  1554. /-- The nontrivial divisors of `∏_{p ∈ I} p`, organised by the size of the subset. -/
  1555. theorem sum_divisors_prod_primes_filter {I : Finset ℕ} (hI : ∀ p ∈ I, p.Prime) (g : ℕ → ℝ) :
  1556. ∑ d ∈ (∏ p ∈ I, p).divisors.filter (fun d => 1 < d), g d
  1557. = ∑ s ∈ Finset.Icc 1 I.card, ∑ J ∈ I.powersetCard s, g (∏ p ∈ J, p) := by
  1558. classical
  1559. set h : Finset ℕ → ℝ := fun J => g (∏ p ∈ J, p) with hh
  1560. have h2 : ∑ d ∈ (∏ p ∈ I, p).divisors.filter (fun d => ¬ (1 < d)), g d = g 1 := by
  1561. have hfil : (∏ p ∈ I, p).divisors.filter (fun d => ¬ (1 < d)) = {1} := by
  1562. ext d
  1563. simp only [Finset.mem_filter, Nat.mem_divisors, Finset.mem_singleton, not_lt]
  1564. constructor
  1565. · rintro ⟨⟨hdvd, h0⟩, hd1⟩
  1566. have : d ≠ 0 := by
  1567. rintro rfl
  1568. exact h0 (Nat.eq_zero_of_zero_dvd hdvd)
  1569. omega
  1570. · rintro rfl
  1571. exact ⟨⟨one_dvd _, (Finset.prod_pos (fun q hq => (hI q hq).pos)).ne'⟩, le_refl 1⟩
  1572. rw [hfil, Finset.sum_singleton]
  1573. have h3 : ∑ J ∈ I.powerset.filter (fun J => ¬ J.Nonempty), h J = g 1 := by
  1574. have hfil : I.powerset.filter (fun J => ¬ J.Nonempty) = {∅} := by
  1575. ext J
  1576. simp only [Finset.mem_filter, Finset.mem_powerset, Finset.mem_singleton,
  1577. Finset.not_nonempty_iff_eq_empty]
  1578. constructor
  1579. · rintro ⟨-, rfl⟩; rfl
  1580. · rintro rfl; exact ⟨Finset.empty_subset _, rfl⟩
  1581. rw [hfil, Finset.sum_singleton]
  1582. simp [hh]
  1583. have e1 := Finset.sum_filter_add_sum_filter_not (∏ p ∈ I, p).divisors (fun d => 1 < d) g
  1584. have e2 := Finset.sum_filter_add_sum_filter_not I.powerset (fun J => J.Nonempty) h
  1585. have hpow : ∑ J ∈ I.powerset, h J
  1586. = ∑ j ∈ range (I.card + 1), ∑ J ∈ I.powersetCard j, h J := Finset.sum_powerset I h
  1587. have hshift : ∑ j ∈ range (I.card + 1), (∑ J ∈ I.powersetCard j, h J)
  1588. = ∑ s ∈ Finset.Icc 1 I.card, (∑ J ∈ I.powersetCard s, h J)
  1589. + ∑ J ∈ I.powersetCard 0, h J := by
  1590. rw [Finset.sum_range_succ']
  1591. congr 1
  1592. rw [← Finset.Ico_add_one_right_eq_Icc, Finset.sum_Ico_eq_sum_range]
  1593. simp [add_comm]
  1594. have hzero : ∑ J ∈ I.powersetCard 0, h J = g 1 := by
  1595. rw [Finset.powersetCard_zero, Finset.sum_singleton]
  1596. simp [hh]
  1597. have hdiv : ∑ d ∈ (∏ p ∈ I, p).divisors, g d = ∑ J ∈ I.powerset, h J :=
  1598. sum_divisors_prod_primes hI g
  1599. rw [hzero] at hshift
  1600. linarith [e1, e2, hpow, hshift, hdiv, h2, h3]
  1601. /-! ### Binomial ratios -/
  1602. theorem descFactorial_mul_pow_le {N R s : ℕ} (hRN : R ≤ N) :
  1603. R.descFactorial s * N ^ s ≤ N.descFactorial s * R ^ s := by
  1604. have hc : ∀ c : ℕ, c ^ s = ∏ _i ∈ range s, c := by intro c; simp
  1605. rw [Nat.descFactorial_eq_prod_range, Nat.descFactorial_eq_prod_range, hc N, hc R,
  1606. ← Finset.prod_mul_distrib, ← Finset.prod_mul_distrib]
  1607. refine Finset.prod_le_prod' ?_
  1608. intro i _
  1609. rcases le_or_gt i R with h | h
  1610. · obtain ⟨a, ha⟩ : ∃ a, R = i + a := ⟨R - i, by omega⟩
  1611. obtain ⟨b, hb⟩ : ∃ b, N = R + b := ⟨N - R, by omega⟩
  1612. subst ha; subst hb
  1613. have h1 : i + a + b - i = a + b := by omega
  1614. have h2 : i + a - i = a := by omega
  1615. rw [h1, h2]
  1616. nlinarith
  1617. · have : R - i = 0 := by omega
  1618. simp [this]
  1619. /-- **Binomial ratio.** -/
  1620. theorem binom_ratio {N R s : ℕ} (hRN : R ≤ N) (hN : 0 < N) :
  1621. ((R.choose s : ℝ)) / (N.choose s) ≤ ((R:ℝ)/N) ^ s := by
  1622. have hNR : (0:ℝ) < (N:ℝ) := by exact_mod_cast hN
  1623. rcases Nat.eq_zero_or_pos (N.choose s) with h0 | hpos
  1624. · rw [h0]
  1625. simp only [Nat.cast_zero, div_zero]
  1626. positivity
  1627. · have key := descFactorial_mul_pow_le (N := N) (R := R) (s := s) hRN
  1628. rw [Nat.descFactorial_eq_factorial_mul_choose, Nat.descFactorial_eq_factorial_mul_choose] at key
  1629. have keyR : (R.choose s : ℝ) * (N:ℝ) ^ s ≤ (N.choose s : ℝ) * (R:ℝ) ^ s := by
  1630. have h' : ((s.factorial * R.choose s * N ^ s : ℕ) : ℝ)
  1631. ≤ ((s.factorial * N.choose s * R ^ s : ℕ):ℝ) := by exact_mod_cast key
  1632. push_cast at h'
  1633. have hf : (0:ℝ) < s.factorial := by positivity
  1634. nlinarith [h']
  1635. rw [div_pow, div_le_div_iff₀ (by exact_mod_cast hpos) (by positivity)]
  1636. linarith [keyR]
  1637. /-! ### Nested subsets -/
  1638. theorem card_nested {α : Type*} [DecidableEq α] {P : Finset α} {s t : ℕ} (hst : s ≤ t)
  1639. {J : Finset α} (hJP : J ⊆ P) (hJcard : J.card = s) :
  1640. ((P.powersetCard t).filter (fun I => J ⊆ I)).card = (P.card - s).choose (t - s) := by
  1641. classical
  1642. have hcard : ((P \ J).powersetCard (t - s)).card = (P.card - s).choose (t - s) := by
  1643. rw [Finset.card_powersetCard, Finset.card_sdiff_of_subset hJP, hJcard]
  1644. rw [← hcard]
  1645. refine Finset.card_bij' (fun I _ => I \ J) (fun K _ => K ∪ J) ?_ ?_ ?_ ?_
  1646. · intro I hI
  1647. simp only [Finset.mem_filter, Finset.mem_powersetCard] at hI
  1648. obtain ⟨⟨hIP, hIcard⟩, hJI⟩ := hI
  1649. refine Finset.mem_powersetCard.mpr ⟨?_, ?_⟩
  1650. · intro x hx
  1651. simp only [Finset.mem_sdiff] at hx ⊢
  1652. exact ⟨hIP hx.1, hx.2⟩
  1653. · rw [Finset.card_sdiff_of_subset hJI, hIcard, hJcard]
  1654. · intro K hK
  1655. simp only [Finset.mem_powersetCard] at hK
  1656. obtain ⟨hKP, hKcard⟩ := hK
  1657. have hdisj : Disjoint K J := Finset.disjoint_right.mpr (fun x hxJ hxK =>
  1658. (Finset.mem_sdiff.mp (hKP hxK)).2 hxJ)
  1659. refine Finset.mem_filter.mpr ⟨Finset.mem_powersetCard.mpr ⟨?_, ?_⟩, Finset.subset_union_right⟩
  1660. · exact Finset.union_subset (hKP.trans Finset.sdiff_subset) hJP
  1661. · rw [Finset.card_union_of_disjoint hdisj, hKcard, hJcard]
  1662. omega
  1663. · intro I hI
  1664. simp only [Finset.mem_filter] at hI
  1665. exact Finset.sdiff_union_of_subset hI.2
  1666. · intro K hK
  1667. simp only [Finset.mem_powersetCard] at hK
  1668. have hdisj : Disjoint K J := Finset.disjoint_right.mpr (fun x hxJ hxK =>
  1669. (Finset.mem_sdiff.mp (hK.1 hxK)).2 hxJ)
  1670. exact Finset.union_sdiff_cancel_right hdisj
  1671. /-- **Nested subsets.** -/
  1672. theorem sum_nested {α : Type*} [DecidableEq α] {P : Finset α} {s t : ℕ} (hst : s ≤ t)
  1673. (f : Finset α → ℝ) :
  1674. ∑ I ∈ P.powersetCard t, ∑ J ∈ I.powersetCard s, f J
  1675. = ((P.card - s).choose (t - s) : ℝ) * ∑ J ∈ P.powersetCard s, f J := by
  1676. classical
  1677. have hinner : ∀ I ∈ P.powersetCard t, ∑ J ∈ I.powersetCard s, f J
  1678. = ∑ J ∈ P.powersetCard s, if J ⊆ I then f J else 0 := by
  1679. intro I hI
  1680. have hIP : I ⊆ P := (Finset.mem_powersetCard.mp hI).1
  1681. rw [Finset.sum_ite, Finset.sum_const_zero, add_zero]
  1682. refine Finset.sum_congr ?_ (fun _ _ => rfl)
  1683. ext J
  1684. simp only [Finset.mem_powersetCard, Finset.mem_filter]
  1685. constructor
  1686. · rintro ⟨hJI, hJc⟩; exact ⟨⟨hJI.trans hIP, hJc⟩, hJI⟩
  1687. · rintro ⟨⟨-, hJc⟩, hJI⟩; exact ⟨hJI, hJc⟩
  1688. rw [Finset.sum_congr rfl hinner, Finset.sum_comm, Finset.mul_sum]
  1689. refine Finset.sum_congr rfl fun J hJ => ?_
  1690. obtain ⟨hJP, hJc⟩ := Finset.mem_powersetCard.mp hJ
  1691. rw [Finset.sum_ite, Finset.sum_const_zero, add_zero, Finset.sum_const,
  1692. card_nested hst hJP hJc, nsmul_eq_mul]
  1693. /-! ### The three-factor Hölder inequality -/
  1694. theorem holder3 {α : Type*} (s : Finset α) {f₁ f₂ f₃ : α → ℝ}
  1695. (h₁ : ∀ x ∈ s, 0 ≤ f₁ x) (h₂ : ∀ x ∈ s, 0 ≤ f₂ x) (h₃ : ∀ x ∈ s, 0 ≤ f₃ x) :
  1696. ∑ x ∈ s, (f₁ x * f₂ x * f₃ x) ^ ((1:ℝ)/3)
  1697. ≤ ((∑ x ∈ s, f₁ x) * (∑ x ∈ s, f₂ x) * (∑ x ∈ s, f₃ x)) ^ ((1:ℝ)/3) := by
  1698. set F₁ := ∑ x ∈ s, f₁ x with hF₁
  1699. set F₂ := ∑ x ∈ s, f₂ x with hF₂
  1700. set F₃ := ∑ x ∈ s, f₃ x with hF₃
  1701. have hF₁0 : 0 ≤ F₁ := Finset.sum_nonneg h₁
  1702. have hF₂0 : 0 ≤ F₂ := Finset.sum_nonneg h₂
  1703. have hF₃0 : 0 ≤ F₃ := Finset.sum_nonneg h₃
  1704. by_cases hz : F₁ = 0 ∨ F₂ = 0 ∨ F₃ = 0
  1705. · have hzero : ∀ x ∈ s, (f₁ x * f₂ x * f₃ x) ^ ((1:ℝ)/3) = 0 := by
  1706. intro x hx
  1707. have hx0 : f₁ x * f₂ x * f₃ x = 0 := by
  1708. rcases hz with h | h | h
  1709. · have := (Finset.sum_eq_zero_iff_of_nonneg h₁).mp h x hx
  1710. rw [this]; ring
  1711. · have := (Finset.sum_eq_zero_iff_of_nonneg h₂).mp h x hx
  1712. rw [this]; ring
  1713. · have := (Finset.sum_eq_zero_iff_of_nonneg h₃).mp h x hx
  1714. rw [this]; ring
  1715. rw [hx0, Real.zero_rpow (by norm_num)]
  1716. rw [Finset.sum_congr rfl hzero]
  1717. simp only [Finset.sum_const_zero]
  1718. positivity
  1719. push_neg at hz
  1720. obtain ⟨hz1, hz2, hz3⟩ := hz
  1721. have hp1 : 0 < F₁ := lt_of_le_of_ne hF₁0 (Ne.symm hz1)
  1722. have hp2 : 0 < F₂ := lt_of_le_of_ne hF₂0 (Ne.symm hz2)
  1723. have hp3 : 0 < F₃ := lt_of_le_of_ne hF₃0 (Ne.symm hz3)
  1724. have hstep : ∀ x ∈ s, (f₁ x * f₂ x * f₃ x) ^ ((1:ℝ)/3)
  1725. ≤ (F₁ * F₂ * F₃) ^ ((1:ℝ)/3)
  1726. * ((1/3) * (f₁ x / F₁) + (1/3) * (f₂ x / F₂) + (1/3) * (f₃ x / F₃)) := by
  1727. intro x hx
  1728. have hq1 : 0 ≤ f₁ x / F₁ := div_nonneg (h₁ x hx) hF₁0
  1729. have hq2 : 0 ≤ f₂ x / F₂ := div_nonneg (h₂ x hx) hF₂0
  1730. have hq3 : 0 ≤ f₃ x / F₃ := div_nonneg (h₃ x hx) hF₃0
  1731. have hgm := Real.geom_mean_le_arith_mean3_weighted (by norm_num : (0:ℝ) ≤ 1/3)
  1732. (by norm_num : (0:ℝ) ≤ 1/3) (by norm_num : (0:ℝ) ≤ 1/3) hq1 hq2 hq3 (by norm_num)
  1733. have hfac : f₁ x * f₂ x * f₃ x
  1734. = (F₁ * F₂ * F₃) * ((f₁ x / F₁) * (f₂ x / F₂) * (f₃ x / F₃)) := by
  1735. field_simp
  1736. rw [hfac, Real.mul_rpow (by positivity) (by positivity),
  1737. Real.mul_rpow (by positivity) hq3, Real.mul_rpow hq1 hq2, mul_assoc]
  1738. exact mul_le_mul_of_nonneg_left hgm (by positivity)
  1739. calc ∑ x ∈ s, (f₁ x * f₂ x * f₃ x) ^ ((1:ℝ)/3)
  1740. ≤ ∑ x ∈ s, (F₁ * F₂ * F₃) ^ ((1:ℝ)/3)
  1741. * ((1/3) * (f₁ x / F₁) + (1/3) * (f₂ x / F₂) + (1/3) * (f₃ x / F₃)) :=
  1742. Finset.sum_le_sum hstep
  1743. _ = (F₁ * F₂ * F₃) ^ ((1:ℝ)/3) := by
  1744. rw [← Finset.mul_sum]
  1745. have hone : ∑ x ∈ s, ((1/3) * (f₁ x / F₁) + (1/3) * (f₂ x / F₂) + (1/3) * (f₃ x / F₃))
  1746. = 1 := by
  1747. rw [Finset.sum_add_distrib, Finset.sum_add_distrib, ← Finset.mul_sum, ← Finset.mul_sum,
  1748. ← Finset.mul_sum, ← Finset.sum_div, ← Finset.sum_div, ← Finset.sum_div,
  1749. ← hF₁, ← hF₂, ← hF₃]
  1750. field_simp
  1751. norm_num
  1752. rw [hone, mul_one]
  1753. /-! ### The average collision bound -/
  1754. theorem card_filter_dvd_prod_le {P : Finset ℕ} {s R : ℕ} {d : ℤ}
  1755. (hR : (P.filter (fun p : ℕ => (p:ℤ) ∣ d)).card ≤ R) :
  1756. ((P.powersetCard s).filter (fun J => ((∏ p ∈ J, p : ℕ) : ℤ) ∣ d)).card ≤ R.choose s := by
  1757. classical
  1758. have hsub : (P.powersetCard s).filter (fun J => ((∏ p ∈ J, p : ℕ) : ℤ) ∣ d)
  1759. ⊆ (P.filter (fun p : ℕ => (p:ℤ) ∣ d)).powersetCard s := by
  1760. intro J hJ
  1761. simp only [Finset.mem_filter, Finset.mem_powersetCard] at hJ ⊢
  1762. obtain ⟨⟨hJP, hJc⟩, hdvd⟩ := hJ
  1763. refine ⟨?_, hJc⟩
  1764. intro p hp
  1765. refine Finset.mem_filter.mpr ⟨hJP hp, ?_⟩
  1766. have h1 : (p:ℤ) ∣ ((∏ q ∈ J, q : ℕ) : ℤ) :=
  1767. Int.natCast_dvd_natCast.mpr (Finset.dvd_prod_of_mem _ hp)
  1768. exact h1.trans hdvd
  1769. calc ((P.powersetCard s).filter (fun J => ((∏ p ∈ J, p : ℕ) : ℤ) ∣ d)).card
  1770. ≤ ((P.filter (fun p : ℕ => (p:ℤ) ∣ d)).powersetCard s).card := Finset.card_le_card hsub
  1771. _ = ((P.filter (fun p : ℕ => (p:ℤ) ∣ d)).card).choose s := Finset.card_powersetCard _ _
  1772. _ ≤ R.choose s := Nat.choose_le_choose s hR
  1773. /-- **Average collision bound.** -/
  1774. theorem collision_average {P : Finset ℕ} {Y : Finset ℕ} (hY : Y.Nonempty) {R s : ℕ}
  1775. (hcol : ∀ x ∈ Y, ∀ y ∈ Y, x ≠ y →
  1776. (P.filter (fun p : ℕ => (p:ℤ) ∣ (x:ℤ) - (y:ℤ))).card ≤ R) :
  1777. ∑ J ∈ P.powersetCard s, Mmom Y (∏ p ∈ J, p)
  1778. ≤ ((P.powersetCard s).card : ℝ) * (1 / (Y.card:ℝ)) + ((R.choose s : ℝ)) := by
  1779. classical
  1780. set T := Y.card with hT
  1781. have hT0 : 0 < T := Finset.card_pos.mpr hY
  1782. have hTR : (0:ℝ) < (T:ℝ) := by exact_mod_cast hT0
  1783. set c : Finset ℕ → ℕ := fun J =>
  1784. ((Y ×ˢ Y).filter (fun q => ((∏ p ∈ J, p : ℕ):ℤ) ∣ (q.1:ℤ) - (q.2:ℤ))).card with hc
  1785. have hMmom : ∀ J, Mmom Y (∏ p ∈ J, p) = (c J : ℝ) / (T:ℝ)^2 := by
  1786. intro J; rfl
  1787. have hswap : ∑ J ∈ P.powersetCard s, c J
  1788. = ∑ q ∈ Y ×ˢ Y, ((P.powersetCard s).filter
  1789. (fun J => ((∏ p ∈ J, p : ℕ):ℤ) ∣ (q.1:ℤ) - (q.2:ℤ))).card := by
  1790. simp only [hc, Finset.card_filter]
  1791. rw [Finset.sum_comm]
  1792. have hpoint : ∀ q ∈ Y ×ˢ Y, ((P.powersetCard s).filter
  1793. (fun J => ((∏ p ∈ J, p : ℕ):ℤ) ∣ (q.1:ℤ) - (q.2:ℤ))).card
  1794. ≤ (if q.1 = q.2 then (P.powersetCard s).card else 0) + R.choose s := by
  1795. intro q hq
  1796. simp only [Finset.mem_product] at hq
  1797. by_cases hqe : q.1 = q.2
  1798. · rw [if_pos hqe]
  1799. exact le_trans (Finset.card_le_card (Finset.filter_subset _ _)) (Nat.le_add_right _ _)
  1800. · rw [if_neg hqe, zero_add]
  1801. exact card_filter_dvd_prod_le (hcol q.1 hq.1 q.2 hq.2 hqe)
  1802. have hdiag : ((Y ×ˢ Y).filter (fun q => q.1 = q.2)).card ≤ T := by
  1803. refine Finset.card_le_card_of_injOn (fun q => q.1) (fun q hq => (Finset.mem_product.mp
  1804. (Finset.mem_filter.mp hq).1).1) ?_
  1805. intro a ha b hb hab
  1806. simp only [Finset.coe_filter, Set.mem_setOf_eq] at ha hb
  1807. exact Prod.ext hab (by rw [← ha.2, ← hb.2]; exact hab)
  1808. have hsum : (∑ J ∈ P.powersetCard s, c J : ℕ)
  1809. ≤ T * (P.powersetCard s).card + T * T * R.choose s := by
  1810. rw [hswap]
  1811. calc ∑ q ∈ Y ×ˢ Y, ((P.powersetCard s).filter
  1812. (fun J => ((∏ p ∈ J, p : ℕ):ℤ) ∣ (q.1:ℤ) - (q.2:ℤ))).card
  1813. ≤ ∑ q ∈ Y ×ˢ Y, ((if q.1 = q.2 then (P.powersetCard s).card else 0) + R.choose s) :=
  1814. Finset.sum_le_sum hpoint
  1815. _ = ((Y ×ˢ Y).filter (fun q => q.1 = q.2)).card * (P.powersetCard s).card
  1816. + (Y ×ˢ Y).card * R.choose s := by
  1817. rw [Finset.sum_add_distrib, Finset.sum_ite, Finset.sum_const, Finset.sum_const_zero,
  1818. add_zero, Finset.sum_const, smul_eq_mul, smul_eq_mul]
  1819. _ ≤ T * (P.powersetCard s).card + T * T * R.choose s := by
  1820. have hcard : (Y ×ˢ Y).card = T * T := by rw [Finset.card_product]
  1821. rw [hcard]
  1822. exact Nat.add_le_add_right (Nat.mul_le_mul_right _ hdiag) _
  1823. have hsumR : (∑ J ∈ P.powersetCard s, (c J : ℝ))
  1824. ≤ (T:ℝ) * (P.powersetCard s).card + (T:ℝ) * T * R.choose s := by
  1825. have h' := (Nat.cast_le (α := ℝ)).mpr hsum
  1826. push_cast at h'
  1827. exact h'
  1828. calc ∑ J ∈ P.powersetCard s, Mmom Y (∏ p ∈ J, p)
  1829. = (∑ J ∈ P.powersetCard s, (c J : ℝ)) / (T:ℝ)^2 := by
  1830. rw [Finset.sum_div]
  1831. exact Finset.sum_congr rfl fun J _ => hMmom J
  1832. _ ≤ ((T:ℝ) * (P.powersetCard s).card + (T:ℝ) * T * R.choose s) / (T:ℝ)^2 :=
  1833. div_le_div_of_nonneg_right hsumR (by positivity)
  1834. _ = ((P.powersetCard s).card : ℝ) * (1 / (T:ℝ)) + (R.choose s : ℝ) := by
  1835. field_simp
  1836. /-! ### Numerical preparation -/
  1837. theorem two_thirds_prod_le {x y r : ℝ} (hx : 0 ≤ x) (hy : 0 ≤ y) (hr : 0 ≤ r) :
  1838. (x + r) ^ ((2:ℝ)/3) * (y + r) ^ ((1:ℝ)/3)
  1839. ≤ x ^ ((2:ℝ)/3) * y ^ ((1:ℝ)/3) + x ^ ((2:ℝ)/3) * r ^ ((1:ℝ)/3)
  1840. + r ^ ((2:ℝ)/3) * y ^ ((1:ℝ)/3) + r := by
  1841. have h1 : (x + r) ^ ((2:ℝ)/3) ≤ x ^ ((2:ℝ)/3) + r ^ ((2:ℝ)/3) :=
  1842. Real.rpow_add_le_add_rpow hx hr (by norm_num) (by norm_num)
  1843. have h2 : (y + r) ^ ((1:ℝ)/3) ≤ y ^ ((1:ℝ)/3) + r ^ ((1:ℝ)/3) :=
  1844. Real.rpow_add_le_add_rpow hy hr (by norm_num) (by norm_num)
  1845. have hrr : r ^ ((2:ℝ)/3) * r ^ ((1:ℝ)/3) = r := by
  1846. rw [← Real.rpow_add' hr (by norm_num)]
  1847. norm_num
  1848. calc (x + r) ^ ((2:ℝ)/3) * (y + r) ^ ((1:ℝ)/3)
  1849. ≤ (x ^ ((2:ℝ)/3) + r ^ ((2:ℝ)/3)) * (y ^ ((1:ℝ)/3) + r ^ ((1:ℝ)/3)) :=
  1850. mul_le_mul h1 h2 (by positivity) (by positivity)
  1851. _ = x ^ ((2:ℝ)/3) * y ^ ((1:ℝ)/3) + x ^ ((2:ℝ)/3) * r ^ ((1:ℝ)/3)
  1852. + r ^ ((2:ℝ)/3) * y ^ ((1:ℝ)/3) + r ^ ((2:ℝ)/3) * r ^ ((1:ℝ)/3) := by ring
  1853. _ = _ := by rw [hrr]
  1854. theorem sum_Icc_choose_pow (t : ℕ) (x : ℝ) :
  1855. ∑ s ∈ Finset.Icc 1 t, (t.choose s : ℝ) * x ^ s = (1 + x) ^ t - 1 := by
  1856. have hbin : (x + 1) ^ t = ∑ s ∈ range (t+1), x ^ s * (t.choose s : ℝ) := by
  1857. rw [add_pow]
  1858. exact Finset.sum_congr rfl fun s _ => by rw [one_pow, mul_one]
  1859. have hsplit : ∑ s ∈ range (t+1), x ^ s * (t.choose s : ℝ)
  1860. = ∑ s ∈ Finset.Icc 1 t, x ^ s * (t.choose s : ℝ) + x ^ 0 * (t.choose 0 : ℝ) := by
  1861. rw [Finset.sum_range_succ']
  1862. congr 1
  1863. rw [← Finset.Ico_add_one_right_eq_Icc, Finset.sum_Ico_eq_sum_range]
  1864. simp [add_comm]
  1865. have hcomm : ∑ s ∈ Finset.Icc 1 t, (t.choose s : ℝ) * x ^ s
  1866. = ∑ s ∈ Finset.Icc 1 t, x ^ s * (t.choose s : ℝ) :=
  1867. Finset.sum_congr rfl fun s _ => by ring
  1868. have hx1 : (1 + x) ^ t = (x + 1) ^ t := by ring_nf
  1869. rw [hcomm, hx1, hbin, hsplit]
  1870. simp
  1871. theorem cube_root_mul {c a b : ℝ} (hc : 0 ≤ c) (ha : 0 ≤ a) (hb : 0 ≤ b) :
  1872. ((2*c*a) * (2*c*a) * (c*b)) ^ ((1:ℝ)/3) ≤ 2 * c * (a ^ ((2:ℝ)/3) * b ^ ((1:ℝ)/3)) := by
  1873. have hdiff : (2*c)^(3:ℕ) * (a^(2:ℕ) * b) - (2*c*a) * (2*c*a) * (c*b) = 4*(c^3*(a^2*b)) := by
  1874. ring
  1875. have hnn : (0:ℝ) ≤ 4*(c^3*(a^2*b)) := by positivity
  1876. have hle : (2*c*a) * (2*c*a) * (c*b) ≤ (2*c)^(3:ℕ) * (a^(2:ℕ) * b) := by linarith
  1877. have h1 : ((2*c*a) * (2*c*a) * (c*b)) ^ ((1:ℝ)/3) ≤ ((2*c)^(3:ℕ) * (a^(2:ℕ) * b)) ^ ((1:ℝ)/3) :=
  1878. Real.rpow_le_rpow (by positivity) hle (by norm_num)
  1879. have h2 : ((2*c)^(3:ℕ) * (a^(2:ℕ) * b)) ^ ((1:ℝ)/3)
  1880. = (2*c) * (a ^ ((2:ℝ)/3) * b ^ ((1:ℝ)/3)) := by
  1881. rw [Real.mul_rpow (by positivity) (by positivity), Real.mul_rpow (by positivity) hb]
  1882. congr 1
  1883. · rw [← Real.rpow_natCast (2*c) 3, ← Real.rpow_mul (by positivity)]
  1884. norm_num
  1885. · congr 1
  1886. rw [← Real.rpow_natCast a 2, ← Real.rpow_mul ha]
  1887. norm_num
  1888. rw [h2] at h1
  1889. calc ((2*c*a) * (2*c*a) * (c*b)) ^ ((1:ℝ)/3) ≤ (2*c) * (a ^ ((2:ℝ)/3) * b ^ ((1:ℝ)/3)) := h1
  1890. _ = 2 * c * (a ^ ((2:ℝ)/3) * b ^ ((1:ℝ)/3)) := by ring
  1891. theorem prod_le_pow_of_le {J : Finset ℕ} {U : ℝ} (hU0 : 0 ≤ U) (hJ : ∀ p ∈ J, (p:ℝ) ≤ U) :
  1892. ((∏ p ∈ J, p : ℕ) : ℝ) ^ ((2:ℝ)/3) ≤ (U ^ ((2:ℝ)/3)) ^ J.card := by
  1893. have hcast : ((∏ p ∈ J, p : ℕ) : ℝ) = ∏ p ∈ J, (p:ℝ) := by push_cast; ring
  1894. have hprod : ∏ p ∈ J, (p:ℝ) ≤ U ^ J.card := by
  1895. calc ∏ p ∈ J, (p:ℝ) ≤ ∏ _p ∈ J, U :=
  1896. Finset.prod_le_prod (fun p _ => by positivity) hJ
  1897. _ = U ^ J.card := by rw [Finset.prod_const]
  1898. have h1 : ((∏ p ∈ J, p : ℕ) : ℝ) ^ ((2:ℝ)/3) ≤ (U ^ J.card) ^ ((2:ℝ)/3) := by
  1899. rw [hcast]
  1900. exact Real.rpow_le_rpow (by positivity) hprod (by norm_num)
  1901. have h2 : (U ^ J.card) ^ ((2:ℝ)/3) = (U ^ ((2:ℝ)/3)) ^ J.card := by
  1902. rw [← Real.rpow_natCast U J.card, ← Real.rpow_mul hU0,
  1903. ← Real.rpow_natCast (U ^ ((2:ℝ)/3)) J.card, ← Real.rpow_mul hU0, mul_comm]
  1904. rw [h2] at h1
  1905. exact h1
  1906. /-- The average moment over all `s`-element subsets of `P`. -/
  1907. theorem moment_avg_le {P Y : Finset ℕ} (hY : Y.Nonempty) {R s : ℕ} (hR : R ≤ P.card)
  1908. (hN : 0 < P.card)
  1909. (hcol : ∀ x ∈ Y, ∀ y ∈ Y, x ≠ y →
  1910. (P.filter (fun p : ℕ => (p:ℤ) ∣ (x:ℤ) - (y:ℤ))).card ≤ R) :
  1911. ∑ J ∈ P.powersetCard s, Mmom Y (∏ p ∈ J, p)
  1912. ≤ (P.card.choose s : ℝ) * (1 / (Y.card:ℝ) + ((R:ℝ)/P.card) ^ s) := by
  1913. have hca := collision_average hY hcol (P := P) (R := R) (s := s)
  1914. rw [Finset.card_powersetCard] at hca
  1915. have hchoose : (R.choose s : ℝ) ≤ (P.card.choose s : ℝ) * ((R:ℝ)/P.card) ^ s := by
  1916. rcases Nat.eq_zero_or_pos (P.card.choose s) with h0 | hpos
  1917. · have hz : R.choose s = 0 := Nat.le_zero.mp (h0 ▸ Nat.choose_le_choose s hR)
  1918. rw [hz, h0]
  1919. simp
  1920. · have hb := binom_ratio hR hN (s := s)
  1921. rw [div_le_iff₀ (by exact_mod_cast hpos)] at hb
  1922. linarith [hb]
  1923. linarith [hca, hchoose]
  1924. /-- The average of the full moment product over all `s`-element subsets of `P`. -/
  1925. theorem moment_prod_sum_bound {P X₁ X₂ X : Finset ℕ}
  1926. (hX₁ : X₁.Nonempty) (hX₂ : X₂.Nonempty) (hX : X.Nonempty)
  1927. {U a b : ℝ} (hU0 : 0 ≤ U) (hPU : ∀ p ∈ P, (p:ℝ) ≤ U) (ha : 0 ≤ a) (hb : 0 ≤ b)
  1928. (hb₁ : 1 / (X₁.card:ℝ) ≤ 2 * a) (hb₂ : 1 / (X₂.card:ℝ) ≤ 2 * a)
  1929. (hbX : 1 / (X.card:ℝ) ≤ b)
  1930. {R s : ℕ} (hR : R ≤ P.card) (hN : 0 < P.card)
  1931. (hcol₁ : ∀ x ∈ X₁, ∀ y ∈ X₁, x ≠ y →
  1932. (P.filter (fun p : ℕ => (p:ℤ) ∣ (x:ℤ) - (y:ℤ))).card ≤ R)
  1933. (hcol₂ : ∀ x ∈ X₂, ∀ y ∈ X₂, x ≠ y →
  1934. (P.filter (fun p : ℕ => (p:ℤ) ∣ (x:ℤ) - (y:ℤ))).card ≤ R)
  1935. (hcolX : ∀ x ∈ X, ∀ y ∈ X, x ≠ y →
  1936. (P.filter (fun p : ℕ => (p:ℤ) ∣ (x:ℤ) - (y:ℤ))).card ≤ R) :
  1937. ∑ J ∈ P.powersetCard s, (((∏ p ∈ J, p : ℕ)):ℝ) ^ ((2:ℝ)/3)
  1938. * (Mmom X₁ (∏ p ∈ J, p) * Mmom X₂ (∏ p ∈ J, p) * Mmom X (∏ p ∈ J, p)) ^ ((1:ℝ)/3)
  1939. ≤ (P.card.choose s : ℝ) * (2 * (U ^ ((2:ℝ)/3)) ^ s
  1940. * ((a + ((R:ℝ)/P.card) ^ s) ^ ((2:ℝ)/3)
  1941. * (b + ((R:ℝ)/P.card) ^ s) ^ ((1:ℝ)/3))) := by
  1942. classical
  1943. set C : ℝ := (P.card.choose s : ℝ) with hC
  1944. set ρ : ℝ := (R:ℝ)/P.card with hρ
  1945. have hρ0 : 0 ≤ ρ := by positivity
  1946. have hC0 : 0 ≤ C := by positivity
  1947. set w : ℝ := U ^ ((2:ℝ)/3) with hw
  1948. have hw0 : 0 ≤ w := Real.rpow_nonneg hU0 _
  1949. have hstep1 : ∀ J ∈ P.powersetCard s, (((∏ p ∈ J, p : ℕ)):ℝ) ^ ((2:ℝ)/3)
  1950. * (Mmom X₁ (∏ p ∈ J, p) * Mmom X₂ (∏ p ∈ J, p) * Mmom X (∏ p ∈ J, p)) ^ ((1:ℝ)/3)
  1951. ≤ w ^ s
  1952. * (Mmom X₁ (∏ p ∈ J, p) * Mmom X₂ (∏ p ∈ J, p) * Mmom X (∏ p ∈ J, p)) ^ ((1:ℝ)/3) := by
  1953. intro J hJ
  1954. obtain ⟨hJP, hJc⟩ := Finset.mem_powersetCard.mp hJ
  1955. have h1 := prod_le_pow_of_le (J := J) hU0 (fun p hp => hPU p (hJP hp))
  1956. rw [hJc] at h1
  1957. exact mul_le_mul_of_nonneg_right h1 (Real.rpow_nonneg
  1958. (mul_nonneg (mul_nonneg (Mmom_nonneg _ _) (Mmom_nonneg _ _)) (Mmom_nonneg _ _)) _)
  1959. have hholder := holder3 (P.powersetCard s)
  1960. (f₁ := fun J => Mmom X₁ (∏ p ∈ J, p)) (f₂ := fun J => Mmom X₂ (∏ p ∈ J, p))
  1961. (f₃ := fun J => Mmom X (∏ p ∈ J, p))
  1962. (fun J _ => Mmom_nonneg _ _) (fun J _ => Mmom_nonneg _ _) (fun J _ => Mmom_nonneg _ _)
  1963. have hm₁ : ∑ J ∈ P.powersetCard s, Mmom X₁ (∏ p ∈ J, p) ≤ 2 * C * (a + ρ ^ s) := by
  1964. have hbnd := moment_avg_le hX₁ hR hN hcol₁ (s := s)
  1965. have h2 : C * (1 / (X₁.card:ℝ) + ρ ^ s) ≤ 2 * C * (a + ρ ^ s) := by
  1966. have hsum : 1 / (X₁.card:ℝ) + ρ ^ s ≤ 2 * (a + ρ ^ s) := by
  1967. have : (0:ℝ) ≤ ρ ^ s := by positivity
  1968. linarith
  1969. nlinarith [hC0]
  1970. linarith
  1971. have hm₂ : ∑ J ∈ P.powersetCard s, Mmom X₂ (∏ p ∈ J, p) ≤ 2 * C * (a + ρ ^ s) := by
  1972. have hbnd := moment_avg_le hX₂ hR hN hcol₂ (s := s)
  1973. have h2 : C * (1 / (X₂.card:ℝ) + ρ ^ s) ≤ 2 * C * (a + ρ ^ s) := by
  1974. have hsum : 1 / (X₂.card:ℝ) + ρ ^ s ≤ 2 * (a + ρ ^ s) := by
  1975. have : (0:ℝ) ≤ ρ ^ s := by positivity
  1976. linarith
  1977. nlinarith [hC0]
  1978. linarith
  1979. have hmX : ∑ J ∈ P.powersetCard s, Mmom X (∏ p ∈ J, p) ≤ C * (b + ρ ^ s) := by
  1980. have hbnd := moment_avg_le hX hR hN hcolX (s := s)
  1981. have h2 : C * (1 / (X.card:ℝ) + ρ ^ s) ≤ C * (b + ρ ^ s) := by
  1982. have hsum : 1 / (X.card:ℝ) + ρ ^ s ≤ b + ρ ^ s := by linarith
  1983. nlinarith [hC0]
  1984. linarith
  1985. have hnn₁ : 0 ≤ ∑ J ∈ P.powersetCard s, Mmom X₁ (∏ p ∈ J, p) :=
  1986. Finset.sum_nonneg fun J _ => Mmom_nonneg _ _
  1987. have hnn₂ : 0 ≤ ∑ J ∈ P.powersetCard s, Mmom X₂ (∏ p ∈ J, p) :=
  1988. Finset.sum_nonneg fun J _ => Mmom_nonneg _ _
  1989. have hnnX : 0 ≤ ∑ J ∈ P.powersetCard s, Mmom X (∏ p ∈ J, p) :=
  1990. Finset.sum_nonneg fun J _ => Mmom_nonneg _ _
  1991. have hA0 : 0 ≤ a + ρ ^ s := by positivity
  1992. have hB0 : 0 ≤ b + ρ ^ s := by positivity
  1993. have hprodle : (∑ J ∈ P.powersetCard s, Mmom X₁ (∏ p ∈ J, p))
  1994. * (∑ J ∈ P.powersetCard s, Mmom X₂ (∏ p ∈ J, p))
  1995. * (∑ J ∈ P.powersetCard s, Mmom X (∏ p ∈ J, p))
  1996. ≤ (2 * C * (a + ρ ^ s)) * (2 * C * (a + ρ ^ s)) * (C * (b + ρ ^ s)) := by
  1997. have h12 : (∑ J ∈ P.powersetCard s, Mmom X₁ (∏ p ∈ J, p))
  1998. * (∑ J ∈ P.powersetCard s, Mmom X₂ (∏ p ∈ J, p))
  1999. ≤ (2 * C * (a + ρ ^ s)) * (2 * C * (a + ρ ^ s)) :=
  2000. mul_le_mul hm₁ hm₂ hnn₂ (by positivity)
  2001. exact mul_le_mul h12 hmX hnnX (by positivity)
  2002. have hcube : ((∑ J ∈ P.powersetCard s, Mmom X₁ (∏ p ∈ J, p))
  2003. * (∑ J ∈ P.powersetCard s, Mmom X₂ (∏ p ∈ J, p))
  2004. * (∑ J ∈ P.powersetCard s, Mmom X (∏ p ∈ J, p))) ^ ((1:ℝ)/3)
  2005. ≤ 2 * C * ((a + ρ ^ s) ^ ((2:ℝ)/3) * (b + ρ ^ s) ^ ((1:ℝ)/3)) := by
  2006. refine le_trans (Real.rpow_le_rpow (mul_nonneg (mul_nonneg hnn₁ hnn₂) hnnX)
  2007. hprodle (by norm_num)) ?_
  2008. exact cube_root_mul hC0 hA0 hB0
  2009. calc ∑ J ∈ P.powersetCard s, (((∏ p ∈ J, p : ℕ)):ℝ) ^ ((2:ℝ)/3)
  2010. * (Mmom X₁ (∏ p ∈ J, p) * Mmom X₂ (∏ p ∈ J, p) * Mmom X (∏ p ∈ J, p)) ^ ((1:ℝ)/3)
  2011. ≤ ∑ J ∈ P.powersetCard s, w ^ s
  2012. * (Mmom X₁ (∏ p ∈ J, p) * Mmom X₂ (∏ p ∈ J, p) * Mmom X (∏ p ∈ J, p)) ^ ((1:ℝ)/3) :=
  2013. Finset.sum_le_sum hstep1
  2014. _ = w ^ s * ∑ J ∈ P.powersetCard s,
  2015. (Mmom X₁ (∏ p ∈ J, p) * Mmom X₂ (∏ p ∈ J, p) * Mmom X (∏ p ∈ J, p)) ^ ((1:ℝ)/3) := by
  2016. rw [Finset.mul_sum]
  2017. _ ≤ w ^ s * (2 * C * ((a + ρ ^ s) ^ ((2:ℝ)/3) * (b + ρ ^ s) ^ ((1:ℝ)/3))) :=
  2018. mul_le_mul_of_nonneg_left (le_trans hholder hcube) (by positivity)
  2019. _ = C * (2 * w ^ s * ((a + ρ ^ s) ^ ((2:ℝ)/3) * (b + ρ ^ s) ^ ((1:ℝ)/3))) := by ring
  2020. /-! ### The finite selection criterion -/
  2021. theorem rpow_npow_comm {x : ℝ} (hx : 0 ≤ x) (c : ℝ) (s : ℕ) : (x ^ s) ^ c = (x ^ c) ^ s := by
  2022. rw [← Real.rpow_natCast x s, ← Real.rpow_mul hx, ← Real.rpow_natCast (x ^ c) s,
  2023. ← Real.rpow_mul hx, mul_comm]
  2024. theorem one_div_card_le {X : Finset ℕ} {k : ℕ}
  2025. (h : (2:ℝ) ^ ((k:ℝ)/2 - 1) ≤ X.card) : 1 / (X.card:ℝ) ≤ 2 * (2:ℝ) ^ (-(k:ℝ)/2) := by
  2026. have hpos : (0:ℝ) < (2:ℝ) ^ ((k:ℝ)/2 - 1) := Real.rpow_pos_of_pos (by norm_num) _
  2027. have h1 : 1 / (X.card:ℝ) ≤ 1 / (2:ℝ) ^ ((k:ℝ)/2 - 1) := one_div_le_one_div_of_le hpos h
  2028. have h2 : (1:ℝ) / (2:ℝ)^((k:ℝ)/2 - 1) = 2 * (2:ℝ)^(-(k:ℝ)/2) := by
  2029. have hA : (1:ℝ) / (2:ℝ)^((k:ℝ)/2 - 1) = (2:ℝ)^(-((k:ℝ)/2 - 1)) := by
  2030. rw [Real.rpow_neg (by norm_num), one_div]
  2031. rw [hA, show -((k:ℝ)/2 - 1) = 1 + (-(k:ℝ)/2) by ring, Real.rpow_add (by norm_num),
  2032. Real.rpow_one]
  2033. linarith
  2034. theorem one_div_card_le' {X : Finset ℕ} {k : ℕ}
  2035. (h : (2:ℝ) ^ (k:ℝ) ≤ X.card) : 1 / (X.card:ℝ) ≤ (2:ℝ) ^ (-(k:ℝ)) := by
  2036. have hpos : (0:ℝ) < (2:ℝ) ^ (k:ℝ) := Real.rpow_pos_of_pos (by norm_num) _
  2037. have h1 : 1 / (X.card:ℝ) ≤ 1 / (2:ℝ) ^ (k:ℝ) := one_div_le_one_div_of_le hpos h
  2038. have h2 : (2:ℝ)^(-(k:ℝ)) = 1/(2:ℝ)^(k:ℝ) := by rw [Real.rpow_neg (by norm_num), one_div]
  2039. linarith
  2040. /-- **A finite selection criterion.** -/
  2041. theorem finite_selection {P : Finset ℕ} (hP : ∀ p ∈ P, p.Prime)
  2042. {U : ℝ} (hU0 : 0 ≤ U) (hPU : ∀ p ∈ P, (p:ℝ) ≤ U)
  2043. {X₁ X₂ X : Finset ℕ} (hX₁ : X₁.Nonempty) (hX₂ : X₂.Nonempty) (hX : X.Nonempty)
  2044. {k : ℕ}
  2045. (hX₁card : (2:ℝ) ^ ((k:ℝ)/2 - 1) ≤ X₁.card)
  2046. (hX₂card : (2:ℝ) ^ ((k:ℝ)/2 - 1) ≤ X₂.card)
  2047. (hXcard : (2:ℝ) ^ (k:ℝ) ≤ X.card)
  2048. {R t : ℕ} (hR : R ≤ P.card) (ht1 : 1 ≤ t) (htN : t ≤ P.card)
  2049. (hcol₁ : ∀ x ∈ X₁, ∀ y ∈ X₁, x ≠ y →
  2050. (P.filter (fun p : ℕ => (p:ℤ) ∣ (x:ℤ) - (y:ℤ))).card ≤ R)
  2051. (hcol₂ : ∀ x ∈ X₂, ∀ y ∈ X₂, x ≠ y →
  2052. (P.filter (fun p : ℕ => (p:ℤ) ∣ (x:ℤ) - (y:ℤ))).card ≤ R)
  2053. (hcolX : ∀ x ∈ X, ∀ y ∈ X, x ≠ y →
  2054. (P.filter (fun p : ℕ => (p:ℤ) ∣ (x:ℤ) - (y:ℤ))).card ≤ R)
  2055. (hnum : (2:ℝ) ^ (-(2*(k:ℝ))/3) * (1 + U ^ ((2:ℝ)/3)) ^ t
  2056. + (2:ℝ) ^ (-(k:ℝ)/3) * (1 + U ^ ((2:ℝ)/3) * ((R:ℝ)/P.card) ^ ((1:ℝ)/3)) ^ t
  2057. + (2:ℝ) ^ (-(k:ℝ)/3) * (1 + U ^ ((2:ℝ)/3) * ((R:ℝ)/P.card) ^ ((2:ℝ)/3)) ^ t
  2058. + (1 + U ^ ((2:ℝ)/3) * ((R:ℝ)/P.card)) ^ t - 1 < 1/2) :
  2059. ∃ I ∈ P.powersetCard t, ∑ d ∈ (∏ p ∈ I, p).divisors.filter (fun d => 1 < d),
  2060. (d:ℝ) ^ ((2:ℝ)/3) * (Mmom X₁ d * Mmom X₂ d * Mmom X d) ^ ((1:ℝ)/3) < 1 := by
  2061. classical
  2062. have hN : 0 < P.card := lt_of_lt_of_le ht1 htN
  2063. set N := P.card with hNdef
  2064. set g : ℕ → ℝ := fun d => (d:ℝ) ^ ((2:ℝ)/3)
  2065. * (Mmom X₁ d * Mmom X₂ d * Mmom X d) ^ ((1:ℝ)/3) with hg
  2066. set ρ : ℝ := (R:ℝ)/N with hρdef
  2067. have hρ0 : 0 ≤ ρ := by positivity
  2068. set w : ℝ := U ^ ((2:ℝ)/3) with hwdef
  2069. have hw0 : 0 ≤ w := Real.rpow_nonneg hU0 _
  2070. set r1 : ℝ := ρ ^ ((1:ℝ)/3) with hr1
  2071. set r2 : ℝ := ρ ^ ((2:ℝ)/3) with hr2
  2072. set a : ℝ := (2:ℝ) ^ (-(k:ℝ)/2) with hadef
  2073. set b : ℝ := (2:ℝ) ^ (-(k:ℝ)) with hbdef
  2074. have ha0 : 0 ≤ a := Real.rpow_nonneg (by norm_num) _
  2075. have hb0 : 0 ≤ b := Real.rpow_nonneg (by norm_num) _
  2076. set c1 : ℝ := (2:ℝ) ^ (-(2*(k:ℝ))/3) with hc1
  2077. set c2 : ℝ := (2:ℝ) ^ (-(k:ℝ)/3) with hc2
  2078. have hc10 : 0 ≤ c1 := Real.rpow_nonneg (by norm_num) _
  2079. have hc20 : 0 ≤ c2 := Real.rpow_nonneg (by norm_num) _
  2080. have ha23 : a ^ ((2:ℝ)/3) = c2 := by
  2081. rw [hadef, hc2, ← Real.rpow_mul (by norm_num : (0:ℝ) ≤ 2)]
  2082. ring_nf
  2083. have hb13 : b ^ ((1:ℝ)/3) = c2 := by
  2084. rw [hbdef, hc2, ← Real.rpow_mul (by norm_num : (0:ℝ) ≤ 2)]
  2085. ring_nf
  2086. have hc1eq : c2 * c2 = c1 := by
  2087. rw [hc1, hc2, ← Real.rpow_add (by norm_num : (0:ℝ) < 2)]
  2088. ring_nf
  2089. set D : ℕ → ℝ := fun s => 2 * (c1 * w ^ s + c2 * (w * r1) ^ s + c2 * (w * r2) ^ s
  2090. + (w * ρ) ^ s) with hD
  2091. have hkey : ∀ s : ℕ, ∑ J ∈ P.powersetCard s, g (∏ p ∈ J, p) ≤ (N.choose s : ℝ) * D s := by
  2092. intro s
  2093. have hmain := moment_prod_sum_bound hX₁ hX₂ hX hU0 hPU ha0 hb0
  2094. (one_div_card_le hX₁card) (one_div_card_le hX₂card) (one_div_card_le' hXcard)
  2095. hR hN hcol₁ hcol₂ hcolX (s := s)
  2096. refine le_trans hmain ?_
  2097. have hprod : (a + ρ ^ s) ^ ((2:ℝ)/3) * (b + ρ ^ s) ^ ((1:ℝ)/3)
  2098. ≤ c1 + c2 * r1 ^ s + r2 ^ s * c2 + ρ ^ s := by
  2099. have h := two_thirds_prod_le (x := a) (y := b) (r := ρ ^ s) ha0 hb0 (by positivity)
  2100. rw [ha23, hb13] at h
  2101. have e1 : (ρ ^ s) ^ ((1:ℝ)/3) = r1 ^ s := by rw [hr1, rpow_npow_comm hρ0]
  2102. have e2 : (ρ ^ s) ^ ((2:ℝ)/3) = r2 ^ s := by rw [hr2, rpow_npow_comm hρ0]
  2103. rw [e1, e2, hc1eq] at h
  2104. exact h
  2105. have hws : (0:ℝ) ≤ 2 * w ^ s := by positivity
  2106. have hstep : 2 * w ^ s * ((a + ρ ^ s) ^ ((2:ℝ)/3) * (b + ρ ^ s) ^ ((1:ℝ)/3)) ≤ D s := by
  2107. refine le_trans (mul_le_mul_of_nonneg_left hprod hws) (le_of_eq ?_)
  2108. simp only [hD, mul_pow]
  2109. ring
  2110. exact mul_le_mul_of_nonneg_left hstep (by positivity)
  2111. have hcardpos : 0 < N.choose t := Nat.choose_pos htN
  2112. have htotal : ∑ I ∈ P.powersetCard t,
  2113. (∑ d ∈ (∏ p ∈ I, p).divisors.filter (fun d => 1 < d), g d)
  2114. ≤ (N.choose t : ℝ) * ∑ s ∈ Finset.Icc 1 t, (t.choose s : ℝ) * D s := by
  2115. have hIeq : ∀ I ∈ P.powersetCard t,
  2116. (∑ d ∈ (∏ p ∈ I, p).divisors.filter (fun d => 1 < d), g d)
  2117. = ∑ s ∈ Finset.Icc 1 t, ∑ J ∈ I.powersetCard s, g (∏ p ∈ J, p) := by
  2118. intro I hI'
  2119. obtain ⟨hIP, hIc⟩ := Finset.mem_powersetCard.mp hI'
  2120. have hx := sum_divisors_prod_primes_filter (I := I) (fun p hp => hP p (hIP hp)) g
  2121. rw [hIc] at hx
  2122. exact hx
  2123. rw [Finset.sum_congr rfl hIeq, Finset.sum_comm, Finset.mul_sum]
  2124. refine Finset.sum_le_sum ?_
  2125. intro s hs'
  2126. have hst : s ≤ t := (Finset.mem_Icc.mp hs').2
  2127. rw [sum_nested hst (fun J => g (∏ p ∈ J, p))]
  2128. have hnn : (0:ℝ) ≤ ((N - s).choose (t - s) : ℝ) := by positivity
  2129. refine le_trans (mul_le_mul_of_nonneg_left (hkey s) hnn) (le_of_eq ?_)
  2130. have hid : (N.choose t) * (t.choose s) = (N.choose s) * ((N - s).choose (t - s)) :=
  2131. Nat.choose_mul hst
  2132. have heq : ((N - s).choose (t - s) : ℝ) * (N.choose s : ℝ)
  2133. = (N.choose t : ℝ) * (t.choose s : ℝ) := by
  2134. have hcast := congrArg (fun n : ℕ => (n : ℝ)) hid
  2135. push_cast at hcast
  2136. linarith
  2137. linear_combination (D s) * heq
  2138. have hexpand : ∑ s ∈ Finset.Icc 1 t, (t.choose s : ℝ) * D s
  2139. = 2 * (c1 * ((1+w)^t - 1) + c2 * ((1+w*r1)^t - 1) + c2 * ((1+w*r2)^t - 1)
  2140. + ((1+w*ρ)^t - 1)) := by
  2141. have hterm : ∀ s ∈ Finset.Icc 1 t, (t.choose s : ℝ) * D s
  2142. = 2 * (c1 * ((t.choose s:ℝ) * w^s) + c2 * ((t.choose s:ℝ) * (w*r1)^s)
  2143. + c2 * ((t.choose s:ℝ) * (w*r2)^s) + ((t.choose s:ℝ) * (w*ρ)^s)) := by
  2144. intro s _
  2145. simp only [hD]
  2146. ring
  2147. rw [Finset.sum_congr rfl hterm, ← Finset.mul_sum]
  2148. congr 1
  2149. rw [Finset.sum_add_distrib, Finset.sum_add_distrib, Finset.sum_add_distrib,
  2150. ← Finset.mul_sum, ← Finset.mul_sum, ← Finset.mul_sum,
  2151. sum_Icc_choose_pow, sum_Icc_choose_pow, sum_Icc_choose_pow, sum_Icc_choose_pow]
  2152. have hlt : ∑ s ∈ Finset.Icc 1 t, (t.choose s : ℝ) * D s < 1 := by
  2153. rw [hexpand]
  2154. linarith [hnum, hc10, hc20]
  2155. by_contra hcon
  2156. push_neg at hcon
  2157. have hge : ((P.powersetCard t).card : ℝ)
  2158. ≤ ∑ I ∈ P.powersetCard t, (∑ d ∈ (∏ p ∈ I, p).divisors.filter (fun d => 1 < d), g d) := by
  2159. calc ((P.powersetCard t).card : ℝ) = ∑ _I ∈ P.powersetCard t, (1:ℝ) := by
  2160. rw [Finset.sum_const, nsmul_eq_mul, mul_one]
  2161. _ ≤ _ := Finset.sum_le_sum hcon
  2162. rw [Finset.card_powersetCard] at hge
  2163. have hpos : (0:ℝ) < (N.choose t : ℝ) := by exact_mod_cast hcardpos
  2164. nlinarith [htotal, hlt, hge, hpos]
  2165. /-! ## The numerical threshold
  2166. For all large `k` the four terms occurring in the finite selection criterion are small,
  2167. uniformly for `0 ≤ ρ ≤ 32 k⁻⁵`.
  2168. -/
  2169. section Numerical
  2170. set_option maxRecDepth 10000
  2171. set_option maxHeartbeats 2000000
  2172. /-- The package of numerical bounds valid for all large `k`. -/
  2173. def NumP (k : ℕ) : Prop :=
  2174. K1P k ∧ 84 ≤ k ∧ 100 ≤ Real.log (Real.log k) ∧
  2175. (Real.log k) ^ ((2:ℝ)/3) * 10 ^ 6 ≤ Real.log k
  2176. theorem exp_hundred_large : (10:ℝ)^(18:ℕ) ≤ Real.exp 100 := by
  2177. have h1 : Real.exp 100 = (Real.exp 1) ^ (100:ℕ) := by
  2178. rw [← Real.exp_nat_mul]
  2179. norm_num
  2180. have h2 : (2:ℝ) ^ (100:ℕ) ≤ (Real.exp 1) ^ (100:ℕ) :=
  2181. pow_le_pow_left₀ (by norm_num) (by linarith [Real.exp_one_gt_d9]) 100
  2182. have h3 : (10:ℝ)^(18:ℕ) ≤ (2:ℝ)^(100:ℕ) := by norm_num
  2183. linarith [h1 ▸ h2]
  2184. theorem cube_root_big {u : ℝ} (hu : (10:ℝ)^(18:ℕ) ≤ u) : u ^ ((2:ℝ)/3) * 10 ^ 6 ≤ u := by
  2185. have hu0 : (0:ℝ) < u := lt_of_lt_of_le (by norm_num) hu
  2186. have h1 : ((10:ℝ)^(18:ℕ)) ^ ((1:ℝ)/3) ≤ u ^ ((1:ℝ)/3) :=
  2187. Real.rpow_le_rpow (by norm_num) hu (by norm_num)
  2188. have h2 : ((10:ℝ)^(18:ℕ)) ^ ((1:ℝ)/3) = 10 ^ 6 := by
  2189. rw [← Real.rpow_natCast (10:ℝ) 18, ← Real.rpow_mul (by norm_num)]
  2190. norm_num
  2191. rw [h2] at h1
  2192. have h3 : u ^ ((2:ℝ)/3) * u ^ ((1:ℝ)/3) = u := by
  2193. rw [← Real.rpow_add hu0]
  2194. norm_num
  2195. calc u ^ ((2:ℝ)/3) * 10 ^ 6 ≤ u ^ ((2:ℝ)/3) * u ^ ((1:ℝ)/3) :=
  2196. mul_le_mul_of_nonneg_left h1 (Real.rpow_nonneg hu0.le _)
  2197. _ = u := h3
  2198. theorem exists_NumP : ∃ K : ℕ, 3 ≤ K ∧ ∀ k : ℕ, K ≤ k → NumP k := by
  2199. obtain ⟨K₁, hK₁3, hK₁⟩ := exists_K1
  2200. refine ⟨max (max K₁ 84) ⌈Real.exp (Real.exp 100)⌉₊,
  2201. le_trans hK₁3 (le_trans (le_max_left _ _) (le_max_left _ _)), ?_⟩
  2202. intro k hk
  2203. have hk1 : K₁ ≤ k := le_trans (le_trans (le_max_left _ _) (le_max_left _ _)) hk
  2204. have hk84 : 84 ≤ k := le_trans (le_trans (le_max_right _ _) (le_max_left _ _)) hk
  2205. have hkexp : Real.exp (Real.exp 100) ≤ (k:ℝ) := by
  2206. have h1 : ⌈Real.exp (Real.exp 100)⌉₊ ≤ k := le_trans (le_max_right _ _) hk
  2207. exact le_trans (Nat.le_ceil _) (by exact_mod_cast h1)
  2208. have hu : Real.exp 100 ≤ Real.log k := by
  2209. have h := Real.log_le_log (Real.exp_pos _) hkexp
  2210. rwa [Real.log_exp] at h
  2211. have hu0 : (0:ℝ) < Real.log k := lt_of_lt_of_le (Real.exp_pos _) hu
  2212. have hloglog : 100 ≤ Real.log (Real.log k) := by
  2213. have h := Real.log_le_log (Real.exp_pos 100) hu
  2214. rwa [Real.log_exp] at h
  2215. exact ⟨hK₁ k hk1, hk84, hloglog, cube_root_big (le_trans exp_hundred_large hu)⟩
  2216. theorem exp_neg_four_lam : Real.exp (-(4*lam)) = 1/16 := by
  2217. have h : Real.log ((1:ℝ)/16) = -(4 * Real.log 2) := by
  2218. rw [Real.log_div one_ne_zero (by norm_num), Real.log_one,
  2219. show (16:ℝ) = 2^(4:ℕ) by norm_num, Real.log_pow]
  2220. push_cast; ring
  2221. rw [lam, ← h, Real.exp_log (by norm_num)]
  2222. theorem two_rpow_eq_exp (x : ℝ) : (2:ℝ) ^ x = Real.exp (lam * x) := by
  2223. rw [Real.rpow_def_of_pos (by norm_num), lam]
  2224. /-- If `1 + w X` is at most `exp c`, then its `t`-th power is at most `exp A`. -/
  2225. theorem term_bound {w X c A : ℝ} {t : ℕ} (hw : 0 < w) (hX : 0 < X)
  2226. (hlog : Real.log w + Real.log X + lam ≤ c) (hc1 : 1 ≤ c)
  2227. (hA : (t:ℝ) * c ≤ A) :
  2228. (1 + w * X) ^ t ≤ Real.exp A := by
  2229. have hexpc : Real.exp 1 ≤ Real.exp c := Real.exp_le_exp.mpr hc1
  2230. have he2 : (2:ℝ) ≤ Real.exp c := le_trans (by linarith [Real.exp_one_gt_d9]) hexpc
  2231. have hwX : w * X ≤ Real.exp c / 2 := by
  2232. have hlogwX : Real.log (w * X) = Real.log w + Real.log X := Real.log_mul hw.ne' hX.ne'
  2233. have h1 : w * X = Real.exp (Real.log (w * X)) := (Real.exp_log (by positivity)).symm
  2234. rw [h1, hlogwX]
  2235. have h2 : Real.exp c / 2 = Real.exp (c - lam) := by
  2236. rw [Real.exp_sub, lam, Real.exp_log (by norm_num)]
  2237. rw [h2]
  2238. exact Real.exp_le_exp.mpr (by linarith)
  2239. have hstep : 1 + w * X ≤ Real.exp c := by linarith
  2240. calc (1 + w * X) ^ t ≤ (Real.exp c) ^ t := pow_le_pow_left₀ (by positivity) hstep t
  2241. _ = Real.exp ((t:ℝ) * c) := by rw [← Real.exp_nat_mul]
  2242. _ ≤ Real.exp A := Real.exp_le_exp.mpr hA
  2243. /-- **Bounds for the four numerical terms.** -/
  2244. theorem numeric_terms {k : ℕ} (hK : NumP k) {ρ : ℝ} (hρ0 : 0 ≤ ρ) (hρ : ρ ≤ 32 / (k:ℝ)^5) :
  2245. (2:ℝ) ^ (-(2*(k:ℝ))/3) * (1 + (4*(Qp k:ℝ)) ^ ((2:ℝ)/3)) ^ (tp k)
  2246. + (2:ℝ) ^ (-(k:ℝ)/3) * (1 + (4*(Qp k:ℝ)) ^ ((2:ℝ)/3) * ρ ^ ((1:ℝ)/3)) ^ (tp k)
  2247. + (2:ℝ) ^ (-(k:ℝ)/3) * (1 + (4*(Qp k:ℝ)) ^ ((2:ℝ)/3) * ρ ^ ((2:ℝ)/3)) ^ (tp k)
  2248. + (1 + (4*(Qp k:ℝ)) ^ ((2:ℝ)/3) * ρ) ^ (tp k) - 1 < 1/2 := by
  2249. obtain ⟨hK1, hk84, hloglog, hcube⟩ := hK
  2250. obtain ⟨hu3, h800, -, -, hQlow, hQhigh, hlog8u, hlogden, hap1, hap2, -, -, ht1, htk⟩ := hK1
  2251. have hlam1 := lam_lt_one
  2252. have hlam0 := lam_pos
  2253. set u := Real.log k with hu
  2254. have hu0 : (0:ℝ) < u := by linarith
  2255. have hlogu0 : (0:ℝ) ≤ Real.log u := Real.log_nonneg (by linarith)
  2256. have hu100 : (100:ℝ) ≤ Real.log u := hloglog
  2257. have hubig : (1000:ℝ) ≤ u := by
  2258. have hexp : Real.exp 100 ≤ u := by
  2259. have h := Real.exp_le_exp.mpr hloglog
  2260. rwa [Real.exp_log hu0] at h
  2261. have h1 : (1000:ℝ) ≤ Real.exp 100 := by
  2262. have h2 : Real.exp 100 = (Real.exp 1) ^ (100:ℕ) := by
  2263. rw [← Real.exp_nat_mul]; norm_num
  2264. have h3 : (2:ℝ) ^ (100:ℕ) ≤ (Real.exp 1) ^ (100:ℕ) :=
  2265. pow_le_pow_left₀ (by norm_num) (by linarith [Real.exp_one_gt_d9]) 100
  2266. have h4 : (1000:ℝ) ≤ (2:ℝ)^(100:ℕ) := by norm_num
  2267. linarith [h2 ▸ h3]
  2268. linarith
  2269. have hu23 : u ^ ((2:ℝ)/3) ≤ u / 10^6 := by linarith [hcube]
  2270. have hlu : Real.log u ≤ u / 10^5 := by
  2271. have h1 : Real.log (u ^ ((2:ℝ)/3)) ≤ u ^ ((2:ℝ)/3) - 1 :=
  2272. Real.log_le_sub_one_of_pos (Real.rpow_pos_of_pos hu0 _)
  2273. rw [Real.log_rpow hu0] at h1
  2274. linarith [h1, hu23]
  2275. have hk3 : 3 ≤ k := by omega
  2276. have hkR : (0:ℝ) < (k:ℝ) := by positivity
  2277. have hk84R : (84:ℝ) ≤ (k:ℝ) := by exact_mod_cast hk84
  2278. have hQ0 : (0:ℝ) < (Qp k : ℝ) := by exact_mod_cast Qp_pos k
  2279. have hU0 : (0:ℝ) < 4 * (Qp k : ℝ) := by linarith
  2280. set W : ℝ := (4 * (Qp k:ℝ)) ^ ((2:ℝ)/3) with hWdef
  2281. have hW0 : 0 < W := Real.rpow_pos_of_pos hU0 _
  2282. have hlogW : Real.log W = (2/3) * Real.log (4 * (Qp k:ℝ)) := by
  2283. rw [hWdef, Real.log_rpow hU0]
  2284. have hlog4Q : Real.log (4 * (Qp k:ℝ)) ≤ 3 + 6*u + Real.log u := by
  2285. have h1 : 4 * (Qp k:ℝ) ≤ 8 * (k:ℝ)^6 * u := by linarith
  2286. have h2 : Real.log (4 * (Qp k:ℝ)) ≤ Real.log (8 * (k:ℝ)^6 * u) := Real.log_le_log hU0 h1
  2287. have h3 : Real.log (8 * (k:ℝ)^6 * u) = Real.log 8 + 6*u + Real.log u := by
  2288. rw [Real.log_mul (by positivity) (by positivity), Real.log_mul (by norm_num) (by positivity),
  2289. Real.log_pow, ← hu]
  2290. push_cast
  2291. ring
  2292. have h4 : Real.log 8 ≤ 3 := log_three_lt_log_eight
  2293. linarith [h3 ▸ h2]
  2294. have hlogW' : Real.log W ≤ 2 + 4*u + (2/3)*Real.log u := by
  2295. rw [hlogW]; linarith
  2296. set t := tp k with htdef
  2297. have htnn : (0:ℝ) ≤ (t:ℝ) := by positivity
  2298. have hden : (0:ℝ) < 7*u + 3*Real.log u := by linarith
  2299. have hapeq : ap k = lam / (7*u + 3*Real.log u) := rfl
  2300. have ht : (t:ℝ) ≤ lam * k / (7*u + 3*Real.log u) := by
  2301. have h1 : (t:ℝ) ≤ ap k * k := tp_le_ap_mul hk3
  2302. rw [hapeq] at h1
  2303. calc (t:ℝ) ≤ lam / (7*u + 3*Real.log u) * k := h1
  2304. _ = lam * k / (7*u + 3*Real.log u) := by ring
  2305. have ht7 : (t:ℝ) ≤ lam * k / (7*u) := by
  2306. have h1 : lam * (k:ℝ) / (7*u + 3*Real.log u) ≤ lam * k / (7*u) :=
  2307. div_le_div_of_nonneg_left (by positivity) (by positivity) (by linarith)
  2308. linarith
  2309. -- the powers of `32 / k^5`
  2310. have hpow0 : (0:ℝ) < 32 / (k:ℝ)^5 := by positivity
  2311. set Y1 : ℝ := (32 / (k:ℝ)^5) ^ ((1:ℝ)/3) with hY1def
  2312. set Y2 : ℝ := (32 / (k:ℝ)^5) ^ ((2:ℝ)/3) with hY2def
  2313. have hY10 : 0 < Y1 := Real.rpow_pos_of_pos hpow0 _
  2314. have hY20 : 0 < Y2 := Real.rpow_pos_of_pos hpow0 _
  2315. have hlog32 : Real.log (32 / (k:ℝ)^5) = 5*lam - 5*u := by
  2316. have e1 : Real.log (32 / (k:ℝ)^5) = Real.log 32 - Real.log ((k:ℝ)^5) :=
  2317. Real.log_div (by norm_num) (by positivity)
  2318. have e2 : Real.log (32:ℝ) = 5 * lam := by
  2319. rw [show (32:ℝ) = 2^(5:ℕ) by norm_num, Real.log_pow, lam]
  2320. push_cast; ring
  2321. have e3 : Real.log ((k:ℝ)^5) = 5 * u := by
  2322. rw [Real.log_pow, ← hu]
  2323. push_cast; ring
  2324. rw [e1, e2, e3]
  2325. have hlogY1 : Real.log Y1 = (1/3) * (5*lam - 5*u) := by
  2326. rw [hY1def, Real.log_rpow hpow0, hlog32]
  2327. have hlogY2 : Real.log Y2 = (2/3) * (5*lam - 5*u) := by
  2328. rw [hY2def, Real.log_rpow hpow0, hlog32]
  2329. -- Term 1
  2330. have hT1 : (2:ℝ) ^ (-(2*(k:ℝ))/3) * (1 + W) ^ t ≤ 1/16 := by
  2331. have hpow : (1 + W) ^ t ≤ Real.exp (13*lam*(k:ℝ)/21) := by
  2332. have hrw : (1:ℝ) + W = 1 + W * 1 := by ring
  2333. rw [hrw]
  2334. refine term_bound (c := 13*u/3) hW0 one_pos ?_ ?_ ?_
  2335. · rw [Real.log_one]
  2336. linarith
  2337. · linarith
  2338. · have h := mul_le_mul_of_nonneg_right ht7 (by positivity : (0:ℝ) ≤ 13*u/3)
  2339. calc (t:ℝ) * (13*u/3) ≤ lam * (k:ℝ) / (7*u) * (13*u/3) := h
  2340. _ = 13*lam*(k:ℝ)/21 := by field_simp; ring
  2341. calc (2:ℝ) ^ (-(2*(k:ℝ))/3) * (1 + W) ^ t
  2342. = Real.exp (lam * (-(2*(k:ℝ))/3)) * (1 + W) ^ t := by rw [two_rpow_eq_exp]
  2343. _ ≤ Real.exp (lam * (-(2*(k:ℝ))/3)) * Real.exp (13*lam*(k:ℝ)/21) :=
  2344. mul_le_mul_of_nonneg_left hpow (Real.exp_pos _).le
  2345. _ = Real.exp (lam * (-(2*(k:ℝ))/3) + 13*lam*(k:ℝ)/21) := (Real.exp_add _ _).symm
  2346. _ ≤ Real.exp (-(4*lam)) := Real.exp_le_exp.mpr (by nlinarith)
  2347. _ = 1/16 := exp_neg_four_lam
  2348. -- Term 2
  2349. have hT2 : (2:ℝ) ^ (-(k:ℝ)/3) * (1 + W * ρ ^ ((1:ℝ)/3)) ^ t ≤ 1/16 := by
  2350. have hmono : (1 + W * ρ ^ ((1:ℝ)/3)) ^ t ≤ (1 + W * Y1) ^ t := by
  2351. refine pow_le_pow_left₀ (by positivity) ?_ t
  2352. have h1 : ρ ^ ((1:ℝ)/3) ≤ Y1 := Real.rpow_le_rpow hρ0 hρ (by norm_num)
  2353. linarith [mul_le_mul_of_nonneg_left h1 hW0.le]
  2354. have hpow : (1 + W * Y1) ^ t ≤ Real.exp (lam * (k:ℝ)/3 - 4*lam) := by
  2355. refine term_bound (c := 5 + (7/3)*u + (2/3)*Real.log u) hW0 hY10 ?_ ?_ ?_
  2356. · rw [hlogY1]; linarith
  2357. · linarith
  2358. · have hc2den : 5 + (7/3)*u + (2/3)*Real.log u
  2359. ≤ (7*u + 3*Real.log u)/3 - (1/6)*Real.log u := by linarith
  2360. have hS : (0:ℝ) ≤ lam * (k:ℝ) / (7*u + 3*Real.log u) := by positivity
  2361. have h1 : (t:ℝ) * (5 + (7/3)*u + (2/3)*Real.log u)
  2362. ≤ (lam * (k:ℝ) / (7*u + 3*Real.log u))
  2363. * ((7*u + 3*Real.log u)/3 - (1/6)*Real.log u) :=
  2364. mul_le_mul ht hc2den (by linarith) hS
  2365. have h2 : (lam * (k:ℝ) / (7*u + 3*Real.log u))
  2366. * ((7*u + 3*Real.log u)/3 - (1/6)*Real.log u)
  2367. = lam * (k:ℝ)/3 - lam * (k:ℝ) * Real.log u / (6*(7*u + 3*Real.log u)) := by
  2368. field_simp
  2369. have h3 : 4*lam ≤ lam * (k:ℝ) * Real.log u / (6*(7*u + 3*Real.log u)) := by
  2370. rw [le_div_iff₀ (by positivity)]
  2371. have hLu : Real.log u ≤ u := by linarith
  2372. have hstep1 : 4*lam * (6*(7*u + 3*Real.log u)) ≤ lam * (240*u) := by
  2373. nlinarith [mul_le_mul_of_nonneg_left hLu hlam0.le]
  2374. have hB : 240*u ≤ (k:ℝ) * Real.log u := by
  2375. have hmul : 800*u^2 * 100 ≤ (k:ℝ) * Real.log u :=
  2376. mul_le_mul h800 hu100 (by norm_num) (by positivity)
  2377. nlinarith [hmul]
  2378. have hstep2 : lam * (240*u) ≤ lam * ((k:ℝ) * Real.log u) :=
  2379. mul_le_mul_of_nonneg_left hB hlam0.le
  2380. nlinarith [hstep1, hstep2]
  2381. linarith
  2382. calc (2:ℝ) ^ (-(k:ℝ)/3) * (1 + W * ρ ^ ((1:ℝ)/3)) ^ t
  2383. = Real.exp (lam * (-(k:ℝ)/3)) * (1 + W * ρ ^ ((1:ℝ)/3)) ^ t := by rw [two_rpow_eq_exp]
  2384. _ ≤ Real.exp (lam * (-(k:ℝ)/3)) * Real.exp (lam * (k:ℝ)/3 - 4*lam) :=
  2385. mul_le_mul_of_nonneg_left (le_trans hmono hpow) (Real.exp_pos _).le
  2386. _ = Real.exp (lam * (-(k:ℝ)/3) + (lam * (k:ℝ)/3 - 4*lam)) := (Real.exp_add _ _).symm
  2387. _ ≤ Real.exp (-(4*lam)) := Real.exp_le_exp.mpr (by linarith)
  2388. _ = 1/16 := exp_neg_four_lam
  2389. -- Term 3
  2390. have hT3 : (2:ℝ) ^ (-(k:ℝ)/3) * (1 + W * ρ ^ ((2:ℝ)/3)) ^ t ≤ 1/16 := by
  2391. have hmono : (1 + W * ρ ^ ((2:ℝ)/3)) ^ t ≤ (1 + W * Y2) ^ t := by
  2392. refine pow_le_pow_left₀ (by positivity) ?_ t
  2393. have h1 : ρ ^ ((2:ℝ)/3) ≤ Y2 := Real.rpow_le_rpow hρ0 hρ (by norm_num)
  2394. linarith [mul_le_mul_of_nonneg_left h1 hW0.le]
  2395. have hpow : (1 + W * Y2) ^ t ≤ Real.exp (2*lam*(k:ℝ)/7) := by
  2396. refine term_bound (c := 2*u) hW0 hY20 ?_ ?_ ?_
  2397. · rw [hlogY2]; linarith
  2398. · linarith
  2399. · have h := mul_le_mul_of_nonneg_right ht7 (by positivity : (0:ℝ) ≤ 2*u)
  2400. calc (t:ℝ) * (2*u) ≤ lam * (k:ℝ) / (7*u) * (2*u) := h
  2401. _ = 2*lam*(k:ℝ)/7 := by field_simp
  2402. calc (2:ℝ) ^ (-(k:ℝ)/3) * (1 + W * ρ ^ ((2:ℝ)/3)) ^ t
  2403. = Real.exp (lam * (-(k:ℝ)/3)) * (1 + W * ρ ^ ((2:ℝ)/3)) ^ t := by rw [two_rpow_eq_exp]
  2404. _ ≤ Real.exp (lam * (-(k:ℝ)/3)) * Real.exp (2*lam*(k:ℝ)/7) :=
  2405. mul_le_mul_of_nonneg_left (le_trans hmono hpow) (Real.exp_pos _).le
  2406. _ = Real.exp (lam * (-(k:ℝ)/3) + 2*lam*(k:ℝ)/7) := (Real.exp_add _ _).symm
  2407. _ ≤ Real.exp (-(4*lam)) := Real.exp_le_exp.mpr (by nlinarith)
  2408. _ = 1/16 := exp_neg_four_lam
  2409. -- Term 4
  2410. have hT4 : (1 + W * ρ) ^ t ≤ 17/16 := by
  2411. have hWle : W ≤ Real.exp 2 * (k:ℝ)^4 * u ^ ((2:ℝ)/3) := by
  2412. have h1 : W ≤ Real.exp (2 + 4*u + (2/3)*Real.log u) := by
  2413. have h := Real.exp_le_exp.mpr hlogW'
  2414. rwa [Real.exp_log hW0] at h
  2415. have h2 : Real.exp (2 + 4*u + (2/3)*Real.log u)
  2416. = Real.exp 2 * (k:ℝ)^4 * u ^ ((2:ℝ)/3) := by
  2417. rw [Real.exp_add, Real.exp_add]
  2418. congr 1
  2419. · congr 1
  2420. rw [hu, show (4:ℝ)*Real.log (k:ℝ) = ((4:ℕ):ℝ)*Real.log (k:ℝ) by push_cast; ring,
  2421. Real.exp_nat_mul, Real.exp_log hkR]
  2422. · rw [Real.rpow_def_of_pos hu0]
  2423. ring_nf
  2424. linarith [h2 ▸ h1]
  2425. have hsmall : (t:ℝ) * (W * ρ) ≤ 1/100 := by
  2426. have hWρ : W * ρ ≤ (Real.exp 2 * (k:ℝ)^4 * u ^ ((2:ℝ)/3)) * (32 / (k:ℝ)^5) := by
  2427. have h1 : W * ρ ≤ W * (32 / (k:ℝ)^5) := mul_le_mul_of_nonneg_left hρ hW0.le
  2428. have h2 : W * (32 / (k:ℝ)^5) ≤ (Real.exp 2 * (k:ℝ)^4 * u ^ ((2:ℝ)/3)) * (32 / (k:ℝ)^5) :=
  2429. mul_le_mul_of_nonneg_right hWle (by positivity)
  2430. linarith
  2431. have htk7 : (t:ℝ) ≤ (k:ℝ) / (7*u) := by
  2432. have h1 : lam * (k:ℝ) / (7*u) ≤ (k:ℝ) / (7*u) := by
  2433. apply div_le_div_of_nonneg_right ?_ (by positivity)
  2434. nlinarith
  2435. linarith
  2436. have he2 : Real.exp 2 ≤ 8 := by
  2437. have h1 : Real.exp 2 = Real.exp 1 * Real.exp 1 := by
  2438. rw [← Real.exp_add]; norm_num
  2439. nlinarith [Real.exp_one_lt_d9, Real.exp_pos 1]
  2440. have hfinal : ((k:ℝ) / (7*u)) * ((Real.exp 2 * (k:ℝ)^4 * u ^ ((2:ℝ)/3)) * (32 / (k:ℝ)^5))
  2441. ≤ 1/100 := by
  2442. have hid : ((k:ℝ) / (7*u)) * ((Real.exp 2 * (k:ℝ)^4 * u ^ ((2:ℝ)/3)) * (32 / (k:ℝ)^5))
  2443. = (32 * Real.exp 2 / 7) * (u ^ ((2:ℝ)/3) / u) := by
  2444. field_simp
  2445. rw [hid]
  2446. have h1 : u ^ ((2:ℝ)/3) / u ≤ 1 / 10^6 := by
  2447. rw [div_le_div_iff₀ hu0 (by norm_num)]
  2448. nlinarith [hu23]
  2449. have h2 : (0:ℝ) ≤ 32 * Real.exp 2 / 7 := by positivity
  2450. nlinarith [h1, h2, he2]
  2451. calc (t:ℝ) * (W * ρ)
  2452. ≤ ((k:ℝ) / (7*u)) * ((Real.exp 2 * (k:ℝ)^4 * u ^ ((2:ℝ)/3)) * (32 / (k:ℝ)^5)) := by
  2453. apply mul_le_mul htk7 hWρ (by positivity) (by positivity)
  2454. _ ≤ 1/100 := hfinal
  2455. have hexp : (1 + W * ρ) ^ t ≤ Real.exp ((t:ℝ) * (W * ρ)) := by
  2456. calc (1 + W * ρ) ^ t ≤ (Real.exp (W * ρ)) ^ t := by
  2457. refine pow_le_pow_left₀ (by positivity) ?_ t
  2458. linarith [Real.add_one_le_exp (W * ρ)]
  2459. _ = Real.exp ((t:ℝ) * (W * ρ)) := by rw [← Real.exp_nat_mul]
  2460. have hup : Real.exp ((t:ℝ) * (W * ρ)) ≤ 17/16 := by
  2461. have h1 : Real.exp ((t:ℝ) * (W * ρ)) ≤ Real.exp (1/100) := Real.exp_le_exp.mpr hsmall
  2462. have h2 : Real.exp (-(1/100:ℝ)) * Real.exp (1/100 : ℝ) = 1 := by
  2463. rw [← Real.exp_add]; norm_num
  2464. have h3 : (99/100 : ℝ) ≤ Real.exp (-(1/100:ℝ)) := by
  2465. linarith [Real.add_one_le_exp (-(1/100:ℝ))]
  2466. have h4 : (0:ℝ) < Real.exp (1/100 : ℝ) := Real.exp_pos _
  2467. nlinarith [h1, h2, h3, h4]
  2468. linarith [hexp, hup]
  2469. linarith [hT1, hT2, hT3, hT4]
  2470. end Numerical
  2471. /-! ## Selection of the modulus
  2472. The analytic core: for all large `k`, every odd squarefree `V` with `ω(V) = k` and prime
  2473. factors at most `4Q(k)` admits a modulus `A`, a product of `t(k)` primes from
  2474. `(Q(k), 4Q(k)]`, such that every residue class modulo `A` is represented as
  2475. `z₀ + 2z₁ + 4z₂ + 8z₃` with divisors `zᵢ` of `V`.
  2476. -/
  2477. /-- **Available primes**: the interval `(Q(k), 4Q(k)]` contains many primes. -/
  2478. theorem exists_K2 : ∃ K₂ : ℕ, 3 ≤ K₂ ∧ ∀ k : ℕ, K₂ ≤ k →
  2479. (k : ℝ) ^ 6 / 8 ≤ (((Finset.Ioc (Qp k) (4 * Qp k)).filter Nat.Prime).card : ℝ) := by
  2480. obtain ⟨q₀, hq₀2, hq₀⟩ := exists_q0
  2481. obtain ⟨K₁, hK₁3, hK₁⟩ := exists_K1
  2482. refine ⟨max K₁ q₀, le_trans hK₁3 (le_max_left _ _), ?_⟩
  2483. intro k hk
  2484. have hk1 : K₁ ≤ k := le_trans (le_max_left _ _) hk
  2485. have hkq : q₀ ≤ k := le_trans (le_max_right _ _) hk
  2486. obtain ⟨hu3, -, -, -, hQlow, -, hvi, -⟩ := hK₁ k hk1
  2487. set u := Real.log k with hu
  2488. have hk3 : 3 ≤ k := le_trans hK₁3 hk1
  2489. have hkR3 : (3:ℝ) ≤ (k:ℝ) := by exact_mod_cast hk3
  2490. have hk0 : (0:ℝ) < (k:ℝ) := by linarith
  2491. have hk6 : (k:ℝ) ≤ (k:ℝ) ^ 6 := by
  2492. have h1 : (1:ℝ) ≤ (k:ℝ) := by linarith
  2493. have h5 : (1:ℝ) ≤ (k:ℝ) ^ 5 := one_le_pow₀ h1
  2494. have hrw : (k:ℝ) ^ 6 = (k:ℝ) ^ 5 * (k:ℝ) := by ring
  2495. rw [hrw]
  2496. calc (k:ℝ) = 1 * (k:ℝ) := by ring
  2497. _ ≤ (k:ℝ) ^ 5 * (k:ℝ) := mul_le_mul_of_nonneg_right h5 (le_of_lt hk0)
  2498. have hQk : (k:ℝ) ≤ (Qp k : ℝ) := by
  2499. have h1 : (k:ℝ) ^ 6 * 1 ≤ (k:ℝ) ^ 6 * u :=
  2500. mul_le_mul_of_nonneg_left (by linarith) (by positivity)
  2501. linarith
  2502. have hQq₀ : q₀ ≤ Qp k := by
  2503. have h1 : (q₀ : ℝ) ≤ (k:ℝ) := by exact_mod_cast hkq
  2504. have : (q₀ : ℝ) ≤ (Qp k : ℝ) := le_trans h1 hQk
  2505. exact_mod_cast this
  2506. have hcount := hq₀ (Qp k) hQq₀
  2507. have hQ1 : (1:ℝ) ≤ (Qp k : ℝ) := by
  2508. have := Qp_pos k
  2509. exact_mod_cast this
  2510. have hlogpos : 0 < Real.log (4 * (Qp k : ℝ)) := by
  2511. refine Real.log_pos ?_
  2512. linarith
  2513. refine le_trans ?_ hcount
  2514. rw [div_le_div_iff₀ (by norm_num) hlogpos]
  2515. calc (k:ℝ) ^ 6 * Real.log (4 * (Qp k : ℝ)) ≤ (k:ℝ) ^ 6 * (8 * u) := by
  2516. apply mul_le_mul_of_nonneg_left hvi (by positivity)
  2517. _ = 8 * ((k:ℝ) ^ 6 * u) := by ring
  2518. _ ≤ 8 * (Qp k : ℝ) := by linarith
  2519. _ = (Qp k : ℝ) * 8 := by ring
  2520. /-- The primes in `(Q(k), 4Q(k)]` that neither divide `V` nor equal `pstar`. -/
  2521. theorem available_primes {k : ℕ}
  2522. (hK2 : (k:ℝ)^6/8 ≤ (((Finset.Ioc (Qp k) (4 * Qp k)).filter Nat.Prime).card : ℝ))
  2523. (h16 : 16 * ((k:ℝ) + 1) ≤ (k:ℝ)^6)
  2524. {V : ℕ} (hV0 : V ≠ 0) (hVcard : V.primeFactors.card = k) (pstar : ℕ) :
  2525. ∃ P : Finset ℕ, (∀ p ∈ P, p.Prime ∧ Qp k < p ∧ p ≤ 4 * Qp k ∧ ¬ (p ∣ V) ∧ p ≠ pstar) ∧
  2526. (k:ℝ)^6/16 ≤ (P.card : ℝ) := by
  2527. classical
  2528. set S := (Finset.Ioc (Qp k) (4 * Qp k)).filter Nat.Prime with hS
  2529. set P := S.filter (fun p => ¬ (p ∣ V) ∧ p ≠ pstar) with hP
  2530. have hPS : P ⊆ S := Finset.filter_subset _ _
  2531. have hsub : S \ P ⊆ insert pstar V.primeFactors := by
  2532. intro p hp
  2533. rw [Finset.mem_sdiff] at hp
  2534. obtain ⟨hpS, hpP⟩ := hp
  2535. have hpprime : p.Prime := (Finset.mem_filter.mp hpS).2
  2536. rw [hP, Finset.mem_filter] at hpP
  2537. push_neg at hpP
  2538. have hcon := hpP hpS
  2539. rw [Finset.mem_insert]
  2540. by_cases hdvd : p ∣ V
  2541. · exact Or.inr (Nat.mem_primeFactors.mpr ⟨hpprime, hdvd, hV0⟩)
  2542. · exact Or.inl (hcon hdvd)
  2543. have hcard1 : (S \ P).card ≤ k + 1 := by
  2544. calc (S \ P).card ≤ (insert pstar V.primeFactors).card := Finset.card_le_card hsub
  2545. _ ≤ V.primeFactors.card + 1 := Finset.card_insert_le _ _
  2546. _ = k + 1 := by rw [hVcard]
  2547. have hcard2 : S.card = P.card + (S \ P).card := by
  2548. rw [add_comm]
  2549. exact (Finset.card_sdiff_add_card_eq_card hPS).symm
  2550. have hcard3 : (S.card : ℝ) ≤ (P.card : ℝ) + ((k:ℝ) + 1) := by
  2551. have h1 : (S.card : ℝ) = (P.card : ℝ) + ((S \ P).card : ℝ) := by exact_mod_cast hcard2
  2552. have h2 : (((S \ P).card : ℕ) : ℝ) ≤ (k:ℝ) + 1 := by exact_mod_cast hcard1
  2553. linarith
  2554. refine ⟨P, ?_, ?_⟩
  2555. · intro p hp
  2556. have hpS := hPS hp
  2557. have h1 := Finset.mem_filter.mp hpS
  2558. have h2 := (Finset.mem_filter.mp hp).2
  2559. have h3 := Finset.mem_Ioc.mp h1.1
  2560. exact ⟨h1.2, h3.1, h3.2, h2.1, h2.2⟩
  2561. · linarith
  2562. /-- **Selection of the modulus.** -/
  2563. theorem exists_K4 : ∃ K₄ : ℕ, 3 ≤ K₄ ∧ ∀ k : ℕ, K₄ ≤ k → ∀ pstar : ℕ, pstar.Prime → Odd pstar →
  2564. ∀ V : ℕ, 0 < V → Odd V → Squarefree V → V.primeFactors.card = k →
  2565. (∀ p ∈ V.primeFactors, p ≤ 4 * Qp k) →
  2566. ∃ A : ℕ, 1 < A ∧ Odd A ∧ Squarefree A ∧ Nat.Coprime A V ∧ ¬ (pstar ∣ A) ∧
  2567. A.primeFactors.card = tp k ∧
  2568. (∀ p ∈ A.primeFactors, Qp k < p ∧ p ≤ 4 * Qp k) ∧
  2569. ∀ c : ℕ, c < A → ∃ z₀ ∈ V.divisors, ∃ z₁ ∈ V.divisors, ∃ z₂ ∈ V.divisors,
  2570. ∃ z₃ ∈ V.divisors, (z₀ + 2 * z₁ + 4 * z₂ + 8 * z₃) % A = c % A := by
  2571. classical
  2572. obtain ⟨K₂, hK₂3, hK₂⟩ := exists_K2
  2573. obtain ⟨KN, hKN3, hKN⟩ := exists_NumP
  2574. refine ⟨max K₂ KN, le_trans hK₂3 (le_max_left _ _), ?_⟩
  2575. intro k hk pstar hpstarP hpstarOdd V hV0 hVodd hVsq hVcard hVle
  2576. have hK2k := hK₂ k (le_trans (le_max_left _ _) hk)
  2577. have hNum : NumP k := hKN k (le_trans (le_max_right _ _) hk)
  2578. have hK1 := hNum.1
  2579. have hk84 := hNum.2.1
  2580. obtain ⟨hu3, h800, h16, h32, hQlow, hQhigh, -, -, -, -, -, -, ht1, htk⟩ := hK1
  2581. have hkR : (0:ℝ) < (k:ℝ) := by
  2582. have : 0 < k := by omega
  2583. exact_mod_cast this
  2584. have hk84R : (84:ℝ) ≤ (k:ℝ) := by exact_mod_cast hk84
  2585. have hQ4 : 4 ≤ Qp k := by
  2586. have h1 : (4:ℝ) ≤ (Qp k : ℝ) := by nlinarith
  2587. exact_mod_cast h1
  2588. -- the available primes
  2589. obtain ⟨P, hPprop, hPcard⟩ := available_primes hK2k h16 hV0.ne' hVcard pstar
  2590. have hNpos : (0:ℝ) < (P.card : ℝ) := by nlinarith
  2591. have hkN : k ≤ P.card := by
  2592. have h1 : (k:ℝ) ≤ (P.card : ℝ) := by nlinarith
  2593. exact_mod_cast h1
  2594. have hRN : 2 * k ≤ P.card := by
  2595. have h1 : ((2*k : ℕ) : ℝ) ≤ (P.card : ℝ) := by push_cast; nlinarith
  2596. exact_mod_cast h1
  2597. have htN : tp k ≤ P.card := le_trans htk hkN
  2598. have hρ0 : (0:ℝ) ≤ ((2*k : ℕ) : ℝ) / (P.card : ℝ) := by positivity
  2599. have hρle : ((2*k : ℕ) : ℝ) / (P.card : ℝ) ≤ 32 / (k:ℝ)^5 := by
  2600. rw [div_le_div_iff₀ hNpos (by positivity)]
  2601. push_cast
  2602. nlinarith [hPcard]
  2603. -- splitting the prime factors
  2604. obtain ⟨V₁, V₂, hV₁0, hV₂0, hV12, hcop, hV₁card, hV₂card⟩ := factor_split hVsq hVcard
  2605. have hV₁dvd : V₁ ∣ V := ⟨V₂, hV12.symm⟩
  2606. have hV₂dvd : V₂ ∣ V := ⟨V₁, by rw [← hV12]; ring⟩
  2607. have hsq₁ : Squarefree V₁ := hVsq.squarefree_of_dvd hV₁dvd
  2608. have hsq₂ : Squarefree V₂ := hVsq.squarefree_of_dvd hV₂dvd
  2609. have hX₁ne : (V₁.divisors).Nonempty := ⟨1, Nat.one_mem_divisors.mpr hV₁0.ne'⟩
  2610. have hX₂ne : (V₂.divisors).Nonempty := ⟨1, Nat.one_mem_divisors.mpr hV₂0.ne'⟩
  2611. have hXne : (V.divisors).Nonempty := ⟨1, Nat.one_mem_divisors.mpr hV0.ne'⟩
  2612. have hcard₁ : (V₁.divisors).card = 2 ^ (k/2) := by
  2613. rw [card_divisors_squarefree hV₁0.ne' hsq₁, hV₁card]
  2614. have hcard₂ : (V₂.divisors).card = 2 ^ (k - k/2) := by
  2615. rw [card_divisors_squarefree hV₂0.ne' hsq₂, hV₂card]
  2616. have hcardV : (V.divisors).card = 2 ^ k := by
  2617. rw [card_divisors_squarefree hV0.ne' hVsq, hVcard]
  2618. have hpow : ∀ m : ℕ, ((2:ℝ))^((m:ℕ):ℝ) = ((2^m : ℕ) : ℝ) := by
  2619. intro m; rw [Real.rpow_natCast]; push_cast; ring
  2620. have hX₁bound : (2:ℝ) ^ ((k:ℝ)/2 - 1) ≤ ((V₁.divisors).card : ℝ) := by
  2621. rw [hcard₁, ← hpow (k/2)]
  2622. refine (Real.rpow_le_rpow_left_iff (by norm_num)).mpr ?_
  2623. have h1 : (k:ℝ) ≤ 2 * ((k/2 : ℕ) : ℝ) + 1 := by
  2624. have h2 : k ≤ 2 * (k/2) + 1 := by omega
  2625. exact_mod_cast h2
  2626. linarith
  2627. have hX₂bound : (2:ℝ) ^ ((k:ℝ)/2 - 1) ≤ ((V₂.divisors).card : ℝ) := by
  2628. rw [hcard₂, ← hpow (k - k/2)]
  2629. refine (Real.rpow_le_rpow_left_iff (by norm_num)).mpr ?_
  2630. have h1 : (k:ℝ) ≤ 2 * ((k - k/2 : ℕ) : ℝ) := by
  2631. have h2 : k ≤ 2 * (k - k/2) := by omega
  2632. exact_mod_cast h2
  2633. linarith
  2634. have hXbound : (2:ℝ) ^ (k:ℝ) ≤ ((V.divisors).card : ℝ) := by
  2635. rw [hcardV, ← hpow k]
  2636. -- the collision hypotheses
  2637. have hPprime : ∀ p ∈ P, p.Prime ∧ Qp k < p := fun p hp => ⟨(hPprop p hp).1, (hPprop p hp).2.1⟩
  2638. have hcolV : ∀ x ∈ V.divisors, ∀ y ∈ V.divisors, x ≠ y →
  2639. (P.filter (fun p : ℕ => (p:ℤ) ∣ (x:ℤ) - (y:ℤ))).card ≤ 2 * k :=
  2640. fun x hx y hy hxy => prime_divisors_of_diff hVsq hVcard hVle hQ4 hx hy hxy hPprime
  2641. have hsub₁ : V₁.divisors ⊆ V.divisors := Nat.divisors_subset_of_dvd hV0.ne' hV₁dvd
  2642. have hsub₂ : V₂.divisors ⊆ V.divisors := Nat.divisors_subset_of_dvd hV0.ne' hV₂dvd
  2643. have hcol₁ : ∀ x ∈ V₁.divisors, ∀ y ∈ V₁.divisors, x ≠ y →
  2644. (P.filter (fun p : ℕ => (p:ℤ) ∣ (x:ℤ) - (y:ℤ))).card ≤ 2 * k :=
  2645. fun x hx y hy hxy => hcolV x (hsub₁ hx) y (hsub₁ hy) hxy
  2646. have hcol₂ : ∀ x ∈ V₂.divisors, ∀ y ∈ V₂.divisors, x ≠ y →
  2647. (P.filter (fun p : ℕ => (p:ℤ) ∣ (x:ℤ) - (y:ℤ))).card ≤ 2 * k :=
  2648. fun x hx y hy hxy => hcolV x (hsub₂ hx) y (hsub₂ hy) hxy
  2649. -- the numerical hypothesis
  2650. have hnum := numeric_terms hNum hρ0 hρle
  2651. -- the selection
  2652. obtain ⟨I, hI, hSI⟩ := finite_selection (P := P) (fun p hp => (hPprop p hp).1)
  2653. (U := 4 * (Qp k : ℝ)) (by positivity)
  2654. (fun p hp => by
  2655. have h1 : p ≤ 4 * Qp k := (hPprop p hp).2.2.1
  2656. have h2 : ((p : ℕ) : ℝ) ≤ ((4 * Qp k : ℕ) : ℝ) := by exact_mod_cast h1
  2657. push_cast at h2
  2658. linarith)
  2659. hX₁ne hX₂ne hXne hX₁bound hX₂bound hXbound (R := 2 * k) (t := tp k) hRN ht1 htN
  2660. hcol₁ hcol₂ hcolV hnum
  2661. obtain ⟨hIP, hIcard⟩ := Finset.mem_powersetCard.mp hI
  2662. have hIprime : ∀ p ∈ I, p.Prime := fun p hp => (hPprop p (hIP hp)).1
  2663. set A := ∏ p ∈ I, p with hAdef
  2664. have hApos : 0 < A := Finset.prod_pos (fun p hp => (hIprime p hp).pos)
  2665. have hApf : A.primeFactors = I := Nat.primeFactors_prod hIprime
  2666. have hIne : I.Nonempty := Finset.card_pos.mp (by omega)
  2667. have hA1 : 1 < A := by
  2668. obtain ⟨p, hp⟩ := hIne
  2669. have h2 : 2 ≤ p := (hIprime p hp).two_le
  2670. have hdvd : p ∣ A := Finset.dvd_prod_of_mem _ hp
  2671. have := Nat.le_of_dvd hApos hdvd
  2672. omega
  2673. have hAsq : Squarefree A := squarefree_prod_primes hIprime
  2674. have hAodd : Odd A := by
  2675. rw [Nat.odd_iff, ← Nat.two_dvd_ne_zero]
  2676. intro hdvd
  2677. obtain ⟨p, hp, hpd⟩ := Prime.exists_mem_finset_dvd Nat.prime_two.prime hdvd
  2678. have hpe : 2 = p := (Nat.prime_dvd_prime_iff_eq Nat.prime_two (hIprime p hp)).mp hpd
  2679. have hQp : Qp k < p := (hPprop p (hIP hp)).2.1
  2680. omega
  2681. have hAcop : Nat.Coprime A V := by
  2682. refine Nat.Coprime.prod_left (fun p hp => ?_)
  2683. exact (Nat.Prime.coprime_iff_not_dvd (hIprime p hp)).mpr (hPprop p (hIP hp)).2.2.2.1
  2684. have hAstar : ¬ (pstar ∣ A) := by
  2685. intro hdvd
  2686. obtain ⟨p, hp, hpd⟩ := Prime.exists_mem_finset_dvd hpstarP.prime hdvd
  2687. have hpe : pstar = p := (Nat.prime_dvd_prime_iff_eq hpstarP (hIprime p hp)).mp hpd
  2688. exact (hPprop p (hIP hp)).2.2.2.2 hpe.symm
  2689. refine ⟨A, hA1, hAodd, hAsq, hAcop, hAstar, by rw [hApf, hIcard], ?_, ?_⟩
  2690. · intro p hp
  2691. rw [hApf] at hp
  2692. exact ⟨(hPprop p (hIP hp)).2.1, (hPprop p (hIP hp)).2.2.1⟩
  2693. · intro c hc
  2694. have hS : ∑ d ∈ A.divisors.filter (fun d => 1 < d),
  2695. (d:ℝ) ^ ((2:ℝ)/3) * (Mmom V₁.divisors d * Mmom V₂.divisors d
  2696. * Mmom (V₁ * V₂).divisors d) ^ ((1:ℝ)/3) < 1 := by
  2697. rw [hV12]
  2698. exact hSI
  2699. have hcov := coverage hV₁0 hV₂0 hcop hA1 hAodd hS c
  2700. rw [hV12] at hcov
  2701. obtain ⟨z, hz⟩ := Finset.card_pos.mp hcov
  2702. rw [Finset.mem_filter] at hz
  2703. obtain ⟨hzmem, hzeq⟩ := hz
  2704. simp only [Finset.mem_product] at hzmem
  2705. exact ⟨z.1, hzmem.1, z.2.1, hzmem.2.1, z.2.2.1, hzmem.2.2.1, z.2.2.2, hzmem.2.2.2, hzeq⟩
  2706. /-! ## Divisor representations
  2707. `Rep n H` says that every `m < n` is a sum of at most `H` distinct positive divisors
  2708. of `n`, together with the basic tools used to build integers with this property.
  2709. -/
  2710. /-- `Rep n H` means `n ≥ 1` and every natural number `m < n` is the sum of a set of
  2711. at most `H` distinct positive divisors of `n`. -/
  2712. def Rep (n H : ℕ) : Prop :=
  2713. 1 ≤ n ∧ ∀ m < n, ∃ F ⊆ n.divisors, F.card ≤ H ∧ ∑ d ∈ F, d = m
  2714. theorem Rep.mono {n H H' : ℕ} (h : Rep n H) (hH : H ≤ H') : Rep n H' :=
  2715. ⟨h.1, fun m hm => by
  2716. obtain ⟨F, hF, hcard, hsum⟩ := h.2 m hm
  2717. exact ⟨F, hF, hcard.trans hH, hsum⟩⟩
  2718. /-- Every `1 ≤ m ≤ n` is a sum of at most `T` distinct divisors of `n`, for real `T ≥ H`, `T ≥ 1`. -/
  2719. theorem Rep.all_le {n H : ℕ} (h : Rep n H) {T : ℝ} (hT : (H : ℝ) ≤ T) (hT1 : 1 ≤ T)
  2720. {m : ℕ} (hm1 : 1 ≤ m) (hmn : m ≤ n) :
  2721. ∃ F ⊆ n.divisors, (F.card : ℝ) ≤ T ∧ ∑ d ∈ F, d = m := by
  2722. rcases lt_or_eq_of_le hmn with hlt | heq
  2723. · obtain ⟨F, hF, hcard, hsum⟩ := h.2 m hlt
  2724. exact ⟨F, hF, le_trans (by exact_mod_cast hcard) hT, hsum⟩
  2725. · refine ⟨{n}, ?_, ?_, ?_⟩
  2726. · intro x hx
  2727. simp only [Finset.mem_singleton] at hx
  2728. subst hx
  2729. exact Nat.mem_divisors_self _ (by have := h.1; omega)
  2730. · simpa using hT1
  2731. · simpa using heq.symm
  2732. /-- Modular lifting. -/
  2733. theorem Rep.lift {A B H L : ℕ} (hA : 2 ≤ A) (hRep : Rep B H)
  2734. (hF : ∀ c < A, ∃ F ⊆ B.divisors, F.card ≤ L ∧ (∑ d ∈ F, d) ≤ B ∧
  2735. (∑ d ∈ F, d) % A = c % A ∧ ∀ d ∈ F, ¬ A ∣ d) :
  2736. Rep (A * B) (H + L) := by
  2737. have hB : 1 ≤ B := hRep.1
  2738. refine ⟨Nat.one_le_iff_ne_zero.mpr (by positivity), fun m hm => ?_⟩
  2739. by_cases hmB : m < B
  2740. · obtain ⟨F, hF, hcard, hsum⟩ := hRep.2 m hmB
  2741. refine ⟨F, ?_, by omega, hsum⟩
  2742. intro x hx
  2743. have := Nat.mem_divisors.mp (hF hx)
  2744. exact Nat.mem_divisors.mpr ⟨this.1.trans (Dvd.intro_left A rfl), by positivity⟩
  2745. · push_neg at hmB
  2746. obtain ⟨F, hF, hcardF, hsumB, hsumc, hnd⟩ := hF (m % A) (Nat.mod_lt _ (by omega))
  2747. set s := ∑ d ∈ F, d with hs
  2748. have hsm : s % A = m % A := by rw [hsumc, Nat.mod_mod_of_dvd]; exact dvd_rfl
  2749. have hsle : s ≤ m := le_trans hsumB hmB
  2750. have hdvd : A ∣ m - s := (Nat.modEq_iff_dvd' hsle).mp hsm
  2751. obtain ⟨u, hu⟩ := hdvd
  2752. have hub : u < B := by
  2753. by_contra hcon
  2754. push_neg at hcon
  2755. have : A * B ≤ A * u := Nat.mul_le_mul_left _ hcon
  2756. omega
  2757. obtain ⟨G, hG, hcardG, hsumG⟩ := hRep.2 u hub
  2758. have hinj : Set.InjOn (fun e => A * e) G := fun x _ y _ h => by
  2759. simpa [Nat.mul_left_cancel_iff (show 0 < A by omega)] using h
  2760. have hdisj : Disjoint F (G.image (fun e => A * e)) := by
  2761. rw [Finset.disjoint_right]
  2762. intro x hx hxF
  2763. obtain ⟨e, _, rfl⟩ := Finset.mem_image.mp hx
  2764. exact hnd _ hxF (Dvd.intro e rfl)
  2765. refine ⟨F ∪ G.image (fun e => A * e), ?_, ?_, ?_⟩
  2766. · intro x hx
  2767. rcases Finset.mem_union.mp hx with hx | hx
  2768. · have := Nat.mem_divisors.mp (hF hx)
  2769. exact Nat.mem_divisors.mpr ⟨this.1.trans (Dvd.intro_left A rfl), by positivity⟩
  2770. · obtain ⟨e, he, rfl⟩ := Finset.mem_image.mp hx
  2771. have := Nat.mem_divisors.mp (hG he)
  2772. exact Nat.mem_divisors.mpr ⟨Nat.mul_dvd_mul_left A this.1, by positivity⟩
  2773. · rw [Finset.card_union_of_disjoint hdisj, Finset.card_image_of_injOn hinj]
  2774. omega
  2775. · rw [Finset.sum_union hdisj, Finset.sum_image hinj, ← Finset.mul_sum, hsumG, ← hs]
  2776. omega
  2777. /-- Binary representations. -/
  2778. theorem rep_two_pow (E : ℕ) : Rep (2 ^ E) E := by
  2779. induction E with
  2780. | zero => exact ⟨le_refl 1, fun m hm => ⟨∅, by simp, by simp, by simpa using (by omega : (0:ℕ) = m)⟩⟩
  2781. | succ E ih =>
  2782. refine ⟨Nat.one_le_two_pow, fun m hm => ?_⟩
  2783. by_cases hlt : m < 2 ^ E
  2784. · obtain ⟨F, hF, hcard, hsum⟩ := ih.2 m hlt
  2785. refine ⟨F, ?_, by omega, hsum⟩
  2786. intro x hx
  2787. have := hF hx
  2788. rw [Nat.mem_divisors] at this ⊢
  2789. exact ⟨this.1.trans (pow_dvd_pow 2 (Nat.le_succ E)), by positivity⟩
  2790. · have hm' : m - 2 ^ E < 2 ^ E := by
  2791. have : (2:ℕ) ^ (E + 1) = 2 ^ E + 2 ^ E := by ring
  2792. omega
  2793. obtain ⟨F, hF, hcard, hsum⟩ := ih.2 _ hm'
  2794. have hnot : 2 ^ E ∉ F := by
  2795. intro hmem
  2796. have : 2 ^ E ≤ ∑ d ∈ F, d := Finset.single_le_sum (f := fun d => d) (by simp) hmem
  2797. omega
  2798. refine ⟨insert (2 ^ E) F, ?_, ?_, ?_⟩
  2799. · intro x hx
  2800. rcases Finset.mem_insert.mp hx with rfl | hx
  2801. · exact Nat.mem_divisors.mpr ⟨pow_dvd_pow 2 (Nat.le_succ E), by positivity⟩
  2802. · have := hF hx
  2803. rw [Nat.mem_divisors] at this ⊢
  2804. exact ⟨this.1.trans (pow_dvd_pow 2 (Nat.le_succ E)), by positivity⟩
  2805. · rw [Finset.card_insert_of_notMem hnot]; omega
  2806. · rw [Finset.sum_insert hnot, hsum]; omega
  2807. /-- Adjoining a prime. -/
  2808. theorem Rep.adjoin_prime {B H E p : ℕ} (hRep : Rep B H) (h2 : 2 ^ E ∣ B)
  2809. (hp : p.Prime) (hodd : Odd p) (hpE : p < 2 ^ E) : Rep (p * B) (H + E) := by
  2810. have hB : 1 ≤ B := hRep.1
  2811. have h2EB : 2 ^ E ≤ B := Nat.le_of_dvd (by omega) h2
  2812. refine Rep.lift (A := p) hp.two_le hRep ?_
  2813. intro c hc
  2814. obtain ⟨F, hF, hcard, hsum⟩ := (rep_two_pow E).2 c (lt_trans hc hpE)
  2815. have hsub : ∀ d ∈ F, d ∣ 2 ^ E := fun d hd => (Nat.mem_divisors.mp (hF hd)).1
  2816. refine ⟨F, ?_, hcard, ?_, ?_, ?_⟩
  2817. · intro x hx
  2818. exact Nat.mem_divisors.mpr ⟨(hsub x hx).trans h2, by omega⟩
  2819. · omega
  2820. · rw [hsum]
  2821. · intro d hd hdvd
  2822. have : p ∣ 2 ^ E := hdvd.trans (hsub d hd)
  2823. have hp2 : p ∣ 2 := hp.dvd_of_dvd_pow this
  2824. have : p = 2 := (Nat.prime_dvd_prime_iff_eq hp Nat.prime_two).mp hp2
  2825. rw [this] at hodd
  2826. exact (Nat.not_odd_iff_even.mpr (even_iff_two_dvd.mpr dvd_rfl)) hodd
  2827. /-- The initial representation bound. -/
  2828. theorem rep_seed {P : Finset ℕ} (hP : ∀ p ∈ P, p.Prime ∧ Odd p) {E : ℕ}
  2829. (hlt : ∀ p ∈ P, p < 2 ^ E) :
  2830. Rep (2 ^ E * ∏ p ∈ P, p) ((P.card + 1) * E) := by
  2831. classical
  2832. revert hP hlt
  2833. induction P using Finset.induction with
  2834. | empty => intro _ _; simpa using rep_two_pow E
  2835. | insert p P hp ih =>
  2836. intro hP hlt
  2837. have hstep := ih (fun q hq => hP q (Finset.mem_insert_of_mem hq))
  2838. (fun q hq => hlt q (Finset.mem_insert_of_mem hq))
  2839. have hpdata := hP p (Finset.mem_insert_self p P)
  2840. have hdvd : (2:ℕ) ^ E ∣ 2 ^ E * ∏ q ∈ P, q := Dvd.intro _ rfl
  2841. have hres := Rep.adjoin_prime hstep hdvd hpdata.1 hpdata.2
  2842. (hlt p (Finset.mem_insert_self p P))
  2843. rw [Finset.prod_insert hp, Finset.card_insert_of_notMem hp]
  2844. have heq : p * (2 ^ E * ∏ q ∈ P, q) = 2 ^ E * (p * ∏ q ∈ P, q) := by ring
  2845. rw [heq] at hres
  2846. exact hres.mono (le_of_eq (by ring))
  2847. /-- Distinct powers of two times odd numbers are distinct. -/
  2848. theorem two_pow_mul_odd_ne {i j a b : ℕ} (hij : i < j) (ha : Odd a) :
  2849. 2 ^ i * a ≠ 2 ^ j * b := by
  2850. intro h
  2851. have hj : 2 ^ j = 2 ^ i * 2 ^ (j - i) := by rw [← pow_add]; congr 1; omega
  2852. rw [hj, mul_assoc] at h
  2853. have h2 : a = 2 ^ (j - i) * b := Nat.eq_of_mul_eq_mul_left (by positivity) h
  2854. have : (2:ℕ) ∣ a := by
  2855. rw [h2]
  2856. exact Dvd.dvd.mul_right (dvd_pow_self 2 (by omega)) b
  2857. rw [Nat.odd_iff] at ha
  2858. omega
  2859. /-- Four residues give one extension. -/
  2860. theorem Rep.four_lift {V E H A : ℕ} (hV : 0 < V) (hVodd : Odd V) (hE : 4 ≤ E)
  2861. (hRep : Rep (2 ^ E * V) H) (hA : 1 < A) (hAodd : Odd A) (hcop : Nat.Coprime A V)
  2862. (hres : ∀ c < A, ∃ z₀ ∈ V.divisors, ∃ z₁ ∈ V.divisors, ∃ z₂ ∈ V.divisors, ∃ z₃ ∈ V.divisors,
  2863. (z₀ + 2 * z₁ + 4 * z₂ + 8 * z₃) % A = c % A) :
  2864. Rep (2 ^ E * V * A) (H + 4) := by
  2865. have key : Rep (A * (2 ^ E * V)) (H + 4) := by
  2866. refine Rep.lift hA hRep ?_
  2867. intro c hc
  2868. obtain ⟨z₀, hz₀, z₁, hz₁, z₂, hz₂, z₃, hz₃, hcong⟩ := hres c hc
  2869. have hdvd : ∀ z ∈ V.divisors, z ∣ V := fun z hz => (Nat.mem_divisors.mp hz).1
  2870. have hodd : ∀ z ∈ V.divisors, Odd z := fun z hz => hVodd.of_dvd_nat (hdvd z hz)
  2871. have hle : ∀ z ∈ V.divisors, z ≤ V := fun z hz => Nat.le_of_dvd hV (hdvd z hz)
  2872. have h01 : z₀ ≠ 2 * z₁ := by
  2873. simpa using two_pow_mul_odd_ne (i := 0) (j := 1) (b := z₁) (by norm_num) (hodd _ hz₀)
  2874. have h02 : z₀ ≠ 4 * z₂ := by
  2875. have := two_pow_mul_odd_ne (i := 0) (j := 2) (b := z₂) (by norm_num) (hodd _ hz₀)
  2876. norm_num at this; exact this
  2877. have h03 : z₀ ≠ 8 * z₃ := by
  2878. have := two_pow_mul_odd_ne (i := 0) (j := 3) (b := z₃) (by norm_num) (hodd _ hz₀)
  2879. norm_num at this; exact this
  2880. have h12 : 2 * z₁ ≠ 4 * z₂ := by
  2881. have := two_pow_mul_odd_ne (i := 1) (j := 2) (b := z₂) (by norm_num) (hodd _ hz₁)
  2882. norm_num at this; exact this
  2883. have h13 : 2 * z₁ ≠ 8 * z₃ := by
  2884. have := two_pow_mul_odd_ne (i := 1) (j := 3) (b := z₃) (by norm_num) (hodd _ hz₁)
  2885. norm_num at this; exact this
  2886. have h23 : 4 * z₂ ≠ 8 * z₃ := by
  2887. have := two_pow_mul_odd_ne (i := 2) (j := 3) (b := z₃) (by norm_num) (hodd _ hz₂)
  2888. norm_num at this; exact this
  2889. have hcard : ({z₀, 2 * z₁, 4 * z₂, 8 * z₃} : Finset ℕ).card = 4 := by
  2890. rw [Finset.card_insert_of_notMem (by simp [h01, h02, h03]),
  2891. Finset.card_insert_of_notMem (by simp [h12, h13]),
  2892. Finset.card_insert_of_notMem (by simp [h23])]
  2893. simp
  2894. have hsum : ∑ d ∈ ({z₀, 2 * z₁, 4 * z₂, 8 * z₃} : Finset ℕ), d
  2895. = z₀ + 2 * z₁ + 4 * z₂ + 8 * z₃ := by
  2896. rw [Finset.sum_insert (by simp [h01, h02, h03]),
  2897. Finset.sum_insert (by simp [h12, h13]),
  2898. Finset.sum_insert (by simp [h23])]
  2899. simp [add_assoc]
  2900. have h16 : (16 : ℕ) ≤ 2 ^ E := by
  2901. calc (16:ℕ) = 2 ^ 4 := by norm_num
  2902. _ ≤ 2 ^ E := Nat.pow_le_pow_right (by norm_num) hE
  2903. refine ⟨{z₀, 2 * z₁, 4 * z₂, 8 * z₃}, ?_, by omega, ?_, ?_, ?_⟩
  2904. · intro x hx
  2905. have hVpos : 0 < 2 ^ E * V := by positivity
  2906. simp only [Finset.mem_insert, Finset.mem_singleton] at hx
  2907. refine Nat.mem_divisors.mpr ⟨?_, by omega⟩
  2908. have hd2 : (2:ℕ) ∣ 2 ^ E := dvd_pow_self 2 (by omega)
  2909. have hd4 : (4:ℕ) ∣ 2 ^ E := by
  2910. rw [show (4:ℕ) = 2 ^ 2 by norm_num]; exact pow_dvd_pow 2 (by omega)
  2911. have hd8 : (8:ℕ) ∣ 2 ^ E := by
  2912. rw [show (8:ℕ) = 2 ^ 3 by norm_num]; exact pow_dvd_pow 2 (by omega)
  2913. rcases hx with rfl | rfl | rfl | rfl
  2914. · exact (hdvd _ hz₀).trans (Dvd.intro_left _ rfl)
  2915. · exact mul_dvd_mul hd2 (hdvd _ hz₁)
  2916. · exact mul_dvd_mul hd4 (hdvd _ hz₂)
  2917. · exact mul_dvd_mul hd8 (hdvd _ hz₃)
  2918. · rw [hsum]
  2919. have := hle _ hz₀; have := hle _ hz₁; have := hle _ hz₂; have := hle _ hz₃
  2920. have : 16 * V ≤ 2 ^ E * V := Nat.mul_le_mul_right V h16
  2921. omega
  2922. · rw [hsum]; exact hcong
  2923. · intro d hd hAdvd
  2924. simp only [Finset.mem_insert, Finset.mem_singleton] at hd
  2925. have hA2 : Nat.Coprime A 2 := Nat.coprime_two_right.mpr hAodd
  2926. have hcopd : Nat.Coprime A d := by
  2927. rcases hd with rfl | rfl | rfl | rfl
  2928. · exact hcop.coprime_dvd_right (hdvd _ hz₀)
  2929. · exact Nat.Coprime.mul_right hA2 (hcop.coprime_dvd_right (hdvd _ hz₁))
  2930. · exact Nat.Coprime.mul_right (by simpa using hA2.pow_right 2)
  2931. (hcop.coprime_dvd_right (hdvd _ hz₂))
  2932. · exact Nat.Coprime.mul_right (by
  2933. have : (8:ℕ) = 2 ^ 3 := by norm_num
  2934. rw [this]; exact hA2.pow_right 3) (hcop.coprime_dvd_right (hdvd _ hz₃))
  2935. have := Nat.Coprime.eq_one_of_dvd hcopd hAdvd
  2936. omega
  2937. have heq : A * (2 ^ E * V) = 2 ^ E * V * A := by ring
  2938. rwa [heq] at key
  2939. /-! ## The recursive construction
  2940. Starting from a seed `2^E V₀` we repeatedly multiply by a modulus supplied by
  2941. `exists_K4`, obtaining integers `B_j` with `Rep (B_j) (H₀ + 4j)` whose sizes grow at
  2942. most quadratically. The first `B_j` beyond a given `x` then satisfies the uniform
  2943. representation bound.
  2944. -/
  2945. /-! ### Products of distinct positive integers -/
  2946. /-- A product of `k` distinct positive integers is at least `k!`. -/
  2947. theorem factorial_le_prod {S : Finset ℕ} (hpos : ∀ p ∈ S, 1 ≤ p) :
  2948. Nat.factorial S.card ≤ ∏ p ∈ S, p := by
  2949. classical
  2950. induction hn : S.card generalizing S with
  2951. | zero =>
  2952. rw [Finset.card_eq_zero] at hn
  2953. subst hn
  2954. simp
  2955. | succ n ih =>
  2956. have hne : S.Nonempty := Finset.card_pos.mp (by omega)
  2957. set m := S.max' hne with hm
  2958. have hmS : m ∈ S := S.max'_mem hne
  2959. have hsub : S ⊆ Finset.Icc 1 m := by
  2960. intro x hx
  2961. exact Finset.mem_Icc.mpr ⟨hpos x hx, S.le_max' x hx⟩
  2962. have hcard : S.card ≤ m := by
  2963. have := Finset.card_le_card hsub
  2964. simpa [Nat.card_Icc] using this
  2965. have herase : (S.erase m).card = n := by
  2966. rw [Finset.card_erase_of_mem hmS, hn]
  2967. omega
  2968. have hih : Nat.factorial n ≤ ∏ p ∈ S.erase m, p :=
  2969. ih (fun p hp => hpos p (Finset.mem_of_mem_erase hp)) herase
  2970. have hprod : ∏ p ∈ S, p = m * ∏ p ∈ S.erase m, p :=
  2971. (Finset.mul_prod_erase S _ hmS).symm
  2972. rw [hprod, Nat.factorial_succ]
  2973. exact Nat.mul_le_mul (by omega) hih
  2974. /-! ### The selection step -/
  2975. /-- The property of `A` produced by `exists_K4` for the integer `V`. -/
  2976. def SelSpec (pstar V A : ℕ) : Prop :=
  2977. 1 < A ∧ Odd A ∧ Squarefree A ∧ Nat.Coprime A V ∧ ¬ (pstar ∣ A) ∧
  2978. A.primeFactors.card = tp V.primeFactors.card ∧
  2979. (∀ p ∈ A.primeFactors, Qp V.primeFactors.card < p ∧ p ≤ 4 * Qp V.primeFactors.card) ∧
  2980. ∀ c : ℕ, c < A → ∃ z₀ ∈ V.divisors, ∃ z₁ ∈ V.divisors, ∃ z₂ ∈ V.divisors,
  2981. ∃ z₃ ∈ V.divisors, (z₀ + 2 * z₁ + 4 * z₂ + 8 * z₃) % A = c % A
  2982. open Classical in
  2983. /-- The chosen modulus at one step. -/
  2984. noncomputable def stepA (pstar V : ℕ) : ℕ :=
  2985. if h : ∃ A, SelSpec pstar V A then Classical.choose h else 1
  2986. theorem stepA_spec {pstar V : ℕ} (h : ∃ A, SelSpec pstar V A) :
  2987. SelSpec pstar V (stepA pstar V) := by
  2988. classical
  2989. rw [stepA, dif_pos h]
  2990. exact Classical.choose_spec h
  2991. /-- The sequence of squarefree odd integers. -/
  2992. noncomputable def Vseq (pstar V₀ : ℕ) : ℕ → ℕ
  2993. | 0 => V₀
  2994. | (j + 1) => Vseq pstar V₀ j * stepA pstar (Vseq pstar V₀ j)
  2995. /-- **The seed.** -/
  2996. theorem exists_seed : ∃ k₀ E H₀ : ℕ, 4 ≤ E ∧ 1 ≤ H₀ ∧ 4 * Qp k₀ < 2 ^ E ∧
  2997. (∀ pstar : ℕ, pstar.Prime → Odd pstar →
  2998. ∃ V₀ : ℕ, 0 < V₀ ∧ Odd V₀ ∧ Squarefree V₀ ∧ V₀.primeFactors.card = k₀ ∧
  2999. ¬ (pstar ∣ V₀) ∧ (∀ p ∈ V₀.primeFactors, Qp k₀ < p ∧ p ≤ 4 * Qp k₀) ∧
  3000. Rep (2 ^ E * V₀) H₀ ∧ 2 ^ E * V₀ ≤ 2 ^ E * (4 * Qp k₀) ^ k₀) ∧
  3001. (∀ k : ℕ, k₀ ≤ k → ∀ pstar : ℕ, pstar.Prime → Odd pstar →
  3002. ∀ V : ℕ, 0 < V → Odd V → Squarefree V → V.primeFactors.card = k →
  3003. (∀ p ∈ V.primeFactors, p ≤ 4 * Qp k) → ∃ A, SelSpec pstar V A) ∧
  3004. (∀ k : ℕ, k₀ ≤ k → K1P k) := by
  3005. classical
  3006. obtain ⟨K₄, hK₄3, hK₄⟩ := exists_K4
  3007. obtain ⟨K₂, hK₂3, hK₂⟩ := exists_K2
  3008. obtain ⟨K₁, hK₁3, hK₁⟩ := exists_K1
  3009. set k₀ := max (max K₄ K₂) K₁ with hk₀
  3010. have hk₀K₄ : K₄ ≤ k₀ := le_trans (le_max_left _ _) (le_max_left _ _)
  3011. have hk₀K₂ : K₂ ≤ k₀ := le_trans (le_max_right _ _) (le_max_left _ _)
  3012. have hk₀K₁ : K₁ ≤ k₀ := le_max_right _ _
  3013. have hk₀3 : 3 ≤ k₀ := le_trans hK₄3 hk₀K₄
  3014. set E := max 4 (4 * Qp k₀) with hE
  3015. have hE4 : 4 ≤ E := le_max_left _ _
  3016. have hE2 : 4 * Qp k₀ < 2 ^ E := by
  3017. calc 4 * Qp k₀ < 2 ^ (4 * Qp k₀) := Nat.lt_two_pow_self
  3018. _ ≤ 2 ^ E := Nat.pow_le_pow_right (by norm_num) (le_max_right _ _)
  3019. -- there are at least `k₀ + 1` primes in the interval
  3020. have hmany : k₀ + 1 ≤ ((Finset.Ioc (Qp k₀) (4 * Qp k₀)).filter Nat.Prime).card := by
  3021. have h1 := hK₂ k₀ hk₀K₂
  3022. have hk₀R : (3:ℝ) ≤ (k₀:ℝ) := by exact_mod_cast hk₀3
  3023. have h2 : ((k₀:ℝ) + 1) ≤ (k₀:ℝ) ^ 6 / 8 := by
  3024. have h5 : (3:ℝ) ^ 5 ≤ (k₀:ℝ) ^ 5 := pow_le_pow_left₀ (by norm_num) hk₀R 5
  3025. have hrw : (k₀:ℝ) ^ 6 = (k₀:ℝ) ^ 5 * (k₀:ℝ) := by ring
  3026. rw [hrw]
  3027. nlinarith
  3028. have h3 : ((k₀:ℝ) + 1) ≤ (((Finset.Ioc (Qp k₀) (4 * Qp k₀)).filter Nat.Prime).card : ℝ) :=
  3029. le_trans h2 h1
  3030. exact_mod_cast h3
  3031. have hH₀ : 1 ≤ (k₀ + 1) * E := Nat.one_le_iff_ne_zero.mpr
  3032. (Nat.mul_ne_zero (by omega) (by omega))
  3033. refine ⟨k₀, E, (k₀ + 1) * E, hE4, hH₀, hE2, ?_, ?_, fun k hk => hK₁ k (le_trans hk₀K₁ hk)⟩
  3034. · -- the seed itself
  3035. intro pstar hpstar hpodd
  3036. set S := ((Finset.Ioc (Qp k₀) (4 * Qp k₀)).filter Nat.Prime).erase pstar with hS
  3037. have hScard : k₀ ≤ S.card := by
  3038. have hpc := Finset.pred_card_le_card_erase
  3039. (s := (Finset.Ioc (Qp k₀) (4 * Qp k₀)).filter Nat.Prime) (a := pstar)
  3040. rw [hS]
  3041. omega
  3042. obtain ⟨P, hPS, hPcard⟩ := Finset.exists_subset_card_eq hScard
  3043. have hPprime : ∀ p ∈ P, p.Prime := by
  3044. intro p hp
  3045. have := hPS hp
  3046. rw [hS, Finset.mem_erase, Finset.mem_filter] at this
  3047. exact this.2.2
  3048. have hPmem : ∀ p ∈ P, Qp k₀ < p ∧ p ≤ 4 * Qp k₀ := by
  3049. intro p hp
  3050. have := hPS hp
  3051. rw [hS, Finset.mem_erase, Finset.mem_filter, Finset.mem_Ioc] at this
  3052. exact this.2.1
  3053. have hPne : ∀ p ∈ P, p ≠ pstar := by
  3054. intro p hp
  3055. have := hPS hp
  3056. rw [hS, Finset.mem_erase] at this
  3057. exact this.1
  3058. have hQ3 : 3 ≤ Qp k₀ := by
  3059. by_contra hcon
  3060. push_neg at hcon
  3061. have hQ : (Qp k₀ : ℝ) < 3 := by exact_mod_cast hcon
  3062. have : False := by
  3063. have hk₀R : (3:ℝ) ≤ (k₀:ℝ) := by exact_mod_cast hk₀3
  3064. have h1 := hK₂ k₀ hk₀K₂
  3065. have hcard : (((Finset.Ioc (Qp k₀) (4 * Qp k₀)).filter Nat.Prime).card : ℝ)
  3066. ≤ ((4 * Qp k₀ : ℕ) : ℝ) := by
  3067. have hsub : ((Finset.Ioc (Qp k₀) (4 * Qp k₀)).filter Nat.Prime).card ≤ 4 * Qp k₀ := by
  3068. calc ((Finset.Ioc (Qp k₀) (4 * Qp k₀)).filter Nat.Prime).card
  3069. ≤ (Finset.Ioc (Qp k₀) (4 * Qp k₀)).card := Finset.card_filter_le _ _
  3070. _ ≤ 4 * Qp k₀ := by rw [Nat.card_Ioc]; omega
  3071. exact_mod_cast hsub
  3072. have h5 : (3:ℝ) ^ 6 ≤ (k₀:ℝ) ^ 6 := pow_le_pow_left₀ (by norm_num) hk₀R 6
  3073. push_cast at hcard
  3074. nlinarith
  3075. exact this
  3076. have hodd : ∀ p ∈ P, Odd p := by
  3077. intro p hp
  3078. have hp2 : 2 < p := lt_of_le_of_lt (by omega) (hPmem p hp).1
  3079. exact (hPprime p hp).odd_of_ne_two (by omega)
  3080. refine ⟨∏ p ∈ P, p, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_⟩
  3081. · exact Finset.prod_pos (fun p hp => (hPprime p hp).pos)
  3082. · rw [Nat.odd_iff, Finset.prod_nat_mod]
  3083. have : ∀ p ∈ P, p % 2 = 1 := fun p hp => Nat.odd_iff.mp (hodd p hp)
  3084. rw [Finset.prod_congr rfl this]
  3085. simp
  3086. · exact squarefree_prod_primes hPprime
  3087. · rw [Nat.primeFactors_prod hPprime]; exact hPcard
  3088. · intro hdvd
  3089. obtain ⟨p, hp, hpa⟩ := Prime.exists_mem_finset_dvd hpstar.prime hdvd
  3090. have := (Nat.prime_dvd_prime_iff_eq hpstar (hPprime p hp)).mp hpa
  3091. exact hPne p hp this.symm
  3092. · intro p hp
  3093. rw [Nat.primeFactors_prod hPprime] at hp
  3094. exact hPmem p hp
  3095. · refine rep_seed (fun p hp => ⟨hPprime p hp, hodd p hp⟩) ?_ |>.mono ?_
  3096. · intro p hp
  3097. exact lt_of_le_of_lt (hPmem p hp).2 hE2
  3098. · rw [hPcard]
  3099. · have hprodle : ∏ p ∈ P, p ≤ (4 * Qp k₀) ^ k₀ := by
  3100. calc ∏ p ∈ P, p ≤ ∏ _p ∈ P, (4 * Qp k₀) :=
  3101. Finset.prod_le_prod' (fun p hp => (hPmem p hp).2)
  3102. _ = (4 * Qp k₀) ^ k₀ := by rw [Finset.prod_const, hPcard]
  3103. exact Nat.mul_le_mul_left _ hprodle
  3104. · -- the inductive step is available
  3105. intro k hk pstar hpstar hpodd V hV hVodd hVsq hVcard hVprimes
  3106. obtain ⟨A, hA⟩ := hK₄ k (le_trans hk₀K₄ hk) pstar hpstar hpodd V hV hVodd hVsq hVcard hVprimes
  3107. refine ⟨A, ?_⟩
  3108. rw [SelSpec, hVcard]
  3109. exact hA
  3110. /-! ### Size bounds for squarefree integers -/
  3111. /-- A squarefree integer with `k` prime factors satisfies `log V ≥ k log k / 12`. -/
  3112. theorem log_self_lower {V : ℕ} (hsq : Squarefree V) {k : ℕ}
  3113. (hcard : V.primeFactors.card = k) (hu : 3 ≤ Real.log k) :
  3114. (k : ℝ) * Real.log k / 12 ≤ Real.log V := by
  3115. have hk3 : 3 ≤ k := by
  3116. by_contra hcon
  3117. push_neg at hcon
  3118. have hkle : (k:ℝ) ≤ 2 := by
  3119. have : k ≤ 2 := by omega
  3120. exact_mod_cast this
  3121. have hlogle : Real.log k ≤ Real.log 2 := by
  3122. rcases Nat.eq_zero_or_pos k with rfl | hkpos
  3123. · simp [Real.log_nonneg]
  3124. · exact Real.log_le_log (by positivity) hkle
  3125. have h2 : Real.log 2 < 1 := by
  3126. have h := lam_lt_one
  3127. unfold lam at h
  3128. exact h
  3129. linarith
  3130. have hkR : (3:ℝ) ≤ (k:ℝ) := by exact_mod_cast hk3
  3131. -- V is at least k!
  3132. have hfac : Nat.factorial k ≤ V := by
  3133. have h := factorial_le_prod (S := V.primeFactors)
  3134. (fun p hp => (Nat.prime_of_mem_primeFactors hp).one_lt.le)
  3135. rw [hcard, Nat.prod_primeFactors_of_squarefree hsq] at h
  3136. exact h
  3137. -- k! is at least (k/2)^(k/2)
  3138. set h := k / 2 with hh
  3139. have hhk : h ≤ k := by omega
  3140. have hpow : h ^ (k - h) ≤ Nat.factorial k := by
  3141. have := Nat.factorial_mul_pow_sub_le_factorial (n := h) (m := k) hhk
  3142. calc h ^ (k - h) ≤ Nat.factorial h * h ^ (k - h) :=
  3143. Nat.le_mul_of_pos_left _ (Nat.factorial_pos h)
  3144. _ ≤ Nat.factorial k := this
  3145. have hple : h ^ h ≤ h ^ (k - h) := Nat.pow_le_pow_right (by omega) (by omega)
  3146. have hVge : (h : ℝ) ^ h ≤ (V : ℝ) := by
  3147. have : h ^ h ≤ V := le_trans hple (le_trans hpow hfac)
  3148. exact_mod_cast this
  3149. have hh0 : (1:ℝ) ≤ (h:ℝ) := by
  3150. have : 1 ≤ h := by omega
  3151. exact_mod_cast this
  3152. have hlogV : (h:ℝ) * Real.log h ≤ Real.log V := by
  3153. have h1 : Real.log ((h:ℝ) ^ h) ≤ Real.log V :=
  3154. Real.log_le_log (by positivity) hVge
  3155. rwa [Real.log_pow] at h1
  3156. -- and (h : ℝ) ≥ k/3, log h ≥ log k - log 3
  3157. have hhge : (k:ℝ) / 3 ≤ (h:ℝ) := by
  3158. have h3 : k ≤ 3 * h := by omega
  3159. have h4 : (k:ℝ) ≤ 3 * (h:ℝ) := by exact_mod_cast h3
  3160. linarith
  3161. have hlog3 : Real.log 3 ≤ (12:ℝ)/10 := by
  3162. have he : (3:ℝ) ≤ Real.exp (12/10) := by
  3163. have h1 : Real.exp (12/10) = Real.exp 1 * Real.exp (2/10) := by
  3164. rw [← Real.exp_add]; norm_num
  3165. have h2 : (1:ℝ) + 2/10 ≤ Real.exp (2/10) := by
  3166. have := Real.add_one_le_exp (2/10 : ℝ)
  3167. linarith
  3168. have h3 : (2.7182818283:ℝ) < Real.exp 1 := Real.exp_one_gt_d9
  3169. nlinarith [Real.exp_pos (2/10 : ℝ)]
  3170. have := Real.log_le_log (by norm_num : (0:ℝ) < 3) he
  3171. rwa [Real.log_exp] at this
  3172. have hlogh : Real.log k - Real.log 3 ≤ Real.log h := by
  3173. have h1 : Real.log ((k:ℝ)/3) ≤ Real.log h :=
  3174. Real.log_le_log (by positivity) hhge
  3175. rwa [Real.log_div (by positivity) (by norm_num)] at h1
  3176. have hlogh0 : 0 ≤ Real.log h := by
  3177. have : Real.log 1 ≤ Real.log h := Real.log_le_log (by norm_num) hh0
  3178. simpa using this
  3179. have hfinal : (k:ℝ) / 3 * (Real.log k - 12/10) ≤ (h:ℝ) * Real.log h := by
  3180. have h1 : (k:ℝ)/3 * (Real.log k - 12/10) ≤ (k:ℝ)/3 * Real.log h := by
  3181. have : Real.log k - 12/10 ≤ Real.log h := by linarith
  3182. exact mul_le_mul_of_nonneg_left this (by positivity)
  3183. have h2 : (k:ℝ)/3 * Real.log h ≤ (h:ℝ) * Real.log h :=
  3184. mul_le_mul_of_nonneg_right hhge hlogh0
  3185. linarith
  3186. have hlast : (k:ℝ) * Real.log k / 12 ≤ (k:ℝ)/3 * (Real.log k - 12/10) := by
  3187. have hpos : (0:ℝ) ≤ (k:ℝ) := by linarith
  3188. have h1 : (0:ℝ) ≤ 3 * Real.log k - 48/10 := by linarith
  3189. nlinarith [mul_nonneg hpos h1]
  3190. linarith
  3191. /-- The invariant maintained along the recursion. -/
  3192. def Inv (k₀ pstar V : ℕ) : Prop :=
  3193. 0 < V ∧ Odd V ∧ Squarefree V ∧ ¬ pstar ∣ V ∧ k₀ ≤ V.primeFactors.card ∧
  3194. ∀ p ∈ V.primeFactors, p ≤ 4 * Qp V.primeFactors.card
  3195. /-- One step of the recursion preserves the invariant and increases the cost by `4`. -/
  3196. theorem inv_step {k₀ E H : ℕ} (hE4 : 4 ≤ E) (hk₀3 : 3 ≤ k₀)
  3197. (hstep : ∀ k : ℕ, k₀ ≤ k → ∀ pstar : ℕ, pstar.Prime → Odd pstar →
  3198. ∀ V : ℕ, 0 < V → Odd V → Squarefree V → V.primeFactors.card = k →
  3199. (∀ p ∈ V.primeFactors, p ≤ 4 * Qp k) → ∃ A, SelSpec pstar V A)
  3200. {pstar V : ℕ} (hp : pstar.Prime) (hpodd : Odd pstar) (hInv : Inv k₀ pstar V)
  3201. (hRep : Rep (2 ^ E * V) H) :
  3202. Inv k₀ pstar (V * stepA pstar V) ∧ Rep (2 ^ E * (V * stepA pstar V)) (H + 4) ∧
  3203. (V * stepA pstar V).primeFactors.card
  3204. = V.primeFactors.card + tp V.primeFactors.card ∧
  3205. V * stepA pstar V ≤ V * (4 * Qp V.primeFactors.card) ^ tp V.primeFactors.card ∧
  3206. V < V * stepA pstar V := by
  3207. obtain ⟨hV0, hVodd, hVsq, hVnd, hVcard, hVpf⟩ := hInv
  3208. set k := V.primeFactors.card with hk
  3209. have hkk₀ : k₀ ≤ k := hVcard
  3210. have hk3 : 3 ≤ k := le_trans hk₀3 hkk₀
  3211. have hex := hstep k hkk₀ pstar hp hpodd V hV0 hVodd hVsq rfl hVpf
  3212. obtain ⟨hA1, hAodd, hAsq, hAcop, hAnd, hAcard, hAprimes, hAres⟩ := stepA_spec hex
  3213. set A := stepA pstar V with hA
  3214. have hA0 : 0 < A := by omega
  3215. have hVA0 : 0 < V * A := by positivity
  3216. have hdisj : Disjoint V.primeFactors A.primeFactors :=
  3217. (Nat.Coprime.disjoint_primeFactors (Nat.Coprime.symm hAcop))
  3218. have hpfmul : (V * A).primeFactors = V.primeFactors ∪ A.primeFactors :=
  3219. Nat.primeFactors_mul (by omega) (by omega)
  3220. have hcardmul : (V * A).primeFactors.card = k + tp k := by
  3221. rw [hpfmul, Finset.card_union_of_disjoint hdisj, hAcard, ← hk]
  3222. have hQmono : Qp k ≤ Qp (k + tp k) := Qp_mono hk3 (by omega)
  3223. refine ⟨⟨hVA0, hVodd.mul hAodd, ?_, ?_, ?_, ?_⟩, ?_, hcardmul, ?_, ?_⟩
  3224. · exact (Nat.squarefree_mul_iff).mpr ⟨Nat.Coprime.symm hAcop, hVsq, hAsq⟩
  3225. · intro hdvd
  3226. rcases (Nat.Prime.dvd_mul hp).mp hdvd with h | h
  3227. · exact hVnd h
  3228. · exact hAnd h
  3229. · rw [hcardmul]; omega
  3230. · intro q hq
  3231. rw [hcardmul]
  3232. rw [hpfmul, Finset.mem_union] at hq
  3233. rcases hq with hq | hq
  3234. · exact le_trans (hVpf q hq) (Nat.mul_le_mul le_rfl hQmono)
  3235. · exact le_trans (hAprimes q hq).2 (Nat.mul_le_mul le_rfl hQmono)
  3236. · have h := Rep.four_lift hV0 hVodd hE4 hRep hA1 hAodd hAcop hAres
  3237. have heq : 2 ^ E * V * A = 2 ^ E * (V * A) := by ring
  3238. rwa [heq] at h
  3239. · have hAle : A ≤ (4 * Qp k) ^ tp k := by
  3240. have h := self_le_pow_of_primeFactors_le (V := A) (M := 4 * Qp k) hAsq
  3241. (fun q hq => (hAprimes q hq).2)
  3242. rwa [hAcard] at h
  3243. exact Nat.mul_le_mul_left V hAle
  3244. · exact (Nat.lt_mul_iff_one_lt_right hV0).mpr hA1
  3245. theorem Vseq_succ (pstar V₀ j : ℕ) :
  3246. Vseq pstar V₀ (j + 1) = Vseq pstar V₀ j * stepA pstar (Vseq pstar V₀ j) := rfl
  3247. /-- The properties of the recursive sequence `Vseq`. -/
  3248. theorem Vseq_props {k₀ E H₀ : ℕ} (hE4 : 4 ≤ E) (hk₀3 : 3 ≤ k₀)
  3249. (hstep : ∀ k : ℕ, k₀ ≤ k → ∀ pstar : ℕ, pstar.Prime → Odd pstar →
  3250. ∀ V : ℕ, 0 < V → Odd V → Squarefree V → V.primeFactors.card = k →
  3251. (∀ p ∈ V.primeFactors, p ≤ 4 * Qp k) → ∃ A, SelSpec pstar V A)
  3252. {pstar V₀ : ℕ} (hp : pstar.Prime) (hpodd : Odd pstar) (hInv₀ : Inv k₀ pstar V₀)
  3253. (hrep₀ : Rep (2 ^ E * V₀) H₀) :
  3254. ∀ j : ℕ, Inv k₀ pstar (Vseq pstar V₀ j) ∧ Rep (2 ^ E * Vseq pstar V₀ j) (H₀ + 4 * j) ∧
  3255. (Vseq pstar V₀ (j + 1)).primeFactors.card
  3256. = (Vseq pstar V₀ j).primeFactors.card + tp (Vseq pstar V₀ j).primeFactors.card ∧
  3257. Vseq pstar V₀ (j + 1) ≤ Vseq pstar V₀ j *
  3258. (4 * Qp (Vseq pstar V₀ j).primeFactors.card) ^ tp (Vseq pstar V₀ j).primeFactors.card ∧
  3259. Vseq pstar V₀ j < Vseq pstar V₀ (j + 1) := by
  3260. intro j
  3261. induction j with
  3262. | zero =>
  3263. obtain ⟨h1, h2, h3, h4, h5⟩ := inv_step hE4 hk₀3 hstep hp hpodd hInv₀ (H := H₀) hrep₀
  3264. refine ⟨hInv₀, by simpa [Vseq] using hrep₀, ?_, ?_, ?_⟩
  3265. · rw [Vseq_succ]; simpa [Vseq] using h3
  3266. · rw [Vseq_succ]; simpa [Vseq] using h4
  3267. · rw [Vseq_succ]; simpa [Vseq] using h5
  3268. | succ j ih =>
  3269. obtain ⟨hInv, hRep, -, -, -⟩ := ih
  3270. obtain ⟨h1, h2, -, -, -⟩ := inv_step hE4 hk₀3 hstep hp hpodd hInv hRep
  3271. have hs : Vseq pstar V₀ (j + 1) = Vseq pstar V₀ j * stepA pstar (Vseq pstar V₀ j) :=
  3272. Vseq_succ _ _ _
  3273. have hInv' : Inv k₀ pstar (Vseq pstar V₀ (j + 1)) := by rw [hs]; exact h1
  3274. have hRep' : Rep (2 ^ E * Vseq pstar V₀ (j + 1)) (H₀ + 4 * (j + 1)) := by
  3275. have he : H₀ + 4 * (j + 1) = H₀ + 4 * j + 4 := by ring
  3276. rw [he, hs]
  3277. exact h2
  3278. obtain ⟨-, -, g3, g4, g5⟩ := inv_step hE4 hk₀3 hstep hp hpodd hInv' hRep'
  3279. refine ⟨hInv', hRep', ?_, ?_, ?_⟩
  3280. · rw [Vseq_succ pstar V₀ (j + 1)]; exact g3
  3281. · rw [Vseq_succ pstar V₀ (j + 1)]; exact g4
  3282. · rw [Vseq_succ pstar V₀ (j + 1)]; exact g5
  3283. /-- The modulus adjoined at one step is at most `2 ^ k`. -/
  3284. theorem pow_step_le_two_pow {k : ℕ} (hk3 : 3 ≤ k) (hk : K1P k) :
  3285. (4 * Qp k) ^ tp k ≤ 2 ^ k := by
  3286. obtain ⟨-, -, -, -, -, -, -, hvii, -, -, -, -, -, -⟩ := hk
  3287. have hlogk : 1 ≤ Real.log k := one_le_log_of_three_le hk3
  3288. have hloglog : 0 ≤ Real.log (Real.log k) := Real.log_nonneg hlogk
  3289. have hQ1 : 1 ≤ Qp k := Qp_pos k
  3290. have hQ1R : (1:ℝ) ≤ (Qp k : ℝ) := by exact_mod_cast hQ1
  3291. have hbase : (1:ℝ) ≤ 4 * (Qp k : ℝ) := by linarith
  3292. have hlogbase0 : 0 ≤ Real.log (4 * (Qp k : ℝ)) := Real.log_nonneg hbase
  3293. have hk0 : (0:ℝ) ≤ (k:ℝ) := by positivity
  3294. have h1 : (tp k : ℝ) * Real.log (4 * (Qp k : ℝ))
  3295. ≤ (ap k * k) * (7 * Real.log k + 3 * Real.log (Real.log k)) :=
  3296. mul_le_mul (tp_le_ap_mul hk3) hvii hlogbase0 (mul_nonneg (ap_pos hk3).le hk0)
  3297. have h2 : (ap k * k) * (7 * Real.log k + 3 * Real.log (Real.log k)) = (k:ℝ) * lam := by
  3298. rw [mul_comm (ap k) ((k:ℝ)), mul_assoc, ap_mul_den hk3]
  3299. rw [h2] at h1
  3300. have h3 : (tp k : ℝ) * Real.log (4 * (Qp k : ℝ)) ≤ (k:ℝ) * Real.log 2 := by
  3301. simpa [lam] using h1
  3302. have hL : Real.log ((4 * (Qp k : ℝ)) ^ tp k) ≤ Real.log ((2:ℝ) ^ k) := by
  3303. rw [Real.log_pow, Real.log_pow]
  3304. exact_mod_cast h3
  3305. have hp1 : (0:ℝ) < (4 * (Qp k : ℝ)) ^ tp k := by positivity
  3306. have hp2 : (0:ℝ) < (2:ℝ) ^ k := by positivity
  3307. have hfin : (4 * (Qp k : ℝ)) ^ tp k ≤ (2:ℝ) ^ k := (Real.log_le_log_iff hp1 hp2).mp hL
  3308. exact_mod_cast hfin
  3309. /-- **A uniform representation bound**, the key analytic input for the three main
  3310. theorems: for every large real `x` and every odd prime `p` there is an integer
  3311. `B ∈ [x, x²)` not divisible by `p`, exactly divisible by `2 ^ E`, such that every
  3312. `m < B` is a sum of at most `H` distinct divisors of `B`, with `H + 1 ≤ c₀ (log log x)²`. -/
  3313. theorem uniform_rep :
  3314. ∃ (E : ℕ) (x₀ : ℝ), 4 ≤ E ∧ Real.exp (Real.exp 1) < x₀ ∧
  3315. ∀ x : ℝ, x₀ ≤ x → ∀ p : ℕ, p.Prime → Odd p →
  3316. ∃ B H : ℕ, x ≤ (B : ℝ) ∧ (B : ℝ) < x ^ 2 ∧ 2 ^ E ∣ B ∧ ¬ (2 ^ (E + 1) ∣ B) ∧
  3317. ¬ (p ∣ B) ∧ Rep B H ∧ 1 ≤ H ∧
  3318. (H : ℝ) + 1 ≤ c0 * (Real.log (Real.log x)) ^ 2 := by
  3319. classical
  3320. obtain ⟨k₀, E, H₀, hE4, hH₀1, hE2, hseed, hstep, hK1⟩ := exists_seed
  3321. have h2Epos : 0 < 2 ^ E := Nat.two_pow_pos E
  3322. have hk₀3 : 3 ≤ k₀ := by
  3323. by_contra hcon
  3324. push_neg at hcon
  3325. have h := (hK1 k₀ le_rfl).1
  3326. have hkle : (k₀:ℝ) ≤ 2 := by
  3327. have : k₀ ≤ 2 := by omega
  3328. exact_mod_cast this
  3329. have hlogle : Real.log k₀ ≤ Real.log 2 := by
  3330. rcases Nat.eq_zero_or_pos k₀ with rfl | hpos
  3331. · simp [Real.log_nonneg]
  3332. · exact Real.log_le_log (by positivity) hkle
  3333. have h2 : Real.log 2 < 1 := by
  3334. have := lam_lt_one
  3335. unfold lam at this
  3336. exact this
  3337. linarith
  3338. obtain ⟨U, hU1, hUabs⟩ := absorb c0 ((H₀:ℝ) + 8) (Real.log 24) c0_pos
  3339. (Real.log_nonneg (by norm_num))
  3340. set k₁ : ℕ := max k₀ ⌈Real.exp U⌉₊ with hk₁def
  3341. set X1 : ℕ := 2 ^ E * (4 * Qp k₁) ^ k₁ with hX1def
  3342. have hX1nn : (0:ℝ) ≤ (X1:ℝ) := by positivity
  3343. refine ⟨E, Real.exp (Real.exp 1) + (X1:ℝ) + 1, hE4, by linarith, ?_⟩
  3344. intro x hx p hp hpodd
  3345. have hexppos : (0:ℝ) < Real.exp (Real.exp 1) := Real.exp_pos _
  3346. have hexp1 : (1:ℝ) ≤ Real.exp (Real.exp 1) := Real.one_le_exp (Real.exp_pos 1).le
  3347. have hxX1 : (X1:ℝ) < x := by linarith
  3348. have hxe : Real.exp (Real.exp 1) < x := by linarith
  3349. have hx1 : (1:ℝ) < x := by linarith
  3350. obtain ⟨V₀, hV₀0, hV₀odd, hV₀sq, hV₀card, hV₀nd, hV₀primes, hV₀rep, hV₀le⟩ := hseed p hp hpodd
  3351. have hInv₀ : Inv k₀ p V₀ := by
  3352. refine ⟨hV₀0, hV₀odd, hV₀sq, hV₀nd, by rw [hV₀card], ?_⟩
  3353. intro q hq
  3354. rw [hV₀card]
  3355. exact (hV₀primes q hq).2
  3356. have hprops := Vseq_props hE4 hk₀3 hstep hp hpodd hInv₀ hV₀rep
  3357. have hV0j : ∀ j, 0 < Vseq p V₀ j := fun j => (hprops j).1.1
  3358. have hoddj : ∀ j, Odd (Vseq p V₀ j) := fun j => (hprops j).1.2.1
  3359. have hsqj : ∀ j, Squarefree (Vseq p V₀ j) := fun j => (hprops j).1.2.2.1
  3360. have hndj : ∀ j, ¬ p ∣ Vseq p V₀ j := fun j => (hprops j).1.2.2.2.1
  3361. have hkkge : ∀ j, k₀ ≤ (Vseq p V₀ j).primeFactors.card := fun j => (hprops j).1.2.2.2.2.1
  3362. have hpfj : ∀ j, ∀ q ∈ (Vseq p V₀ j).primeFactors,
  3363. q ≤ 4 * Qp (Vseq p V₀ j).primeFactors.card := fun j => (hprops j).1.2.2.2.2.2
  3364. have hcardstep : ∀ j, (Vseq p V₀ (j + 1)).primeFactors.card
  3365. = (Vseq p V₀ j).primeFactors.card + tp (Vseq p V₀ j).primeFactors.card :=
  3366. fun j => (hprops j).2.2.1
  3367. have hsizestep : ∀ j, Vseq p V₀ (j + 1) ≤ Vseq p V₀ j *
  3368. (4 * Qp (Vseq p V₀ j).primeFactors.card) ^ tp (Vseq p V₀ j).primeFactors.card :=
  3369. fun j => (hprops j).2.2.2.1
  3370. have hltj : ∀ j, Vseq p V₀ j < Vseq p V₀ (j + 1) := fun j => (hprops j).2.2.2.2
  3371. have hk3j : ∀ j, 3 ≤ (Vseq p V₀ j).primeFactors.card := fun j => le_trans hk₀3 (hkkge j)
  3372. -- the sequence grows at least linearly
  3373. have hgrow : ∀ j, j + 1 ≤ Vseq p V₀ j := by
  3374. intro j
  3375. induction j with
  3376. | zero => have := hV0j 0; omega
  3377. | succ j ih => have := hltj j; omega
  3378. -- and at most quadratically
  3379. have hquad : ∀ j, 2 ^ E * Vseq p V₀ (j + 1) < (2 ^ E * Vseq p V₀ j) ^ 2 := by
  3380. intro j
  3381. have h1 := hsizestep j
  3382. have h2 := pow_step_le_two_pow (hk3j j) (hK1 _ (hkkge j))
  3383. have h3 : (2:ℕ) ^ (Vseq p V₀ j).primeFactors.card
  3384. < 3 ^ (Vseq p V₀ j).primeFactors.card :=
  3385. Nat.pow_lt_pow_left (by norm_num) (by have := hk3j j; omega)
  3386. have h4 : (3:ℕ) ^ (Vseq p V₀ j).primeFactors.card ≤ Vseq p V₀ j :=
  3387. three_pow_le_self (hoddj j) (hsqj j)
  3388. have hVpos := hV0j j
  3389. have haV : (4 * Qp (Vseq p V₀ j).primeFactors.card)
  3390. ^ tp (Vseq p V₀ j).primeFactors.card < Vseq p V₀ j := by omega
  3391. calc 2 ^ E * Vseq p V₀ (j + 1)
  3392. ≤ 2 ^ E * (Vseq p V₀ j *
  3393. (4 * Qp (Vseq p V₀ j).primeFactors.card) ^ tp (Vseq p V₀ j).primeFactors.card) :=
  3394. Nat.mul_le_mul_left _ h1
  3395. _ < 2 ^ E * (Vseq p V₀ j * Vseq p V₀ j) :=
  3396. mul_lt_mul_of_pos_left (mul_lt_mul_of_pos_left haV hVpos) h2Epos
  3397. _ ≤ (2 ^ E * Vseq p V₀ j) ^ 2 := by
  3398. have hrw : (2 ^ E * Vseq p V₀ j) ^ 2 = 2 ^ E * (2 ^ E * (Vseq p V₀ j * Vseq p V₀ j)) := by
  3399. ring
  3400. rw [hrw]
  3401. exact Nat.mul_le_mul_left _ (Nat.le_mul_of_pos_left _ h2Epos)
  3402. -- the first index beyond `x`
  3403. have hexists : ∃ j, x ≤ ((2 ^ E * Vseq p V₀ j : ℕ) : ℝ) := by
  3404. refine ⟨⌈x⌉₊, ?_⟩
  3405. have h1 : x ≤ (⌈x⌉₊ : ℝ) := Nat.le_ceil x
  3406. have h2 : ⌈x⌉₊ + 1 ≤ Vseq p V₀ ⌈x⌉₊ := hgrow ⌈x⌉₊
  3407. have h3 : Vseq p V₀ ⌈x⌉₊ ≤ 2 ^ E * Vseq p V₀ ⌈x⌉₊ := Nat.le_mul_of_pos_left _ h2Epos
  3408. have h4 : (⌈x⌉₊ : ℝ) ≤ ((2 ^ E * Vseq p V₀ ⌈x⌉₊ : ℕ) : ℝ) := by
  3409. have : ⌈x⌉₊ ≤ 2 ^ E * Vseq p V₀ ⌈x⌉₊ := by omega
  3410. exact_mod_cast this
  3411. linarith
  3412. set j := Nat.find hexists with hjdef
  3413. have hxB : x ≤ ((2 ^ E * Vseq p V₀ j : ℕ) : ℝ) := Nat.find_spec hexists
  3414. -- the seed is small
  3415. have hQ4mono : ∀ m : ℕ, 3 ≤ m → m ≤ k₁ → (4 * Qp m) ^ m ≤ (4 * Qp k₁) ^ k₁ := by
  3416. intro m hm3 hmk₁
  3417. have hQ : Qp m ≤ Qp k₁ := Qp_mono hm3 hmk₁
  3418. have hQ1 : 1 ≤ Qp k₁ := Qp_pos k₁
  3419. calc (4 * Qp m) ^ m ≤ (4 * Qp k₁) ^ m := Nat.pow_le_pow_left (by omega) _
  3420. _ ≤ (4 * Qp k₁) ^ k₁ := Nat.pow_le_pow_right (by omega) hmk₁
  3421. -- at the chosen index the number of prime factors is large
  3422. have hkkbig : k₁ < (Vseq p V₀ j).primeFactors.card := by
  3423. by_contra hcon
  3424. push_neg at hcon
  3425. have h1 : Vseq p V₀ j ≤ (4 * Qp (Vseq p V₀ j).primeFactors.card)
  3426. ^ (Vseq p V₀ j).primeFactors.card :=
  3427. self_le_pow_of_primeFactors_le (hsqj j) (hpfj j)
  3428. have h2 := hQ4mono _ (hk3j j) hcon
  3429. have h3 : 2 ^ E * Vseq p V₀ j ≤ X1 := by
  3430. rw [hX1def]
  3431. exact Nat.mul_le_mul_left _ (le_trans h1 h2)
  3432. have h4 : ((2 ^ E * Vseq p V₀ j : ℕ) : ℝ) ≤ (X1:ℝ) := by exact_mod_cast h3
  3433. linarith
  3434. -- the chosen `B` is below `x ^ 2`
  3435. have hBx2 : ((2 ^ E * Vseq p V₀ j : ℕ) : ℝ) < x ^ 2 := by
  3436. rcases Nat.eq_zero_or_pos j with hj0 | hjpos
  3437. · have hle : 2 ^ E * Vseq p V₀ 0 ≤ X1 := by
  3438. have h1 : (4 * Qp k₀) ^ k₀ ≤ (4 * Qp k₁) ^ k₁ := hQ4mono _ hk₀3 (le_max_left _ _)
  3439. have h2 : 2 ^ E * V₀ ≤ X1 := by
  3440. rw [hX1def]
  3441. exact le_trans hV₀le (Nat.mul_le_mul_left _ h1)
  3442. simpa [Vseq] using h2
  3443. have h5 : ((2 ^ E * Vseq p V₀ 0 : ℕ) : ℝ) ≤ (X1:ℝ) := by exact_mod_cast hle
  3444. rw [hj0]
  3445. nlinarith
  3446. · obtain ⟨m, hm⟩ : ∃ m, j = m + 1 := ⟨j - 1, by omega⟩
  3447. have hmin : ¬ (x ≤ ((2 ^ E * Vseq p V₀ m : ℕ) : ℝ)) := Nat.find_min hexists (by omega)
  3448. push_neg at hmin
  3449. have hq := hquad m
  3450. have hqR : ((2 ^ E * Vseq p V₀ (m + 1) : ℕ) : ℝ)
  3451. < ((2 ^ E * Vseq p V₀ m : ℕ) : ℝ) ^ 2 := by exact_mod_cast hq
  3452. have hnn : (0:ℝ) ≤ ((2 ^ E * Vseq p V₀ m : ℕ) : ℝ) := by positivity
  3453. have hlt2 : ((2 ^ E * Vseq p V₀ m : ℕ) : ℝ) ^ 2 < x ^ 2 := by nlinarith
  3454. rw [hm]
  3455. linarith
  3456. -- the analytic estimates
  3457. have hU_le_u : U ≤ Real.log ((Vseq p V₀ j).primeFactors.card) := by
  3458. have h1 : Real.exp U ≤ (⌈Real.exp U⌉₊ : ℝ) := Nat.le_ceil _
  3459. have h2 : ⌈Real.exp U⌉₊ ≤ k₁ := le_max_right _ _
  3460. have h2R : ((⌈Real.exp U⌉₊ : ℕ) : ℝ) ≤ (k₁:ℝ) := by exact_mod_cast h2
  3461. have h3 : (k₁:ℝ) < (((Vseq p V₀ j).primeFactors.card : ℕ) : ℝ) := by exact_mod_cast hkkbig
  3462. have h4 : Real.exp U < (((Vseq p V₀ j).primeFactors.card : ℕ) : ℝ) := by linarith
  3463. have h5 := Real.log_lt_log (Real.exp_pos U) h4
  3464. rw [Real.log_exp] at h5
  3465. linarith
  3466. have hu1 : 1 ≤ Real.log ((Vseq p V₀ j).primeFactors.card) := le_trans hU1 hU_le_u
  3467. have hu3 : 3 ≤ Real.log ((Vseq p V₀ j).primeFactors.card) := (hK1 _ (hkkge j)).1
  3468. have hjle : (j:ℝ) ≤ Fpot (Real.log ((Vseq p V₀ j).primeFactors.card))
  3469. + 2 * Real.log ((Vseq p V₀ j).primeFactors.card) :=
  3470. step_count hK1 (fun i => (Vseq p V₀ i).primeFactors.card) (hkkge 0) hcardstep j
  3471. have hkjpos : (0:ℝ) < (((Vseq p V₀ j).primeFactors.card : ℕ) : ℝ) := by
  3472. have := hk3j j
  3473. have : (0:ℕ) < (Vseq p V₀ j).primeFactors.card := by omega
  3474. exact_mod_cast this
  3475. have hlogVlow : (((Vseq p V₀ j).primeFactors.card : ℕ) : ℝ)
  3476. * Real.log ((Vseq p V₀ j).primeFactors.card) / 12 ≤ Real.log (Vseq p V₀ j) :=
  3477. log_self_lower (hsqj j) rfl hu3
  3478. have hlogB : Real.log (Vseq p V₀ j) ≤ Real.log ((2 ^ E * Vseq p V₀ j : ℕ) : ℝ) := by
  3479. have hle : (Vseq p V₀ j : ℝ) ≤ ((2 ^ E * Vseq p V₀ j : ℕ) : ℝ) := by
  3480. have : Vseq p V₀ j ≤ 2 ^ E * Vseq p V₀ j := Nat.le_mul_of_pos_left _ h2Epos
  3481. exact_mod_cast this
  3482. exact Real.log_le_log (by exact_mod_cast hV0j j) hle
  3483. have hBlog : Real.log ((2 ^ E * Vseq p V₀ j : ℕ) : ℝ) < 2 * Real.log x := by
  3484. have hBpos : (0:ℝ) < ((2 ^ E * Vseq p V₀ j : ℕ) : ℝ) := by
  3485. have : 0 < 2 ^ E * Vseq p V₀ j := by have := hV0j j; positivity
  3486. exact_mod_cast this
  3487. have h1 := Real.log_lt_log hBpos hBx2
  3488. rwa [Real.log_pow] at h1
  3489. have hlogx : (((Vseq p V₀ j).primeFactors.card : ℕ) : ℝ)
  3490. * Real.log ((Vseq p V₀ j).primeFactors.card) / 24 < Real.log x := by linarith
  3491. have hT : Real.log ((Vseq p V₀ j).primeFactors.card)
  3492. + Real.log (Real.log ((Vseq p V₀ j).primeFactors.card)) - Real.log 24
  3493. ≤ Real.log (Real.log x) := by
  3494. have hpos : (0:ℝ) < (((Vseq p V₀ j).primeFactors.card : ℕ) : ℝ)
  3495. * Real.log ((Vseq p V₀ j).primeFactors.card) / 24 := by positivity
  3496. have h1 := Real.log_le_log hpos hlogx.le
  3497. have h2 : Real.log ((((Vseq p V₀ j).primeFactors.card : ℕ) : ℝ)
  3498. * Real.log ((Vseq p V₀ j).primeFactors.card) / 24)
  3499. = Real.log ((Vseq p V₀ j).primeFactors.card)
  3500. + Real.log (Real.log ((Vseq p V₀ j).primeFactors.card)) - Real.log 24 := by
  3501. rw [Real.log_div (by positivity) (by norm_num),
  3502. Real.log_mul (by positivity) (by positivity)]
  3503. linarith [h2 ▸ h1]
  3504. refine ⟨2 ^ E * Vseq p V₀ j, H₀ + 4 * j, hxB, hBx2, ⟨Vseq p V₀ j, rfl⟩, ?_, ?_,
  3505. (hprops j).2.1, by omega, ?_⟩
  3506. · intro hdvd
  3507. rw [pow_succ] at hdvd
  3508. have h2 : 2 ∣ Vseq p V₀ j := (Nat.mul_dvd_mul_iff_left h2Epos).mp hdvd
  3509. have hV := hoddj j
  3510. rw [Nat.odd_iff] at hV
  3511. omega
  3512. · intro hdvd
  3513. rcases (Nat.Prime.dvd_mul hp).mp hdvd with h | h
  3514. · have hp2 : p ∣ 2 := Nat.Prime.dvd_of_dvd_pow hp h
  3515. have hpe : p = 2 := (Nat.prime_dvd_prime_iff_eq hp Nat.prime_two).mp hp2
  3516. rw [hpe] at hpodd
  3517. exact (by decide : ¬ Odd 2) hpodd
  3518. · exact hndj j h
  3519. · have habs := hUabs (Real.log ((Vseq p V₀ j).primeFactors.card)) (Real.log (Real.log x))
  3520. hU_le_u hT
  3521. have hlam : 0 < lam := lam_pos
  3522. have hc0 : c0 = 14 / lam := rfl
  3523. have hFpot : 4 * Fpot (Real.log ((Vseq p V₀ j).primeFactors.card))
  3524. = c0 * (Real.log ((Vseq p V₀ j).primeFactors.card)) ^ 2
  3525. + (6 * c0 / 7) * (Real.log ((Vseq p V₀ j).primeFactors.card)
  3526. * Real.log (Real.log ((Vseq p V₀ j).primeFactors.card))
  3527. - Real.log ((Vseq p V₀ j).primeFactors.card)) := by
  3528. unfold Fpot
  3529. rw [hc0]
  3530. field_simp
  3531. ring
  3532. have hH₀nn : (0:ℝ) ≤ (H₀:ℝ) := by positivity
  3533. push_cast
  3534. nlinarith [mul_nonneg hH₀nn
  3535. (show (0:ℝ) ≤ Real.log ((Vseq p V₀ j).primeFactors.card) - 1 by linarith),
  3536. mul_pos c0_pos (show (0:ℝ) < Real.log ((Vseq p V₀ j).primeFactors.card) by linarith)]
  3537. /-! ## Unit fractions and prescribed denominators -/
  3538. /-- Dividing by a divisor: `1 / (M / d) = d / M`. -/
  3539. theorem one_div_div_eq {M d : ℕ} (hM : 0 < M) (hd : d ∣ M) :
  3540. (1 : ℚ) / ((M / d : ℕ) : ℚ) = (d : ℚ) / (M : ℚ) := by
  3541. obtain ⟨q, rfl⟩ := hd
  3542. have hd0 : d ≠ 0 := by rintro rfl; simp at hM
  3543. have hq0 : q ≠ 0 := by rintro rfl; simp at hM
  3544. rw [Nat.mul_div_cancel_left _ (Nat.pos_of_ne_zero hd0)]
  3545. push_cast
  3546. field_simp
  3547. /-- The map `d ↦ M / d` is injective on divisors of `M`. -/
  3548. theorem div_injOn {M : ℕ} (hM : 0 < M) :
  3549. Set.InjOn (fun d => M / d) {d : ℕ | d ∣ M} := by
  3550. intro x hx y hy h
  3551. simp only [Set.mem_setOf_eq] at hx hy
  3552. obtain ⟨a, ha⟩ := hx
  3553. obtain ⟨b, hb⟩ := hy
  3554. have hx0 : 0 < x := Nat.pos_of_dvd_of_pos ⟨a, ha⟩ hM
  3555. have hy0 : 0 < y := Nat.pos_of_dvd_of_pos ⟨b, hb⟩ hM
  3556. simp only at h
  3557. have h1 : M / x * x = M := Nat.div_mul_cancel ⟨a, ha⟩
  3558. have h2 : M / y * y = M := Nat.div_mul_cancel ⟨b, hb⟩
  3559. have hM0 : 0 < M / x := Nat.div_pos (Nat.le_of_dvd hM ⟨a, ha⟩) hx0
  3560. rw [h] at h1
  3561. nlinarith [h1, h2]
  3562. /-- Quotient and remainder: a short unit-fraction expansion of `a / b`. -/
  3563. theorem quotient_remainder {B H a b : ℕ} (hb : 2 ≤ b) (hbB : b ≤ B) (hab : a < b)
  3564. (hRep : Rep B H) :
  3565. ∃ F₁ F₂ : Finset ℕ, Disjoint F₁ F₂ ∧ (∀ d ∈ F₁, 1 < d ∧ d ∣ B ∧ d ≤ B) ∧
  3566. (∀ e ∈ F₂, B < e) ∧ F₁.card ≤ H ∧ F₂.card ≤ H ∧
  3567. (∑ d ∈ F₁, (1:ℚ)/d) + (∑ e ∈ F₂, (1:ℚ)/e) = (a:ℚ)/(b:ℚ) ∧
  3568. (a = b - 1 → ¬ (b ∣ B) → b ∉ F₁ ∧ b ∉ F₂ ∧ F₂.Nonempty) := by
  3569. classical
  3570. have hB : 1 ≤ B := hRep.1
  3571. have hb0 : 0 < b := by omega
  3572. set q := a * B / b with hqdef
  3573. set r := a * B % b with hrdef
  3574. have hqr : b * q + r = a * B := Nat.div_add_mod (a * B) b
  3575. have hrb : r < b := Nat.mod_lt _ hb0
  3576. have hqB : q < B := by
  3577. by_contra hcon
  3578. push_neg at hcon
  3579. have h1 : b * B ≤ b * q := Nat.mul_le_mul_left b hcon
  3580. have h2 : a * B < b * B := by
  3581. exact Nat.mul_lt_mul_of_lt_of_le hab (le_refl B) (by omega)
  3582. omega
  3583. obtain ⟨D₁, hD₁, hcard₁, hsum₁⟩ := hRep.2 q hqB
  3584. obtain ⟨D₂, hD₂, hcard₂, hsum₂⟩ := hRep.2 r (lt_of_lt_of_le hrb hbB)
  3585. have hd₁dvd : ∀ d ∈ D₁, d ∣ B ∧ 0 < d := fun d hd => by
  3586. have := Nat.mem_divisors.mp (hD₁ hd); exact ⟨this.1, Nat.pos_of_mem_divisors (hD₁ hd)⟩
  3587. have hd₂dvd : ∀ e ∈ D₂, e ∣ B ∧ 0 < e := fun e he => by
  3588. have := Nat.mem_divisors.mp (hD₂ he); exact ⟨this.1, Nat.pos_of_mem_divisors (hD₂ he)⟩
  3589. have hd₁le : ∀ d ∈ D₁, d ≤ q := fun d hd => by
  3590. rw [← hsum₁]; exact Finset.single_le_sum (f := fun x => x) (by simp) hd
  3591. have hd₂le : ∀ e ∈ D₂, e ≤ r := fun e he => by
  3592. rw [← hsum₂]; exact Finset.single_le_sum (f := fun x => x) (by simp) he
  3593. have hinj₁ : Set.InjOn (fun d => B / d) ↑D₁ := by
  3594. intro x hx y hy h
  3595. exact div_injOn (show 0 < B by omega) (hd₁dvd x (by simpa using hx)).1
  3596. (hd₁dvd y (by simpa using hy)).1 h
  3597. have hinj₂ : Set.InjOn (fun e => b * B / e) ↑D₂ := by
  3598. intro x hx y hy h
  3599. refine div_injOn (M := b * B) (by positivity) ?_ ?_ h
  3600. · exact Dvd.dvd.mul_left (hd₂dvd x (by simpa using hx)).1 b
  3601. · exact Dvd.dvd.mul_left (hd₂dvd y (by simpa using hy)).1 b
  3602. refine ⟨D₁.image (fun d => B / d), D₂.image (fun e => b * B / e), ?_, ?_, ?_, ?_, ?_, ?_, ?_⟩
  3603. · -- disjointness
  3604. rw [Finset.disjoint_left]
  3605. intro x hx hx'
  3606. obtain ⟨d, hd, rfl⟩ := Finset.mem_image.mp hx
  3607. obtain ⟨e, he, heq⟩ := Finset.mem_image.mp hx'
  3608. have h1 : B / d ≤ B := Nat.div_le_self B d
  3609. have h2 : B < b * B / e := by
  3610. obtain ⟨hedvd, hepos⟩ := hd₂dvd e he
  3611. have hBe : e * (B / e) = B := Nat.mul_div_cancel' hedvd
  3612. have hBe' : 0 < B / e := Nat.div_pos (Nat.le_of_dvd (by omega) hedvd) hepos
  3613. have : b * B / e = b * (B / e) := by
  3614. rw [Nat.mul_div_assoc b hedvd]
  3615. rw [this]
  3616. calc B = e * (B / e) := hBe.symm
  3617. _ < b * (B / e) := by
  3618. apply Nat.mul_lt_mul_of_lt_of_le _ (le_refl _) hBe'
  3619. exact lt_of_le_of_lt (hd₂le e he) hrb
  3620. omega
  3621. · -- members of F₁
  3622. intro x hx
  3623. obtain ⟨d, hd, rfl⟩ := Finset.mem_image.mp hx
  3624. obtain ⟨hdvd, hpos⟩ := hd₁dvd d hd
  3625. refine ⟨?_, Nat.div_dvd_of_dvd hdvd, Nat.div_le_self B d⟩
  3626. have hdq : d ≤ q := hd₁le d hd
  3627. have hdB : d < B := by omega
  3628. have hcancel : d * (B / d) = B := Nat.mul_div_cancel' hdvd
  3629. have hstep : d * 1 < d * (B / d) := by omega
  3630. exact lt_of_mul_lt_mul_left hstep (Nat.zero_le d)
  3631. · -- members of F₂
  3632. intro y hy
  3633. obtain ⟨e, he, rfl⟩ := Finset.mem_image.mp hy
  3634. obtain ⟨hedvd, hepos⟩ := hd₂dvd e he
  3635. have hBe : e * (B / e) = B := Nat.mul_div_cancel' hedvd
  3636. have hBe' : 0 < B / e := Nat.div_pos (Nat.le_of_dvd (by omega) hedvd) hepos
  3637. have heq : b * B / e = b * (B / e) := Nat.mul_div_assoc b hedvd
  3638. rw [heq]
  3639. calc B = e * (B / e) := hBe.symm
  3640. _ < b * (B / e) := by
  3641. apply Nat.mul_lt_mul_of_lt_of_le _ (le_refl _) hBe'
  3642. exact lt_of_le_of_lt (hd₂le e he) hrb
  3643. · exact le_trans (Finset.card_image_le) hcard₁
  3644. · exact le_trans (Finset.card_image_le) hcard₂
  3645. · -- the sum identity
  3646. rw [Finset.sum_image hinj₁, Finset.sum_image hinj₂]
  3647. have e₁ : ∀ d ∈ D₁, (1:ℚ)/((B / d : ℕ) : ℚ) = (d : ℚ)/(B : ℚ) := by
  3648. intro d hd
  3649. exact one_div_div_eq (by omega) (hd₁dvd d hd).1
  3650. have e₂ : ∀ e ∈ D₂, (1:ℚ)/((b * B / e : ℕ) : ℚ) = (e : ℚ)/((b * B : ℕ) : ℚ) := by
  3651. intro e he
  3652. exact one_div_div_eq (by positivity) (Dvd.dvd.mul_left (hd₂dvd e he).1 b)
  3653. rw [Finset.sum_congr rfl e₁, Finset.sum_congr rfl e₂, ← Finset.sum_div, ← Finset.sum_div]
  3654. have c₁ : ((∑ d ∈ D₁, d : ℕ) : ℚ) = (q : ℚ) := by exact_mod_cast hsum₁
  3655. have c₂ : ((∑ e ∈ D₂, e : ℕ) : ℚ) = (r : ℚ) := by exact_mod_cast hsum₂
  3656. push_cast at c₁ c₂ ⊢
  3657. rw [c₁, c₂]
  3658. have hB0 : (B:ℚ) ≠ 0 := by positivity
  3659. have hb0' : (b:ℚ) ≠ 0 := by positivity
  3660. have hcast : (b:ℚ) * (q:ℚ) + (r:ℚ) = (a:ℚ) * (B:ℚ) := by exact_mod_cast hqr
  3661. field_simp
  3662. nlinarith [hcast]
  3663. · -- the final assertions
  3664. intro hab' hbB'
  3665. refine ⟨?_, ?_, ?_⟩
  3666. · intro hmem
  3667. obtain ⟨d, hd, heq⟩ := Finset.mem_image.mp hmem
  3668. exact hbB' (heq ▸ Nat.div_dvd_of_dvd (hd₁dvd d hd).1)
  3669. · intro hmem
  3670. obtain ⟨e, he, heq⟩ := Finset.mem_image.mp hmem
  3671. obtain ⟨hedvd, hepos⟩ := hd₂dvd e he
  3672. have hBe : e * (B / e) = B := Nat.mul_div_cancel' hedvd
  3673. have hBe' : 0 < B / e := Nat.div_pos (Nat.le_of_dvd (by omega) hedvd) hepos
  3674. have heq2 : b * B / e = b * (B / e) := Nat.mul_div_assoc b hedvd
  3675. have : B < b * B / e := by
  3676. rw [heq2]
  3677. calc B = e * (B / e) := hBe.symm
  3678. _ < b * (B / e) := by
  3679. apply Nat.mul_lt_mul_of_lt_of_le _ (le_refl _) hBe'
  3680. exact lt_of_le_of_lt (hd₂le e he) hrb
  3681. omega
  3682. · have hrpos : 0 < r := by
  3683. rcases Nat.eq_zero_or_pos r with h0 | h; swap
  3684. · exact h
  3685. exfalso
  3686. have hdvd : b ∣ a * B := by
  3687. have : a * B = b * q := by omega
  3688. exact ⟨q, this⟩
  3689. have : b ∣ B := by
  3690. have h1 : b ∣ b * B := Dvd.intro B rfl
  3691. have h2 : a * B + B = b * B := by
  3692. have : a + 1 = b := by omega
  3693. calc a * B + B = (a + 1) * B := by ring
  3694. _ = b * B := by rw [this]
  3695. have : b ∣ a * B + B := h2 ▸ h1
  3696. exact (Nat.dvd_add_right hdvd).mp this
  3697. exact hbB' this
  3698. have hne : D₂.Nonempty := by
  3699. rcases Finset.eq_empty_or_nonempty D₂ with rfl | h
  3700. · simp at hsum₂; omega
  3701. · exact h
  3702. exact hne.image _
  3703. /-- If `b ≥ 2` has no odd prime divisor then it is a power of two. -/
  3704. theorem eq_two_pow_of_no_odd_prime {b : ℕ} (hb : 2 ≤ b)
  3705. (h : ∀ p : ℕ, p.Prime → Odd p → ¬ p ∣ b) : ∃ r : ℕ, 1 ≤ r ∧ b = 2 ^ r := by
  3706. have hb0 : b ≠ 0 := by omega
  3707. have key : ∀ {d : ℕ}, Nat.Prime d → d ∣ b → d = 2 := by
  3708. intro d hd hdvd
  3709. by_contra hne
  3710. exact h d hd (hd.odd_of_ne_two hne) hdvd
  3711. have hpow := Nat.eq_prime_pow_of_unique_prime_dvd hb0 key
  3712. refine ⟨b.primeFactorsList.length, ?_, hpow⟩
  3713. by_contra hcon
  3714. have : b.primeFactorsList.length = 0 := by omega
  3715. rw [this] at hpow
  3716. simp at hpow
  3717. omega
  3718. /-- Avoiding a prescribed integer `b`. -/
  3719. theorem avoid_b : ∃ b₁ : ℕ, Real.exp (Real.exp 1) < b₁ ∧ 2 ≤ b₁ ∧
  3720. ∀ b : ℕ, b₁ ≤ b → ∃ B H : ℕ, b ≤ B ∧ 1 ≤ H ∧ ¬ (b ∣ B) ∧ Rep B H ∧
  3721. (H : ℝ) + 1 ≤ c0 * (Real.log (Real.log b)) ^ 2 := by
  3722. obtain ⟨E, x₀, hE, hx₀, hmain⟩ := uniform_rep
  3723. obtain ⟨b₁, hb₁x, hb₁E⟩ : ∃ n : ℕ, x₀ < (n : ℝ) ∧ (2:ℕ) ^ E < n := by
  3724. obtain ⟨n, hn⟩ := exists_nat_gt (max x₀ ((2 : ℝ) ^ E))
  3725. refine ⟨n, lt_of_le_of_lt (le_max_left _ _) hn, ?_⟩
  3726. have := lt_of_le_of_lt (le_max_right x₀ ((2:ℝ)^E)) hn
  3727. exact_mod_cast this
  3728. have h2E : (16:ℕ) ≤ 2 ^ E := by
  3729. calc (16:ℕ) = 2 ^ 4 := by norm_num
  3730. _ ≤ 2 ^ E := Nat.pow_le_pow_right (by norm_num) hE
  3731. refine ⟨b₁, lt_trans hx₀ hb₁x, by omega, ?_⟩
  3732. intro b hb
  3733. have hbx : x₀ ≤ (b : ℝ) := le_of_lt (lt_of_lt_of_le hb₁x (by exact_mod_cast hb))
  3734. by_cases hodd : ∃ p : ℕ, p.Prime ∧ Odd p ∧ p ∣ b
  3735. · obtain ⟨p, hp, hpodd, hpb⟩ := hodd
  3736. obtain ⟨B, H, hxB, hBx2, hdvdE, hndvdE, hnp, hRep, hH1, hcost⟩ :=
  3737. hmain (b : ℝ) hbx p hp hpodd
  3738. refine ⟨B, H, by exact_mod_cast hxB, hH1, ?_, hRep, hcost⟩
  3739. intro hcon
  3740. exact hnp (dvd_trans hpb hcon)
  3741. · push_neg at hodd
  3742. have hb2 : 2 ≤ b := by omega
  3743. obtain ⟨r, hr1, hrb⟩ := eq_two_pow_of_no_odd_prime hb2 (fun p hp hpo => hodd p hp hpo)
  3744. have hrE : E + 1 ≤ r := by
  3745. by_contra hcon
  3746. push_neg at hcon
  3747. have : b ≤ 2 ^ E := by
  3748. rw [hrb]
  3749. exact Nat.pow_le_pow_right (by norm_num) (by omega)
  3750. omega
  3751. obtain ⟨B, H, hxB, hBx2, hdvdE, hndvdE, hnp, hRep, hH1, hcost⟩ :=
  3752. hmain (b : ℝ) hbx 3 (by norm_num) (by decide)
  3753. refine ⟨B, H, by exact_mod_cast hxB, hH1, ?_, hRep, hcost⟩
  3754. intro hcon
  3755. apply hndvdE
  3756. refine dvd_trans ?_ hcon
  3757. rw [hrb]
  3758. exact pow_dvd_pow 2 hrE
  3759. /-- A short representation of `1` containing a prescribed large denominator. -/
  3760. theorem large_denominator {b₁ : ℕ} (hb₁2 : 2 ≤ b₁)
  3761. (H₁ : ∀ b : ℕ, b₁ ≤ b → ∃ B H : ℕ, b ≤ B ∧ 1 ≤ H ∧ ¬ (b ∣ B) ∧ Rep B H ∧
  3762. (H : ℝ) + 1 ≤ c0 * (Real.log (Real.log b)) ^ 2)
  3763. {b : ℕ} (hb : b₁ ≤ b) :
  3764. ∃ F : Finset ℕ, (∀ d ∈ F, 0 < d) ∧ (∑ d ∈ F, (1:ℚ)/d) = 1 ∧ b ∈ F ∧
  3765. (∃ d ∈ F, b < d) ∧ (F.card : ℝ) ≤ 2 * c0 * (Real.log (Real.log b)) ^ 2 := by
  3766. classical
  3767. obtain ⟨B, H, hbB, hH1, hnd, hRep, hcost⟩ := H₁ b hb
  3768. have hb2 : 2 ≤ b := le_trans hb₁2 hb
  3769. obtain ⟨F₁, F₂, hdisj, hF₁, hF₂, hc₁, hc₂, hsum, hextra⟩ :=
  3770. quotient_remainder hb2 hbB (show b - 1 < b by omega) hRep
  3771. obtain ⟨hnb₁, hnb₂, hne₂⟩ := hextra rfl hnd
  3772. have hbnot : b ∉ F₁ ∪ F₂ := by
  3773. simp only [Finset.mem_union, not_or]
  3774. exact ⟨hnb₁, hnb₂⟩
  3775. refine ⟨insert b (F₁ ∪ F₂), ?_, ?_, Finset.mem_insert_self _ _, ?_, ?_⟩
  3776. · intro x hx
  3777. rcases Finset.mem_insert.mp hx with rfl | hx
  3778. · omega
  3779. · rcases Finset.mem_union.mp hx with hx | hx
  3780. · exact lt_trans Nat.zero_lt_one (hF₁ x hx).1
  3781. · have := hF₂ x hx
  3782. omega
  3783. · rw [Finset.sum_insert hbnot, Finset.sum_union hdisj, hsum]
  3784. have hb0 : (b:ℚ) ≠ 0 := by
  3785. have : 0 < b := by omega
  3786. positivity
  3787. have hcast : ((b - 1 : ℕ) : ℚ) = (b : ℚ) - 1 := by
  3788. have h1 : (1:ℕ) ≤ b := by omega
  3789. push_cast [Nat.cast_sub h1]
  3790. ring
  3791. rw [hcast]
  3792. field_simp
  3793. ring
  3794. · obtain ⟨e, he⟩ := hne₂
  3795. exact ⟨e, Finset.mem_insert_of_mem (Finset.mem_union_right _ he),
  3796. lt_of_le_of_lt hbB (hF₂ e he)⟩
  3797. · have hcard : (insert b (F₁ ∪ F₂)).card ≤ 2 * H + 1 := by
  3798. rw [Finset.card_insert_of_notMem hbnot]
  3799. have := Finset.card_union_le F₁ F₂
  3800. omega
  3801. have h1 : ((insert b (F₁ ∪ F₂)).card : ℝ) ≤ 2 * (H : ℝ) + 1 := by
  3802. exact_mod_cast hcard
  3803. have h2 : 2 * (H : ℝ) + 1 ≤ 2 * ((H : ℝ) + 1) := by linarith
  3804. calc ((insert b (F₁ ∪ F₂)).card : ℝ) ≤ 2 * ((H : ℝ) + 1) := le_trans h1 h2
  3805. _ ≤ 2 * (c0 * (Real.log (Real.log b)) ^ 2) := by linarith
  3806. _ = 2 * c0 * (Real.log (Real.log b)) ^ 2 := by ring
  3807. /-- Increasing the number of terms by one. -/
  3808. theorem split_one {F : Finset ℕ} {b : ℕ} (hb : 2 ≤ b) (hpos : ∀ d ∈ F, 0 < d)
  3809. (hsum : (∑ d ∈ F, (1:ℚ)/d) = 1) (hbF : b ∈ F) (hbig : ∃ d ∈ F, b < d) :
  3810. ∃ F' : Finset ℕ, (∀ d ∈ F', 0 < d) ∧ (∑ d ∈ F', (1:ℚ)/d) = 1 ∧
  3811. F'.card = F.card + 1 ∧ b ∈ F' ∧ (∃ d ∈ F', b < d) := by
  3812. classical
  3813. obtain ⟨e, heF, hbe⟩ := hbig
  3814. have hne : F.Nonempty := ⟨b, hbF⟩
  3815. set d := F.max' hne with hd
  3816. have hdF : d ∈ F := F.max'_mem hne
  3817. have hbd : b < d := lt_of_lt_of_le hbe (F.le_max' e heF)
  3818. have hd2 : 2 ≤ d := by omega
  3819. have hdmax : ∀ x ∈ F, x ≤ d := fun x hx => F.le_max' x hx
  3820. have hlt1 : d < d + 1 := by omega
  3821. have hlt2 : d + 1 < d * (d + 1) := by nlinarith
  3822. have hnotmem1 : d + 1 ∉ insert (d * (d + 1)) (F.erase d) := by
  3823. simp only [Finset.mem_insert, Finset.mem_erase, not_or]
  3824. refine ⟨by omega, ?_⟩
  3825. rintro ⟨-, hmem⟩
  3826. exact absurd (hdmax _ hmem) (by omega)
  3827. have hnotmem2 : d * (d + 1) ∉ F.erase d := by
  3828. intro hmem
  3829. exact absurd (hdmax _ (Finset.mem_of_mem_erase hmem)) (by nlinarith)
  3830. refine ⟨insert (d + 1) (insert (d * (d + 1)) (F.erase d)), ?_, ?_, ?_, ?_, ?_⟩
  3831. · intro x hx
  3832. simp only [Finset.mem_insert, Finset.mem_erase] at hx
  3833. rcases hx with rfl | rfl | ⟨-, hx⟩
  3834. · omega
  3835. · positivity
  3836. · exact hpos x hx
  3837. · rw [Finset.sum_insert hnotmem1, Finset.sum_insert hnotmem2]
  3838. have hd0 : (d:ℚ) ≠ 0 := by
  3839. have : 0 < d := by omega
  3840. positivity
  3841. have hFsum : (∑ x ∈ F.erase d, (1:ℚ)/x) = 1 - 1/(d:ℚ) := by
  3842. have h := Finset.sum_erase_add F (fun x => (1:ℚ)/(x:ℚ)) hdF
  3843. rw [hsum] at h
  3844. linarith
  3845. push_cast
  3846. rw [hFsum]
  3847. field_simp
  3848. ring
  3849. · rw [Finset.card_insert_of_notMem hnotmem1, Finset.card_insert_of_notMem hnotmem2,
  3850. Finset.card_erase_of_mem hdF]
  3851. have : 1 ≤ F.card := Finset.card_pos.mpr hne
  3852. omega
  3853. · simp only [Finset.mem_insert, Finset.mem_erase]
  3854. exact Or.inr (Or.inr ⟨by omega, hbF⟩)
  3855. · exact ⟨d + 1, by simp, by omega⟩
  3856. /-- Every larger length is attainable. -/
  3857. theorem padding {F : Finset ℕ} {b : ℕ} (hb : 2 ≤ b) (hpos : ∀ d ∈ F, 0 < d)
  3858. (hsum : (∑ d ∈ F, (1:ℚ)/d) = 1) (hbF : b ∈ F) (hbig : ∃ d ∈ F, b < d)
  3859. {k : ℕ} (hk : F.card ≤ k) :
  3860. ∃ G : Finset ℕ, (∀ d ∈ G, 0 < d) ∧ (∑ d ∈ G, (1:ℚ)/d) = 1 ∧ G.card = k ∧ b ∈ G ∧
  3861. (∃ d ∈ G, b < d) := by
  3862. classical
  3863. obtain ⟨j, hj⟩ : ∃ j, k = F.card + j := ⟨k - F.card, by omega⟩
  3864. subst hj
  3865. clear hk
  3866. induction j generalizing F with
  3867. | zero => exact ⟨F, hpos, hsum, rfl, hbF, hbig⟩
  3868. | succ j ih =>
  3869. obtain ⟨F', hpos', hsum', hcard', hbF', hbig'⟩ := split_one hb hpos hsum hbF hbig
  3870. obtain ⟨G, hG1, hG2, hG3, hG4, hG5⟩ := ih hpos' hsum' hbF' hbig'
  3871. exact ⟨G, hG1, hG2, by omega, hG4, hG5⟩
  3872. /-- The telescoping identity `∑_{r=1}^{n} 1/(r(r+1)) = 1 - 1/(n+1)`. -/
  3873. theorem sum_telescope (n : ℕ) :
  3874. ∑ r ∈ Finset.Icc 1 n, (1:ℚ)/((r:ℚ)*((r:ℚ)+1)) = 1 - 1/((n:ℚ)+1) := by
  3875. induction n with
  3876. | zero => simp
  3877. | succ n ih =>
  3878. rw [Finset.sum_Icc_succ_top (by omega), ih]
  3879. have h1 : ((n:ℚ) + 1) ≠ 0 := by positivity
  3880. have h2 : ((n:ℚ) + 1 + 1) ≠ 0 := by positivity
  3881. push_cast
  3882. field_simp
  3883. ring
  3884. /-- A representation of `1` containing any fixed denominator `b ≥ 2`, of length at most `b+1`. -/
  3885. theorem fixed_denominator {b : ℕ} (hb : 2 ≤ b) :
  3886. ∃ F : Finset ℕ, (∀ d ∈ F, 0 < d) ∧ (∑ d ∈ F, (1:ℚ)/d) = 1 ∧ b ∈ F ∧
  3887. (∃ d ∈ F, b < d) ∧ F.card ≤ b + 1 := by
  3888. classical
  3889. set G : Finset ℕ := (Finset.Icc 1 (b-1)).image (fun r => r * (r+1)) with hGdef
  3890. have hinj : Set.InjOn (fun r => r * (r+1)) ↑(Finset.Icc 1 (b-1)) := by
  3891. intro x _ y _ h
  3892. simp only at h
  3893. nlinarith
  3894. have hcardG : G.card = b - 1 := by
  3895. rw [hGdef, Finset.card_image_of_injOn hinj, Nat.card_Icc]
  3896. omega
  3897. have hmemG : ∀ d ∈ G, ∃ r, 1 ≤ r ∧ r ≤ b - 1 ∧ d = r * (r+1) := by
  3898. intro d hd
  3899. rw [hGdef, Finset.mem_image] at hd
  3900. obtain ⟨r, hr, rfl⟩ := hd
  3901. rw [Finset.mem_Icc] at hr
  3902. exact ⟨r, hr.1, hr.2, rfl⟩
  3903. have hposG : ∀ d ∈ G, 0 < d := by
  3904. intro d hd
  3905. obtain ⟨r, hr1, -, rfl⟩ := hmemG d hd
  3906. positivity
  3907. have hleG : ∀ d ∈ G, d ≤ (b-1) * b := by
  3908. intro d hd
  3909. obtain ⟨r, hr1, hr2, rfl⟩ := hmemG d hd
  3910. have : r + 1 ≤ b := by omega
  3911. exact Nat.mul_le_mul hr2 this
  3912. have hevenG : ∀ d ∈ G, Even d := by
  3913. intro d hd
  3914. obtain ⟨r, -, -, rfl⟩ := hmemG d hd
  3915. exact Nat.even_mul_succ_self r
  3916. have hsumG : (∑ d ∈ G, (1:ℚ)/d) = 1 - 1/(b:ℚ) := by
  3917. rw [hGdef, Finset.sum_image hinj]
  3918. have := sum_telescope (b-1)
  3919. have hcast : ∀ r ∈ Finset.Icc 1 (b-1), (1:ℚ)/((r * (r+1) : ℕ) : ℚ)
  3920. = (1:ℚ)/((r:ℚ)*((r:ℚ)+1)) := by
  3921. intro r _
  3922. push_cast
  3923. ring
  3924. rw [Finset.sum_congr rfl hcast, this]
  3925. have : ((b - 1 : ℕ) : ℚ) + 1 = (b : ℚ) := by
  3926. have : ((b - 1 : ℕ) : ℚ) = (b : ℚ) - 1 := by
  3927. have : (1:ℕ) ≤ b := by omega
  3928. push_cast [Nat.cast_sub this]
  3929. ring
  3930. rw [this]; ring
  3931. rw [this]
  3932. by_cases hbG : b ∈ G
  3933. · -- `b` already occurs; adjoin `b+1` and `b(b+1)`
  3934. have hb1 : b + 1 ∉ G := by
  3935. intro hmem
  3936. have h1 := hevenG _ hmem
  3937. have h2 := hevenG _ hbG
  3938. rw [Nat.even_add_one] at h1
  3939. exact h1 h2
  3940. have hb2 : b * (b+1) ∉ insert (b+1) G := by
  3941. simp only [Finset.mem_insert, not_or]
  3942. constructor
  3943. · nlinarith
  3944. · intro hmem
  3945. have h1 := hleG _ hmem
  3946. have h2 : (b-1) * b ≤ b * b := Nat.mul_le_mul_right b (Nat.sub_le b 1)
  3947. have h3 : b * b < b * (b+1) := by nlinarith [show 0 < b by omega]
  3948. omega
  3949. refine ⟨insert (b * (b+1)) (insert (b+1) G), ?_, ?_, ?_, ?_, ?_⟩
  3950. · intro x hx
  3951. simp only [Finset.mem_insert] at hx
  3952. rcases hx with rfl | rfl | hx
  3953. · positivity
  3954. · omega
  3955. · exact hposG x hx
  3956. · rw [Finset.sum_insert hb2, Finset.sum_insert hb1, hsumG]
  3957. have hb0 : (b:ℚ) ≠ 0 := by
  3958. have : 0 < b := by omega
  3959. positivity
  3960. have hb10 : ((b:ℚ) + 1) ≠ 0 := by positivity
  3961. push_cast
  3962. field_simp
  3963. ring
  3964. · simp only [Finset.mem_insert]
  3965. exact Or.inr (Or.inr hbG)
  3966. · exact ⟨b * (b+1), by simp, by nlinarith⟩
  3967. · rw [Finset.card_insert_of_notMem hb2, Finset.card_insert_of_notMem hb1, hcardG]
  3968. omega
  3969. · -- `b` does not occur; adjoin it
  3970. have hb3 : 3 ≤ b := by
  3971. rcases Nat.lt_or_ge b 3 with h | h
  3972. · exfalso
  3973. have hb2' : b = 2 := by omega
  3974. apply hbG
  3975. rw [hGdef, Finset.mem_image]
  3976. exact ⟨1, by simp [hb2'], by omega⟩
  3977. · exact h
  3978. refine ⟨insert b G, ?_, ?_, ?_, ?_, ?_⟩
  3979. · intro x hx
  3980. simp only [Finset.mem_insert] at hx
  3981. rcases hx with rfl | hx
  3982. · omega
  3983. · exact hposG x hx
  3984. · rw [Finset.sum_insert hbG, hsumG]
  3985. ring
  3986. · simp
  3987. · refine ⟨(b-1) * b, ?_, ?_⟩
  3988. · simp only [Finset.mem_insert]
  3989. refine Or.inr ?_
  3990. rw [hGdef, Finset.mem_image]
  3991. refine ⟨b - 1, by simp [Finset.mem_Icc]; omega, ?_⟩
  3992. congr 1
  3993. omega
  3994. · nlinarith [Nat.sub_add_cancel (show 1 ≤ b by omega)]
  3995. · rw [Finset.card_insert_of_notMem hbG, hcardG]
  3996. omega
  3997. /-! ## The three main theorems -/
  3998. /-- **First main theorem.** There are infinitely many natural numbers `n` such that every integer
  3999. `1 ≤ m ≤ n` is a sum of at most `c₀ (log log n)^2` distinct positive divisors of `n`. -/
  4000. theorem infinitely_many_divisor_representable :
  4001. ∀ N : ℕ, ∃ n : ℕ, N < n ∧ ∀ m : ℕ, 1 ≤ m → m ≤ n →
  4002. ∃ F ⊆ n.divisors, (F.card : ℝ) ≤ c0 * (Real.log (Real.log n)) ^ 2 ∧
  4003. ∑ d ∈ F, d = m := by
  4004. intro N
  4005. obtain ⟨E, x₀, hE, hx₀, hmain⟩ := uniform_rep
  4006. obtain ⟨x, hxN, hxx₀⟩ : ∃ x : ℝ, (N : ℝ) < x ∧ x₀ ≤ x := by
  4007. obtain ⟨n, hn⟩ := exists_nat_gt (max (N : ℝ) x₀)
  4008. exact ⟨n, lt_of_le_of_lt (le_max_left _ _) hn,
  4009. le_of_lt (lt_of_le_of_lt (le_max_right _ _) hn)⟩
  4010. obtain ⟨B, H, hxB, hBx2, hdvdE, hndvdE, hnp, hRep, hH1, hcost⟩ :=
  4011. hmain x hxx₀ 3 (by norm_num) (by decide)
  4012. have hxe : Real.exp (Real.exp 1) < x := lt_of_lt_of_le hx₀ hxx₀
  4013. have hNB : N < B := by
  4014. have : (N : ℝ) < (B : ℝ) := lt_of_lt_of_le hxN hxB
  4015. exact_mod_cast this
  4016. refine ⟨B, hNB, ?_⟩
  4017. intro m hm1 hmB
  4018. have hmono : Real.log (Real.log x) ≤ Real.log (Real.log B) := loglog_le_loglog hxe hxB
  4019. have hloglog1 : 1 < Real.log (Real.log x) := one_lt_loglog hxe
  4020. have hsq : c0 * (Real.log (Real.log x)) ^ 2 ≤ c0 * (Real.log (Real.log B)) ^ 2 := by
  4021. have h0 : (0:ℝ) ≤ Real.log (Real.log x) := le_of_lt (lt_trans zero_lt_one hloglog1)
  4022. have := pow_le_pow_left₀ h0 hmono 2
  4023. nlinarith [c0_pos, this]
  4024. have hT : (H : ℝ) ≤ c0 * (Real.log (Real.log B)) ^ 2 := by linarith
  4025. have hT1 : (1:ℝ) ≤ c0 * (Real.log (Real.log B)) ^ 2 := by
  4026. have : (1:ℝ) ≤ (H : ℝ) := by exact_mod_cast hH1
  4027. linarith
  4028. exact hRep.all_le hT hT1 hm1 hmB
  4029. /-- **Second main theorem.** There is a `b₀` such that every rational `a / b ∈ (0,1)` with `b ≥ b₀`
  4030. is a sum of at most `2 c₀ (log log b)^2` distinct unit fractions. -/
  4031. theorem exists_short_egyptian_fraction :
  4032. ∃ b₀ : ℕ, ∀ b a : ℕ, b₀ ≤ b → 0 < a → a < b →
  4033. ∃ A : Finset ℕ, (∀ d ∈ A, 0 < d) ∧
  4034. (A.card : ℝ) ≤ 2 * c0 * (Real.log (Real.log b)) ^ 2 ∧
  4035. ∑ d ∈ A, (1 : ℚ) / d = (a : ℚ) / (b : ℚ) := by
  4036. classical
  4037. obtain ⟨E, x₀, hE, hx₀, hmain⟩ := uniform_rep
  4038. obtain ⟨b₀, hb₀⟩ := exists_nat_gt (max x₀ 2)
  4039. refine ⟨b₀, ?_⟩
  4040. intro b a hb ha hab
  4041. have hb₀x : x₀ < (b₀ : ℝ) := lt_of_le_of_lt (le_max_left _ _) hb₀
  4042. have hb₀2 : (2:ℝ) < (b₀ : ℝ) := lt_of_le_of_lt (le_max_right _ _) hb₀
  4043. have hb2 : 2 ≤ b := by
  4044. have : (2:ℝ) < (b:ℝ) := lt_of_lt_of_le hb₀2 (by exact_mod_cast hb)
  4045. exact_mod_cast le_of_lt this
  4046. have hbx : x₀ ≤ (b : ℝ) := le_of_lt (lt_of_lt_of_le hb₀x (by exact_mod_cast hb))
  4047. obtain ⟨B, H, hxB, hBx2, hdvdE, hndvdE, hnp, hRep, hH1, hcost⟩ :=
  4048. hmain (b : ℝ) hbx 3 (by norm_num) (by decide)
  4049. have hbB : b ≤ B := by exact_mod_cast hxB
  4050. obtain ⟨F₁, F₂, hdisj, hF₁, hF₂, hc₁, hc₂, hsum, -⟩ :=
  4051. quotient_remainder hb2 hbB hab hRep
  4052. refine ⟨F₁ ∪ F₂, ?_, ?_, ?_⟩
  4053. · intro x hx
  4054. rcases Finset.mem_union.mp hx with hx | hx
  4055. · exact lt_trans Nat.zero_lt_one (hF₁ x hx).1
  4056. · have := hF₂ x hx
  4057. have : 1 ≤ B := hRep.1
  4058. omega
  4059. · have hcard : (F₁ ∪ F₂).card ≤ 2 * H := by
  4060. have := Finset.card_union_le F₁ F₂
  4061. omega
  4062. have h1 : ((F₁ ∪ F₂).card : ℝ) ≤ 2 * (H : ℝ) := by exact_mod_cast hcard
  4063. calc ((F₁ ∪ F₂).card : ℝ) ≤ 2 * (H : ℝ) := h1
  4064. _ ≤ 2 * (c0 * (Real.log (Real.log b)) ^ 2) := by linarith
  4065. _ = 2 * c0 * (Real.log (Real.log b)) ^ 2 := by ring
  4066. · rw [Finset.sum_union hdisj]
  4067. exact hsum
  4068. /-- **Third main theorem.** There is a `k₀` such that for every `k ≥ k₀` and every integer
  4069. `2 ≤ b ≤ exp (exp (sqrt (k / (2 c₀))))` there is a set `A` of exactly `k` positive integers
  4070. with `b ∈ A` and `∑_{n ∈ A} 1/n = 1`. -/
  4071. theorem exists_egyptian_with_prescribed_denominator :
  4072. ∃ k₀ : ℕ, ∀ k : ℕ, k₀ ≤ k → ∀ b : ℕ, 2 ≤ b →
  4073. (b : ℝ) ≤ Real.exp (Real.exp (Real.sqrt ((k : ℝ) / (2 * c0)))) →
  4074. ∃ A : Finset ℕ, A.card = k ∧ (∀ d ∈ A, 0 < d) ∧ b ∈ A ∧
  4075. ∑ d ∈ A, (1 : ℚ) / d = 1 := by
  4076. classical
  4077. obtain ⟨b₁, hb₁e, hb₁2, hb₁main⟩ := avoid_b
  4078. refine ⟨b₁, ?_⟩
  4079. intro k hk b hb2 hbound
  4080. have hc0 : 0 < c0 := c0_pos
  4081. -- produce a representation of `1` containing `b` and some larger integer
  4082. have key : ∃ F : Finset ℕ, (∀ d ∈ F, 0 < d) ∧ (∑ d ∈ F, (1:ℚ)/d) = 1 ∧ b ∈ F ∧
  4083. (∃ d ∈ F, b < d) ∧ F.card ≤ k := by
  4084. by_cases hbig : b₁ ≤ b
  4085. · obtain ⟨F, hpos, hsum, hbF, hbig', hcard⟩ := large_denominator hb₁2 hb₁main hbig
  4086. refine ⟨F, hpos, hsum, hbF, hbig', ?_⟩
  4087. -- the size bound
  4088. have hbe : Real.exp (Real.exp 1) < (b : ℝ) :=
  4089. lt_of_lt_of_le hb₁e (by exact_mod_cast hbig)
  4090. have hll1 : 1 < Real.log (Real.log b) := one_lt_loglog hbe
  4091. have hsqrt : Real.log (Real.log b) ≤ Real.sqrt ((k : ℝ) / (2 * c0)) := by
  4092. have hb0 : (0:ℝ) < b := by positivity
  4093. have h1 : Real.log b ≤ Real.exp (Real.sqrt ((k : ℝ) / (2 * c0))) := by
  4094. have := Real.log_le_log hb0 hbound
  4095. rwa [Real.log_exp] at this
  4096. have hlogb0 : 0 < Real.log b := by
  4097. have : (1:ℝ) < (b:ℝ) := by exact_mod_cast hb2.trans_lt' one_lt_two
  4098. exact Real.log_pos this
  4099. have := Real.log_le_log hlogb0 h1
  4100. rwa [Real.log_exp] at this
  4101. have hnn : (0:ℝ) ≤ (k : ℝ) / (2 * c0) := by positivity
  4102. have hsq : (Real.log (Real.log b)) ^ 2 ≤ (k : ℝ) / (2 * c0) := by
  4103. have h0 : (0:ℝ) ≤ Real.log (Real.log b) := le_of_lt (lt_trans zero_lt_one hll1)
  4104. have := pow_le_pow_left₀ h0 hsqrt 2
  4105. rwa [Real.sq_sqrt hnn] at this
  4106. have hfinal : (F.card : ℝ) ≤ (k : ℝ) := by
  4107. calc (F.card : ℝ) ≤ 2 * c0 * (Real.log (Real.log b)) ^ 2 := hcard
  4108. _ ≤ 2 * c0 * ((k : ℝ) / (2 * c0)) := by
  4109. have : (0:ℝ) < 2 * c0 := by linarith
  4110. exact mul_le_mul_of_nonneg_left hsq (le_of_lt this)
  4111. _ = (k : ℝ) := by field_simp
  4112. exact_mod_cast hfinal
  4113. · push_neg at hbig
  4114. obtain ⟨F, hpos, hsum, hbF, hbig', hcard⟩ := fixed_denominator hb2
  4115. exact ⟨F, hpos, hsum, hbF, hbig', by omega⟩
  4116. obtain ⟨F, hpos, hsum, hbF, hbig', hcard⟩ := key
  4117. obtain ⟨G, hG1, hG2, hG3, hG4, -⟩ := padding hb2 hpos hsum hbF hbig' hcard
  4118. exact ⟨G, hG3, hG1, hG4, hG2⟩
  4119. #print axioms infinitely_many_divisor_representable
  4120. #print axioms exists_short_egyptian_fraction
  4121. #print axioms exists_egyptian_with_prescribed_denominator
  4122. end SDS