import Mathlib/-!# Practical numbers and Egyptian fractionsLet `c₀ = 14 / log 2`. We then have the following three results:* There are infinitely many `n` such that every `1 ≤ m ≤ n` is a sum of at most`c₀ (log log n)²` distinct divisors of `n`;* Every rational `a / b ∈ (0,1)` with `b` sufficiently large is the sum of atmost `2 c₀ (log log b)²` distinct unit fractions;* For every sufficiently large `k` and every integer `b` with`2 ≤ b ≤ exp (exp (√(k / (2 c₀))))` there is a `k`-element set `A` of positiveintegers containing `b` with `∑_{n ∈ A} 1/n = 1`.These results provide bounds Erdős Problems 18, 293 and 304 respectively. Theproofs were written down by ChatGPT and inspired by an earlier write-up ofChatGPT and posted by Liam Price, on the first of the problems mentioned above.https://www.overleaf.com/project/6a6209b061d71464999ab0e6All results have been formalized by Aristotle (aristotle-harmonic@harmonic.fun)https://github.com/Woett/ChatGPT-s-note-on-Erdos18/tree/mainLean version: leanprover/lean4:v4.28.0-/open Finsetnamespace SDS/-! ## The constant `c₀` and elementary `log log` facts -//-- `c₀ = 14 / log 2`. -/noncomputable def c0 : ℝ := 14 / Real.log 2theorem c0_pos : 0 < c0 := byhave : 0 < Real.log 2 := Real.log_pos (by norm_num)unfold c0; positivity/-- For `x > e^e` one has `log (log x) > 1`. -/theorem one_lt_loglog {x : ℝ} (hx : Real.exp (Real.exp 1) < x) : 1 < Real.log (Real.log x) := byhave hx0 : 0 < Real.exp (Real.exp 1) := Real.exp_pos _have hlog : Real.exp 1 < Real.log x := byhave := Real.log_lt_log hx0 hxrwa [Real.log_exp] at thishave h1 : Real.log (Real.exp 1) < Real.log (Real.log x) :=Real.log_lt_log (Real.exp_pos 1) hlogrwa [Real.log_exp] at h1/-- `log ∘ log` is monotone above `e^e`. -/theorem loglog_le_loglog {x y : ℝ} (hx : Real.exp (Real.exp 1) < x) (hxy : x ≤ y) :Real.log (Real.log x) ≤ Real.log (Real.log y) := byhave hx0 : (0:ℝ) < x := lt_trans (Real.exp_pos _) hxhave hlogx : Real.exp 1 < Real.log x := byhave := Real.log_lt_log (Real.exp_pos (Real.exp 1)) hxrwa [Real.log_exp] at thishave h1 : Real.log x ≤ Real.log y := Real.log_le_log hx0 hxyexact Real.log_le_log (lt_trans (Real.exp_pos 1) hlogx) h1/-! ## A Chebyshev-type lower bound and primes in `(q, 4q]`Mathlib provides the upper bound `θ x ≤ x log 4` and the comparison`|ψ x - θ x| ≤ 2 √x log x`. Here we prove the matching lower bound`log (centralBinom m) ≤ ψ (2m)` and deduce that the interval `(q, 4q]`contains at least `q / log (4q)` primes for all large `q`.-/section Chebyshevopen Filter Asymptotics ArithmeticFunction/-- `log` is little-o of `√·`. -/theorem log_isLittleO_sqrt : (fun x : ℝ => Real.log x) =o[atTop] Real.sqrt := byhave h := isLittleO_log_rpow_atTop (r := 1/2) (by norm_num)refine h.congr' (by rfl) ?_filter_upwards with xrw [Real.sqrt_eq_rpow]theorem sqrt_mul_log_isLittleO :(fun x : ℝ => Real.sqrt x * Real.log (4 * x)) =o[atTop] (fun x => x) := byhave h1 : (fun x : ℝ => Real.log (4 * x)) =o[atTop] Real.sqrt := byhave h2 : (fun x : ℝ => Real.log (4 * x)) =ᶠ[atTop] (fun x => Real.log 4 + Real.log x) := byfilter_upwards [eventually_gt_atTop (0:ℝ)] with x hxrw [Real.log_mul (by norm_num) (ne_of_gt hx)]refine Asymptotics.IsLittleO.congr' ?_ h2.symm (by rfl)refine Asymptotics.IsLittleO.add ?_ log_isLittleO_sqrtexact Asymptotics.isLittleO_const_left.mpr (Or.inr (bysimpa using Filter.tendsto_abs_atTop_atTop.comp Real.tendsto_sqrt_atTop))have := (Asymptotics.isBigO_refl Real.sqrt atTop).mul_isLittleO h1refine this.congr' (by rfl) ?_filter_upwards [eventually_ge_atTop (0:ℝ)] with x hxsimp [Real.mul_self_sqrt hx]theorem half_lt_log_two : (1:ℝ)/2 < Real.log 2 := byhave hsq : Real.exp (1/2) * Real.exp (1/2) = Real.exp 1 := byrw [← Real.exp_add]; norm_numhave he : Real.exp 1 < 4 := by linarith [Real.exp_one_lt_d9]have hpos : 0 < Real.exp (1/2 : ℝ) := Real.exp_pos _have h : Real.exp (1/2 : ℝ) < 2 := by nlinarithhave := Real.log_lt_log hpos hrwa [Real.log_exp] at thistheorem one_lt_log_four : (1:ℝ) < Real.log 4 := byhave h4 : Real.log 4 = 2 * Real.log 2 := byrw [show (4:ℝ) = 2 ^ 2 by norm_num, Real.log_pow]; push_cast; ringhave := half_lt_log_twolinarith/-- The error terms in the Chebyshev bounds are eventually small. -/theorem eventually_error_small : ∀ᶠ x : ℝ in atTop,Real.log (2 * x) + 4 * Real.sqrt x * Real.log (4 * x) ≤ (Real.log 4 - 1) * x := byhave hc : 0 < (Real.log 4 - 1) / 2 := by linarith [one_lt_log_four]have h1 : (fun x : ℝ => Real.log (2 * x)) =o[atTop] (fun x => x) := byhave h2 : (fun x : ℝ => Real.log (2 * x)) =ᶠ[atTop] (fun x => Real.log 2 + Real.log x) := byfilter_upwards [eventually_gt_atTop (0:ℝ)] with x hxrw [Real.log_mul (by norm_num) (ne_of_gt hx)]refine Asymptotics.IsLittleO.congr' ?_ h2.symm (by rfl)refine Asymptotics.IsLittleO.add ?_ Real.isLittleO_log_id_atTopexact Asymptotics.isLittleO_const_left.mpr (Or.inr (bysimpa using Filter.tendsto_abs_atTop_atTop))have b1 := h1.bound hchave b2 := (sqrt_mul_log_isLittleO.const_mul_left (4:ℝ)).bound hcfilter_upwards [b1, b2, eventually_ge_atTop (0:ℝ)] with x hx1 hx2 hx0rw [Real.norm_eq_abs, Real.norm_eq_abs, abs_of_nonneg hx0] at hx1rw [Real.norm_eq_abs, Real.norm_eq_abs, abs_of_nonneg hx0] at hx2have e1 : Real.log (2 * x) ≤ (Real.log 4 - 1) / 2 * x := le_trans (le_abs_self _) hx1have e2 : 4 * (Real.sqrt x * Real.log (4 * x)) ≤ (Real.log 4 - 1) / 2 * x :=le_trans (le_abs_self _) hx2linarith/-- The central binomial coefficient is at most `exp (ψ (2m))`. -/theorem log_centralBinom_le_psi {m : ℕ} (hm : 1 ≤ m) :Real.log (Nat.centralBinom m) ≤ Chebyshev.psi ((2 * m : ℕ) : ℝ) := byclassicalset N := Nat.centralBinom m with hNdefhave hNpos : 0 < N := Nat.centralBinom_pos mhave hchoose : N = (2 * m).choose m := rflhave hlog : Real.log N = ∑ p ∈ N.primeFactors, (N.factorization p : ℝ) * Real.log p := byconv_lhs => rw [show N = ∏ p ∈ N.primeFactors, p ^ N.factorization p from byrw [← Nat.support_factorization]; exact (Nat.factorization_prod_pow_eq_self hNpos.ne').symm]push_castrw [Real.log_prod]· exact Finset.sum_congr rfl fun p hp => by rw [Real.log_pow]· intro p hphave : 0 < p := (Nat.prime_of_mem_primeFactors hp).pospositivityset S : Finset ℕ := N.primeFactors.biUnion(fun p => (Finset.Icc 1 (N.factorization p)).image (fun j => p ^ j)) with hSdefhave hple : ∀ p ∈ N.primeFactors, p ^ N.factorization p ≤ 2 * m := byintro p hprw [hchoose]exact Nat.pow_factorization_choose_le (by omega)have hsub : S ⊆ Finset.Ioc 0 (2 * m) := byintro n hnrw [hSdef, Finset.mem_biUnion] at hnobtain ⟨p, hp, hn⟩ := hnobtain ⟨j, hj, rfl⟩ := Finset.mem_image.mp hnrw [Finset.mem_Icc] at hjhave hprime := Nat.prime_of_mem_primeFactors hprefine Finset.mem_Ioc.mpr ⟨Nat.pow_pos hprime.pos, ?_⟩calc p ^ j ≤ p ^ N.factorization p := Nat.pow_le_pow_right hprime.pos hj.2_ ≤ 2 * m := hple p hphave hdisj : (N.primeFactors : Set ℕ).PairwiseDisjoint(fun p => (Finset.Icc 1 (N.factorization p)).image (fun j => p ^ j)) := byintro p hp q hq hpqsimp only [Function.onFun, Finset.disjoint_left]intro x hx hx'obtain ⟨i, hi, rfl⟩ := Finset.mem_image.mp hxobtain ⟨j, hj, hji⟩ := Finset.mem_image.mp hx'rw [Finset.mem_Icc] at hi hjhave hpp := Nat.prime_of_mem_primeFactors hphave hqp := Nat.prime_of_mem_primeFactors hqhave hdvd : p ∣ q ^ j := byrw [hji]exact dvd_pow_self p (by omega)exact hpq ((Nat.prime_dvd_prime_iff_eq hpp hqp).mp (hpp.dvd_of_dvd_pow hdvd))have hsum : ∑ n ∈ S, Λ n = Real.log N := byrw [hSdef, Finset.sum_biUnion hdisj, hlog]refine Finset.sum_congr rfl fun p hp => ?_have hprime := Nat.prime_of_mem_primeFactors hprw [Finset.sum_image (byintro x _ y _ hexact Nat.pow_right_injective hprime.two_le h)]have hval : ∀ j ∈ Finset.Icc 1 (N.factorization p), Λ (p ^ j) = Real.log p := byintro j hjrw [Finset.mem_Icc] at hjrw [vonMangoldt_apply_pow (by omega), vonMangoldt_apply_prime hprime]rw [Finset.sum_congr rfl hval, Finset.sum_const, Nat.card_Icc]simphave hpsi : Chebyshev.psi ((2 * m : ℕ) : ℝ) = ∑ n ∈ Finset.Ioc 0 (2 * m), Λ n := byunfold Chebyshev.psirw [Nat.floor_natCast]rw [hpsi, ← hsum]exact Finset.sum_le_sum_of_subset_of_nonneg hsub (fun i _ _ => vonMangoldt_nonneg)/-- Chebyshev's lower bound for `ψ`. -/theorem psi_lower {m : ℕ} (hm : 4 ≤ m) :(m : ℝ) * Real.log 4 - Real.log m ≤ Chebyshev.psi ((2 * m : ℕ) : ℝ) := byhave h := Nat.four_pow_lt_mul_centralBinom m hmhave hm0 : (0:ℝ) < (m:ℝ) := by positivityhave hc0 : (0:ℝ) < (Nat.centralBinom m : ℝ) := byexact_mod_cast Nat.centralBinom_pos mhave hcast : ((4:ℝ)) ^ m < (m:ℝ) * (Nat.centralBinom m : ℝ) := by exact_mod_cast hhave hlog : Real.log ((4:ℝ) ^ m) ≤ Real.log ((m:ℝ) * (Nat.centralBinom m : ℝ)) :=Real.log_le_log (by positivity) hcast.lerw [Real.log_pow, Real.log_mul (ne_of_gt hm0) (ne_of_gt hc0)] at hloghave hkey := log_centralBinom_le_psi (show 1 ≤ m by omega)push_cast at hlog hkey ⊢linarith/-- Chebyshev's lower bound for `θ`. -/theorem theta_lower {m : ℕ} (hm : 4 ≤ m) :(m : ℝ) * Real.log 4 - Real.log m- 2 * Real.sqrt ((2 * m : ℕ) : ℝ) * Real.log ((2 * m : ℕ) : ℝ)≤ Chebyshev.theta ((2 * m : ℕ) : ℝ) := byhave h1 : (1:ℝ) ≤ ((2 * m : ℕ) : ℝ) := byhave : (8:ℕ) ≤ 2 * m := by omegahave : (8:ℝ) ≤ ((2*m : ℕ) : ℝ) := by exact_mod_cast thislinarithhave h2 := Chebyshev.abs_psi_sub_theta_le_sqrt_mul_log h1have h3 := psi_lower hmhave h4 : Chebyshev.psi ((2*m:ℕ):ℝ) - Chebyshev.theta ((2*m:ℕ):ℝ)≤ 2 * Real.sqrt ((2*m:ℕ):ℝ) * Real.log ((2*m:ℕ):ℝ) :=le_trans (le_abs_self _) h2linarith/-- **Primes in an interval.** -/theorem exists_q0 : ∃ q₀ : ℕ, 2 ≤ q₀ ∧ ∀ q : ℕ, q₀ ≤ q →(q : ℝ) / Real.log (4 * q) ≤ (((Finset.Ioc q (4 * q)).filter Nat.Prime).card : ℝ) := byclassicalobtain ⟨X, hX⟩ := Filter.eventually_atTop.mp eventually_error_smallrefine ⟨max 2 ⌈X⌉₊, le_max_left _ _, ?_⟩intro q hqhave hq2 : 2 ≤ q := le_trans (le_max_left _ _) hqhave hqX : X ≤ (q : ℝ) := byhave h1 : ⌈X⌉₊ ≤ q := le_trans (le_max_right _ _) hqexact le_trans (Nat.le_ceil X) (by exact_mod_cast h1)have hq0 : (0:ℝ) < (q:ℝ) := by positivityhave hlog4q : 0 < Real.log (4 * (q:ℝ)) := byrefine Real.log_pos ?_have : (2:ℝ) ≤ (q:ℝ) := by exact_mod_cast hq2linarith-- the Chebyshev boundshave hthlow := theta_lower (m := 2 * q) (by omega)have hcast4 : ((2 * (2 * q) : ℕ) : ℝ) = 4 * (q:ℝ) := by push_cast; ringhave hcast2 : ((2 * q : ℕ) : ℝ) = 2 * (q:ℝ) := by push_cast; ringrw [hcast4, hcast2] at hthlowhave hsqrt : Real.sqrt (4 * (q:ℝ)) = 2 * Real.sqrt (q:ℝ) := byrw [show (4:ℝ) * (q:ℝ) = 2 ^ 2 * (q:ℝ) by ring, Real.sqrt_mul (by positivity),Real.sqrt_sq (by norm_num)]rw [hsqrt] at hthlowhave hthup : Chebyshev.theta ((q : ℕ) : ℝ) ≤ Real.log 4 * (q:ℝ) :=Chebyshev.theta_le_log4_mul_x (by positivity)have herr := hX (q:ℝ) hqXhave hdiff : (q:ℝ) ≤ Chebyshev.theta (4 * (q:ℝ)) - Chebyshev.theta ((q:ℕ):ℝ) := bynlinarith [hthlow, hthup, herr]-- identify the difference as a sum over primes in the intervalset f : ℕ → ℝ := fun p => if p.Prime then Real.log p else 0 with hfhave hth1 : Chebyshev.theta ((q:ℕ):ℝ) = ∑ p ∈ Finset.Ioc 0 q, f p := byunfold Chebyshev.thetarw [Nat.floor_natCast, Finset.sum_filter]have hth2 : Chebyshev.theta (4 * (q:ℝ)) = ∑ p ∈ Finset.Ioc 0 (4 * q), f p := byhave : (4 : ℝ) * (q:ℝ) = ((4 * q : ℕ) : ℝ) := by push_cast; ringrw [this]unfold Chebyshev.thetarw [Nat.floor_natCast, Finset.sum_filter]have hsplit : (∑ p ∈ Finset.Ioc 0 q, f p) + (∑ p ∈ Finset.Ioc q (4 * q), f p)= ∑ p ∈ Finset.Ioc 0 (4 * q), f p :=Finset.sum_Ioc_consecutive f (Nat.zero_le q) (by omega)have hsum : Chebyshev.theta (4 * (q:ℝ)) - Chebyshev.theta ((q:ℕ):ℝ)= ∑ p ∈ Finset.Ioc q (4 * q), f p := byrw [hth1, hth2, ← hsplit]; ring-- bound the sum by the number of primes times log (4q)have hbound : (∑ p ∈ Finset.Ioc q (4 * q), f p)≤ (((Finset.Ioc q (4 * q)).filter Nat.Prime).card : ℝ) * Real.log (4 * (q:ℝ)) := byhave hrw : ∑ p ∈ Finset.Ioc q (4 * q), f p= ∑ p ∈ (Finset.Ioc q (4 * q)).filter Nat.Prime, Real.log p := byrw [Finset.sum_filter]rw [hrw]have hle : ∀ p ∈ (Finset.Ioc q (4 * q)).filter Nat.Prime,Real.log p ≤ Real.log (4 * (q:ℝ)) := byintro p hprw [Finset.mem_filter, Finset.mem_Ioc] at hphave hp1 : (p:ℝ) ≤ 4 * (q:ℝ) := byhave : (p : ℝ) ≤ ((4 * q : ℕ) : ℝ) := by exact_mod_cast hp.1.2rwa [show ((4 * q : ℕ) : ℝ) = 4 * (q:ℝ) by push_cast; ring] at thishave hp0 : (0:ℝ) < p := byhave := hp.2.posexact_mod_cast thisexact Real.log_le_log hp0 hp1calc ∑ p ∈ (Finset.Ioc q (4 * q)).filter Nat.Prime, Real.log p≤ ∑ _p ∈ (Finset.Ioc q (4 * q)).filter Nat.Prime, Real.log (4 * (q:ℝ)) :=Finset.sum_le_sum hle_ = (((Finset.Ioc q (4 * q)).filter Nat.Prime).card : ℝ) * Real.log (4 * (q:ℝ)) := byrw [Finset.sum_const, nsmul_eq_mul]rw [div_le_iff₀ hlog4q]calc (q:ℝ) ≤ Chebyshev.theta (4 * (q:ℝ)) - Chebyshev.theta ((q:ℕ):ℝ) := hdiff_ = ∑ p ∈ Finset.Ioc q (4 * q), f p := hsum_ ≤ _ := hboundend Chebyshev/-! ## The parameters `Q(k)`, `t(k)` and the threshold `K₁` -//-- `λ = log 2`. -/noncomputable def lam : ℝ := Real.log 2theorem lam_pos : 0 < lam := Real.log_pos (by norm_num)theorem lam_lt_one : lam < 1 := byhave h2 : (2:ℝ) < Real.exp 1 := by linarith [Real.exp_one_gt_d9]have := Real.log_lt_log (by norm_num : (0:ℝ) < 2) h2rwa [Real.log_exp] at thistheorem half_lt_lam : 1/2 < lam := half_lt_log_two/-- `Q(k) = ⌈k⁶ log k⌉` for `k ≥ 3`, and `1` otherwise. -/noncomputable def Qp (k : ℕ) : ℕ := if 3 ≤ k then ⌈(k:ℝ)^6 * Real.log k⌉₊ else 1/-- `a(k) = λ / (7 log k + 3 log log k)`. -/noncomputable def ap (k : ℕ) : ℝ := lam / (7 * Real.log k + 3 * Real.log (Real.log k))/-- `t(k) = ⌊a(k) k⌋` for `k ≥ 3`, and `0` otherwise. -/noncomputable def tp (k : ℕ) : ℕ := if 3 ≤ k then ⌊ap k * k⌋₊ else 0theorem Qp_pos (k : ℕ) : 0 < Qp k := byunfold Qpsplit· rename_i hhave hk : (3:ℝ) ≤ (k:ℝ) := by exact_mod_cast hhave : (1:ℝ) ≤ (k:ℝ)^6 * Real.log k := byhave h1 : (1:ℝ) ≤ (k:ℝ)^6 := one_le_pow₀ (by linarith)have h2 : (1:ℝ) ≤ Real.log k := byhave : Real.log 3 ≤ Real.log k := Real.log_le_log (by norm_num) hkhave h3 : (1:ℝ) ≤ Real.log 3 := byhave : Real.exp 1 < 3 := byhave := Real.exp_one_lt_d9linarithhave := Real.log_le_log (Real.exp_pos 1) (le_of_lt this)rwa [Real.log_exp] at thislinarithnlinarithexact Nat.lt_of_lt_of_le Nat.zero_lt_one (Nat.one_le_ceil_iff.mpr (by linarith))· norm_numtheorem one_le_log_of_three_le {k : ℕ} (hk : 3 ≤ k) : 1 ≤ Real.log k := byhave hk3 : (3:ℝ) ≤ (k:ℝ) := by exact_mod_cast hkhave h3 : (1:ℝ) ≤ Real.log 3 := byhave he : Real.exp 1 < 3 := by linarith [Real.exp_one_lt_d9]have := Real.log_le_log (Real.exp_pos 1) (le_of_lt he)rwa [Real.log_exp] at thisexact le_trans h3 (Real.log_le_log (by norm_num) hk3)theorem ap_pos {k : ℕ} (hk : 3 ≤ k) : 0 < ap k := byhave h1 : 1 ≤ Real.log k := one_le_log_of_three_le hkhave h2 : 0 ≤ Real.log (Real.log k) := Real.log_nonneg h1have hden : 0 < 7 * Real.log k + 3 * Real.log (Real.log k) := by linarithexact div_pos lam_pos hdentheorem ap_mul_den {k : ℕ} (hk : 3 ≤ k) :ap k * (7 * Real.log k + 3 * Real.log (Real.log k)) = lam := byhave h1 : 1 ≤ Real.log k := one_le_log_of_three_le hkhave h2 : 0 ≤ Real.log (Real.log k) := Real.log_nonneg h1have hden : 0 < 7 * Real.log k + 3 * Real.log (Real.log k) := by linarithunfold apfield_simptheorem tp_le_ap_mul {k : ℕ} (hk : 3 ≤ k) : (tp k : ℝ) ≤ ap k * k := byhave hap := ap_pos hkhave hk0 : (0:ℝ) ≤ (k:ℝ) := by positivityunfold tprw [if_pos hk]exact Nat.floor_le (by positivity)/-- `Q` is nondecreasing on integers `≥ 3`. -/theorem Qp_mono {j k : ℕ} (hj : 3 ≤ j) (hjk : j ≤ k) : Qp j ≤ Qp k := byhave hk : 3 ≤ k := le_trans hj hjkunfold Qprw [if_pos hj, if_pos hk]apply Nat.ceil_le_ceilhave hlj : 1 ≤ Real.log j := one_le_log_of_three_le hjhave hlk : Real.log j ≤ Real.log k := byhave hj0 : (0:ℝ) < (j:ℝ) := by positivityexact Real.log_le_log hj0 (by exact_mod_cast hjk)have hpow : (j:ℝ) ^ 6 ≤ (k:ℝ) ^ 6 := byhave : (j:ℝ) ≤ (k:ℝ) := by exact_mod_cast hjkhave hj0 : (0:ℝ) ≤ (j:ℝ) := by positivityexact pow_le_pow_left₀ hj0 this 6have hj6 : (0:ℝ) ≤ (j:ℝ) ^ 6 := by positivitynlinariththeorem log_ge_three {k : ℕ} (hk : 21 ≤ k) : 3 ≤ Real.log k := byhave he3 : Real.exp 3 < 21 := byhave h1 : Real.exp 3 = Real.exp 1 * (Real.exp 1 * Real.exp 1) := byrw [← Real.exp_add, ← Real.exp_add]; norm_numhave h2 : Real.exp 1 < 2.7182818286 := Real.exp_one_lt_d9have h3 : 0 < Real.exp 1 := Real.exp_pos 1rw [h1]nlinarithhave hk21 : (21:ℝ) ≤ (k:ℝ) := by exact_mod_cast hkhave : Real.exp 3 ≤ (k:ℝ) := le_of_lt (lt_of_lt_of_le he3 hk21)have := Real.log_le_log (Real.exp_pos 3) thisrwa [Real.log_exp] at thistheorem log_three_lt_log_eight : Real.log 8 ≤ 3 := byhave h8 : Real.log 8 = 3 * Real.log 2 := byrw [show (8:ℝ) = 2 ^ 3 by norm_num, Real.log_pow]; push_cast; ringhave := lam_lt_oneunfold lam at thislinarith/-- The package of parameter bounds valid for all large `k`. -/def K1P (k : ℕ) : Prop :=3 ≤ Real.log k ∧800 * (Real.log k) ^ 2 ≤ (k:ℝ) ∧16 * ((k:ℝ) + 1) ≤ (k:ℝ) ^ 6 ∧32 * (k:ℝ) ≤ (k:ℝ) ^ 6 ∧(k:ℝ) ^ 6 * Real.log k ≤ (Qp k : ℝ) ∧(Qp k : ℝ) ≤ 2 * (k:ℝ) ^ 6 * Real.log k ∧Real.log (4 * Qp k) ≤ 8 * Real.log k ∧Real.log (4 * Qp k) ≤ 7 * Real.log k + 3 * Real.log (Real.log k) ∧1 / (20 * Real.log k) ≤ ap k ∧ap k ≤ 1 / (7 * Real.log k) ∧ap k ≤ 1 / 2 ∧1 / (k:ℝ) ≤ (ap k) ^ 2 / 2 ∧1 ≤ tp k ∧ tp k ≤ k/-- **Bounds for the parameters.** -/theorem exists_K1 : ∃ K₁ : ℕ, 3 ≤ K₁ ∧ ∀ k : ℕ, K₁ ≤ k → K1P k := byunfold K1Phave hlogsqrt := log_isLittleO_sqrthave hlittle : (fun x : ℝ => Real.log x ^ 2) =o[Filter.atTop] (fun x : ℝ => x) := byhave h := hlogsqrt.mul hlogsqrtrefine h.congr' ?_ ?_· filter_upwards with x; ring· filter_upwards [Filter.eventually_ge_atTop (0:ℝ)] with x hxsimp [Real.mul_self_sqrt hx]have hb := hlittle.bound (show (0:ℝ) < 1/800 by norm_num)obtain ⟨X, hX⟩ := Filter.eventually_atTop.mp hbrefine ⟨max 21 ⌈X⌉₊, le_trans (by norm_num) (le_max_left _ _), ?_⟩intro k hkhave hk21 : 21 ≤ k := le_trans (le_max_left _ _) hkhave hk3 : 3 ≤ k := by omegahave hkX : X ≤ (k:ℝ) := byhave h1 : ⌈X⌉₊ ≤ k := le_trans (le_max_right _ _) hkexact le_trans (Nat.le_ceil X) (by exact_mod_cast h1)have hkR : (21:ℝ) ≤ (k:ℝ) := by exact_mod_cast hk21have hk0 : (0:ℝ) < (k:ℝ) := by linarithset u := Real.log k with huhave hu3 : 3 ≤ u := log_ge_three hk21have hu0 : (0:ℝ) < u := by linarithhave hlogu0 : 0 ≤ Real.log u := Real.log_nonneg (by linarith)have hlogu : Real.log u ≤ u := byhave := Real.log_le_sub_one_of_pos hu0linarithhave hlog8 : Real.log 8 ≤ 3 := log_three_lt_log_eight-- a quadratic bound for `log k`have hii : 800 * u ^ 2 ≤ (k:ℝ) := byhave hb1 := hX (k:ℝ) hkXrw [Real.norm_eq_abs, Real.norm_eq_abs, abs_of_nonneg (le_of_lt hk0)] at hb1have h2 : u ^ 2 ≤ |u ^ 2| := le_abs_self _have h3 : u ^ 2 ≤ 1/800 * (k:ℝ) := le_trans h2 hb1linarith-- powers of `k`have h5 : (32:ℝ) ≤ (k:ℝ) ^ 5 := byhave h := pow_le_pow_left₀ (by norm_num : (0:ℝ) ≤ 2) (by linarith : (2:ℝ) ≤ (k:ℝ)) 5norm_num at hlinarithhave hk6 : 32 * (k:ℝ) ≤ (k:ℝ) ^ 6 := byhave hrw : (k:ℝ) ^ 6 = (k:ℝ) ^ 5 * (k:ℝ) := by ringrw [hrw]exact mul_le_mul_of_nonneg_right h5 (by linarith)have hiii : 16 * ((k:ℝ) + 1) ≤ (k:ℝ) ^ 6 := by linarith-- two-sided bounds for `Q k`have hk6one : (1:ℝ) ≤ (k:ℝ) ^ 6 := one_le_pow₀ (by linarith)have hxge : (1:ℝ) ≤ (k:ℝ) ^ 6 * u := by nlinarithhave hQdef : (Qp k : ℝ) = (⌈(k:ℝ) ^ 6 * u⌉₊ : ℝ) := byunfold Qp; rw [if_pos hk3]have hQlow : (k:ℝ) ^ 6 * u ≤ (Qp k : ℝ) := byrw [hQdef]; exact Nat.le_ceil _have hQhigh : (Qp k : ℝ) ≤ 2 * (k:ℝ) ^ 6 * u := byrw [hQdef]have h := Nat.ceil_lt_add_one (show (0:ℝ) ≤ (k:ℝ) ^ 6 * u by positivity)linarith-- logs of Qhave hQpos : (0:ℝ) < (Qp k : ℝ) := byhave := Qp_pos kexact_mod_cast thishave hlogQ : Real.log (4 * (Qp k : ℝ)) ≤ Real.log 8 + 6 * u + Real.log u := byhave h1 : (4:ℝ) * (Qp k : ℝ) ≤ 8 * (k:ℝ) ^ 6 * u := by linarithhave h2 : Real.log ((4 : ℝ) * (Qp k : ℝ)) ≤ Real.log (8 * (k:ℝ) ^ 6 * u) :=Real.log_le_log (by positivity) h1have h3 : Real.log (8 * (k:ℝ) ^ 6 * u) = Real.log 8 + 6 * u + Real.log u := byrw [Real.log_mul (by positivity) (by linarith), Real.log_mul (by norm_num) (by positivity),Real.log_pow]push_castringlinarith [h3 ▸ h2]have hvi : Real.log (4 * (Qp k : ℝ)) ≤ 8 * u := by linarithhave hvii : Real.log (4 * (Qp k : ℝ)) ≤ 7 * u + 3 * Real.log u := by linarith-- the parameter ahave hden2 : 7 * u + 3 * Real.log u ≤ 10 * u := by linarithhave hdenpos : (0:ℝ) < 7 * u + 3 * Real.log u := by linarithhave hapdef : ap k = lam / (7 * u + 3 * Real.log u) := rflhave hlam1 : (1:ℝ)/2 < lam := half_lt_lamhave hlam2 : lam < 1 := lam_lt_onehave hviii1 : 1 / (20 * u) ≤ ap k := byrw [hapdef, div_le_div_iff₀ (by positivity) hdenpos]nlinarith [mul_pos (sub_pos.mpr hlam1) hu0]have hviii2 : ap k ≤ 1 / (7 * u) := byrw [hapdef, div_le_div_iff₀ hdenpos (by positivity)]nlinarith [mul_pos (sub_pos.mpr hlam2) hu0]have hviii3 : ap k ≤ 1 / 2 := byhave hstep : 1 / (7 * u) ≤ 1 / 2 := byrw [div_le_div_iff₀ (by positivity) (by norm_num)]linarithlinarith-- a lower bound for `a k ^ 2`have hix : 1 / (k:ℝ) ≤ (ap k) ^ 2 / 2 := byhave hpos20 : (0:ℝ) < 1 / (20 * u) := by positivityhave h2 : (1 / (20 * u)) ^ 2 ≤ (ap k) ^ 2 := pow_le_pow_left₀ hpos20.le hviii1 2have h3 : (1 / (20 * u)) ^ 2 / 2 = 1 / (800 * u ^ 2) := byfield_simpringhave h4 : 1 / (k:ℝ) ≤ 1 / (800 * u ^ 2) :=one_div_le_one_div_of_le (by positivity) hiilinarith [h3 ▸ h4]-- `t k` lies between `1` and `k`have h40 : 40 * u ≤ (k:ℝ) := by nlinarithhave hak2 : (2:ℝ) ≤ ap k * (k:ℝ) := byhave h1 : (1 / (20 * u)) * (k:ℝ) ≤ ap k * (k:ℝ) :=mul_le_mul_of_nonneg_right hviii1 hk0.lehave h2 : (2:ℝ) ≤ (1 / (20 * u)) * (k:ℝ) := byrw [div_mul_eq_mul_div, one_mul, le_div_iff₀ (by positivity)]linarithlinarithhave hapk_le : ap k * (k:ℝ) ≤ (k:ℝ) := byhave h1 : ap k * (k:ℝ) ≤ (1/2) * (k:ℝ) := mul_le_mul_of_nonneg_right hviii3 hk0.lelinarithhave hx1 : 1 ≤ tp k := byhave hfl : (1:ℕ) ≤ ⌊ap k * (k:ℝ)⌋₊ := (Nat.one_le_floor_iff _).mpr (by linarith)unfold tp; rw [if_pos hk3]; exact hflhave hx2 : tp k ≤ k := byhave hfl : ⌊ap k * (k:ℝ)⌋₊ ≤ ⌊((k:ℕ):ℝ)⌋₊ := Nat.floor_le_floor hapk_lerw [Nat.floor_natCast] at hflunfold tp; rw [if_pos hk3]; exact hflexact ⟨hu3, hii, hiii, hk6, hQlow, hQhigh, hvi, hvii, hviii1, hviii2, hviii3, hix, hx1, hx2⟩/-! ## The potential function`F(u) = (7/(2λ)) u² + (3/λ)(u log u - u)` is convex with `F'(u) = (7u + 3 log u)/λ`,and each step of the recursion increases `F(u) + 2u` by at least `1`.-//-- The potential `F`. -/noncomputable def Fpot (u : ℝ) : ℝ := (7 / (2 * lam)) * u ^ 2 + (3 / lam) * (u * Real.log u - u)theorem Fpot_nonneg {u : ℝ} (hu : 1 ≤ u) : 0 ≤ Fpot u := byhave hl : 0 < lam := lam_poshave hlog : 0 ≤ Real.log u := Real.log_nonneg huhave h1 : 0 ≤ (7 / (2 * lam)) * u ^ 2 - (3 / lam) * u := byhave h2 : (7 / (2 * lam)) * u ^ 2 - (3 / lam) * u = (u / lam) * (7 * u / 2 - 3) := byfield_simprw [h2]have h4 : (0:ℝ) ≤ 7 * u / 2 - 3 := by linarithhave h5 : (0:ℝ) ≤ u / lam := by positivityexact mul_nonneg h5 h4have h3 : 0 ≤ (3 / lam) * (u * Real.log u) := by positivityunfold Fpotnlinarith/-- Convexity of `F`, in the form of the tangent-line inequality. -/theorem Fpot_tangent {u u' : ℝ} (hu : 1 ≤ u) (hle : u ≤ u') :(u' - u) * (7 * u + 3 * Real.log u) / lam ≤ Fpot u' - Fpot u := byhave hl : 0 < lam := lam_poshave hu0 : 0 < u := by linarithhave hu'0 : 0 < u' := by linarith-- key: u' * log (u'/u) ≥ u' - uhave hkey : u' - u ≤ u' * (Real.log u' - Real.log u) := byrcases eq_or_lt_of_le hle with rfl | hlt· simp· have hr : 0 < u / u' := by positivityhave h1 : Real.log (u / u') ≤ u / u' - 1 := Real.log_le_sub_one_of_pos hrrw [Real.log_div (ne_of_gt hu0) (ne_of_gt hu'0)] at h1have h2 : Real.log u' - Real.log u ≥ 1 - u / u' := by linarithhave h3 : u' * (1 - u / u') = u' - u := by field_simpnlinarithhave hexp : Fpot u' - Fpot u - (u' - u) * (7 * u + 3 * Real.log u) / lam= ((7 / 2) * (u' - u) ^ 2 + 3 * (u' * (Real.log u' - Real.log u) - (u' - u))) / lam := byunfold Fpotfield_simpringhave hnn : 0 ≤ ((7:ℝ) / 2) * (u' - u) ^ 2 + 3 * (u' * (Real.log u' - Real.log u) - (u' - u)) := bynlinarith [sq_nonneg (u' - u)]have : 0 ≤ Fpot u' - Fpot u - (u' - u) * (7 * u + 3 * Real.log u) / lam := byrw [hexp]positivitylinarith/-- `log (1 + x) ≥ x - x²` for `x ≥ 0`. -/theorem log_one_add_ge {x : ℝ} (hx : 0 ≤ x) : x - x ^ 2 ≤ Real.log (1 + x) := byhave hpos : (0:ℝ) < 1 + x := by linarithhave h1 : Real.log (1 / (1 + x)) ≤ 1 / (1 + x) - 1 :=Real.log_le_sub_one_of_pos (by positivity)rw [Real.log_div (by norm_num) (ne_of_gt hpos), Real.log_one] at h1have h2 : 1 - 1 / (1 + x) ≤ Real.log (1 + x) := by linarithhave h3 : x - x ^ 2 ≤ 1 - 1 / (1 + x) := byhave key : (1 - 1 / (1 + x)) - (x - x ^ 2) = x ^ 3 / (1 + x) := byfield_simpringhave hnn : 0 ≤ x ^ 3 / (1 + x) := by positivitylinarithlinarith/-- One logarithmic increment. -/theorem delta_lower {k : ℕ} (hk : K1P k) :ap k - (3/2) * (ap k) ^ 2 ≤ Real.log ((k : ℝ) + tp k) - Real.log k := byobtain ⟨hu3, h800, -, -, -, -, -, -, hap1, hap2, hap3, hkinv, ht1, ht2⟩ := hkhave hk0 : (0:ℝ) < (k:ℝ) := by nlinarithhave hap0 : 0 < ap k := byhave : (0:ℝ) < 1 / (20 * Real.log k) := by positivitylinarithset x : ℝ := (tp k : ℝ) / (k:ℝ) with hxhave hx0 : 0 ≤ x := by positivityhave htfloor : (tp k : ℝ) ≤ ap k * (k:ℝ) := byunfold tphave hk3 : 3 ≤ k := byby_contra hconpush_neg at hconhave : (k:ℝ) < 3 := by exact_mod_cast hconnlinarithrw [if_pos hk3]exact Nat.floor_le (by positivity)have htfloor2 : ap k * (k:ℝ) < (tp k : ℝ) + 1 := byunfold tphave hk3 : 3 ≤ k := byby_contra hconpush_neg at hconhave : (k:ℝ) < 3 := by exact_mod_cast hconnlinarithrw [if_pos hk3]exact Nat.lt_floor_add_one _have hxle : x ≤ ap k := byrw [hx, div_le_iff₀ hk0]linarithhave hxge : ap k - 1 / (k:ℝ) ≤ x := byrw [hx, le_div_iff₀ hk0]have : (ap k - 1 / (k:ℝ)) * (k:ℝ) = ap k * (k:ℝ) - 1 := byfield_simprw [this]linarithhave hlogsplit : Real.log ((k:ℝ) + tp k) - Real.log k = Real.log (1 + x) := byrw [hx]rw [show (1:ℝ) + (tp k : ℝ) / (k:ℝ) = ((k:ℝ) + tp k) / (k:ℝ) by field_simp]rw [Real.log_div (by positivity) (ne_of_gt hk0)]rw [hlogsplit]have hmain : x - x ^ 2 ≤ Real.log (1 + x) := log_one_add_ge hx0have hx2 : x ^ 2 ≤ (ap k) ^ 2 := byapply pow_le_pow_left₀ hx0 hxlelinarith/-- Each step increases the potential `F(u) + 2u` by at least `1`. -/theorem potential_step {k : ℕ} (hk : K1P k) :Fpot (Real.log k) + 2 * Real.log k + 1≤ Fpot (Real.log ((k:ℝ) + tp k)) + 2 * Real.log ((k:ℝ) + tp k) := byhave hkc := hkobtain ⟨hu3, h800, -, -, -, -, -, -, hap1, hap2, hap3, hkinv, ht1, ht2⟩ := hkhave hk0 : (0:ℝ) < (k:ℝ) := by nlinarithset u := Real.log k with huset u' := Real.log ((k:ℝ) + tp k) with hu'have hap0 : 0 < ap k := byhave : (0:ℝ) < 1 / (20 * Real.log k) := by positivitylinarithhave hapsmall : ap k ≤ 1 / 21 := byhave h1 : 1 / (7 * u) ≤ 1 / 21 := byrw [div_le_div_iff₀ (by positivity) (by norm_num)]linarithlinarithhave hδ := delta_lower hkchave huu' : u ≤ u' := byrw [hu, hu']apply Real.log_le_log hk0have : (1:ℝ) ≤ (tp k : ℝ) := by exact_mod_cast ht1linarithhave htangent := Fpot_tangent (u := u) (u' := u') (by linarith) huu'-- rewrite the tangent slope in terms of `ap k`have hlogu0 : 0 ≤ Real.log u := Real.log_nonneg (by linarith)have hdenpos : (0:ℝ) < 7 * u + 3 * Real.log u := by linarithhave hslope : (7 * u + 3 * Real.log u) / lam = 1 / ap k := byunfold aprw [hu]field_simphave hδa : 1 - (3/2) * ap k ≤ (u' - u) / ap k := byrw [le_div_iff₀ hap0]nlinarithhave hkey : (u' - u) * (7 * u + 3 * Real.log u) / lam = (u' - u) / ap k := byrw [mul_div_assoc, hslope]field_simprw [hkey] at htangenthave h2δ : 2 * ap k - 3 * (ap k) ^ 2 ≤ 2 * (u' - u) := by linarithnlinarith [sq_nonneg (ap k)]/-- **Counting the steps.** -/theorem step_count {k₀ : ℕ} (hK1 : ∀ k : ℕ, k₀ ≤ k → K1P k)(kseq : ℕ → ℕ) (h0 : k₀ ≤ kseq 0)(hrec : ∀ j, kseq (j + 1) = kseq j + tp (kseq j)) :∀ j : ℕ, (j:ℝ) ≤ Fpot (Real.log (kseq j)) + 2 * Real.log (kseq j) := byhave hmono : ∀ j, k₀ ≤ kseq j := byintro jinduction j with| zero => exact h0| succ j ih =>rw [hrec j]omegaintro jinduction j with| zero =>have hk := hK1 (kseq 0) (hmono 0)have hu3 : 3 ≤ Real.log (kseq 0) := hk.1have := Fpot_nonneg (u := Real.log (kseq 0)) (by linarith)push_castlinarith| succ j ih =>have hk := hK1 (kseq j) (hmono j)have hstep := potential_step hkhave hcast : ((kseq j : ℝ) + tp (kseq j)) = ((kseq (j+1) : ℕ) : ℝ) := byrw [hrec j]; push_cast; ringrw [hcast] at hsteppush_castpush_cast at ihlinarith/-- **Absorbing the lower-order terms.** -/theorem absorb (c D L : ℝ) (hc : 0 < c) (hL : 0 ≤ L) :∃ U : ℝ, 1 ≤ U ∧ ∀ u H : ℝ, U ≤ u → u + Real.log u - L ≤ H →c * u ^ 2 + (6 * c / 7) * u * Real.log u + D * u + 1 ≤ c * H ^ 2 := byhave hpos : 0 < 8 * c / 7 := by positivityset M : ℝ := max L ((2 * c * L + D + 1) / (8 * c / 7)) with hMhave hM0 : 0 ≤ M := le_trans hL (le_max_left _ _)refine ⟨Real.exp M, Real.one_le_exp hM0, ?_⟩intro u H hu hHhave hlogU : Real.log (Real.exp M) = M := Real.log_exp Mhave hu1 : 1 ≤ u := le_trans (Real.one_le_exp hM0) huhave hu0 : (0:ℝ) < u := by linarithhave hMlogu : M ≤ Real.log u := byhave := Real.log_le_log (Real.exp_pos M) hurwa [hlogU] at thishave hlogu : L ≤ Real.log u := le_trans (le_max_left _ _) hMloguhave hbracket : 2 * c * L + D + 1 ≤ (8 * c / 7) * Real.log u := byhave h1 : (2 * c * L + D + 1) / (8 * c / 7) ≤ M := le_max_right _ _rw [div_le_iff₀ hpos] at h1nlinarithhave h1 : 0 ≤ u + Real.log u - L := by linarithhave hHu : u ≤ H := by linarithhave hsq : (u + Real.log u - L) ^ 2 ≤ H ^ 2 := by nlinarithhave hstep1 : c * (u + Real.log u - L) ^ 2 ≤ c * H ^ 2 := by nlinarithhave hstep2 : c * u ^ 2 + 2 * c * u * (Real.log u - L) ≤ c * (u + Real.log u - L) ^ 2 := bynlinarith [mul_nonneg hc.le (sq_nonneg (Real.log u - L))]have hstep3 : c * u ^ 2 + (6 * c / 7) * u * Real.log u + D * u + 1≤ c * u ^ 2 + 2 * c * u * (Real.log u - L) := bynlinarith [mul_nonneg (sub_nonneg.mpr hu1)(sub_nonneg.mpr (show (1:ℝ) ≤ (8 * c / 7) * Real.log u - 2 * c * L - D by linarith))]linarith/-! ## Squarefree integers, their divisors and differencesThe number of divisors, the splitting of the prime factors into two halves, and thebound for the number of large primes dividing the difference of two divisors.-/theorem three_le_of_mem_primeFactors {V p : ℕ} (hodd : Odd V) (hp : p ∈ V.primeFactors) :3 ≤ p := byhave hprime := Nat.prime_of_mem_primeFactors hphave hdvd := Nat.dvd_of_mem_primeFactors hphave hne2 : p ≠ 2 := byintro hrw [h] at hdvdrw [Nat.odd_iff] at hoddomegahave := hprime.two_leomegatheorem three_pow_le_self {V : ℕ} (hodd : Odd V) (hsq : Squarefree V) :3 ^ V.primeFactors.card ≤ V := byconv_rhs => rw [← Nat.prod_primeFactors_of_squarefree hsq]calc 3 ^ V.primeFactors.card = ∏ _p ∈ V.primeFactors, 3 := by rw [Finset.prod_const]_ ≤ ∏ p ∈ V.primeFactors, p :=Finset.prod_le_prod' (fun p hp => three_le_of_mem_primeFactors hodd hp)theorem self_le_pow_of_primeFactors_le {V M : ℕ} (hsq : Squarefree V)(hle : ∀ p ∈ V.primeFactors, p ≤ M) : V ≤ M ^ V.primeFactors.card := byconv_lhs => rw [← Nat.prod_primeFactors_of_squarefree hsq]calc ∏ p ∈ V.primeFactors, p ≤ ∏ _p ∈ V.primeFactors, M := Finset.prod_le_prod' hle_ = M ^ V.primeFactors.card := by rw [Finset.prod_const]/-- A squarefree integer has exactly `2^ω` divisors. -/theorem card_divisors_squarefree {n : ℕ} (hn : n ≠ 0) (h : Squarefree n) :n.divisors.card = 2 ^ n.primeFactors.card := byrw [Nat.card_divisors hn]have hc : ∀ p ∈ n.primeFactors, n.factorization p + 1 = 2 := byintro p hphave h1 : n.factorization p ≤ 1 := Squarefree.natFactorization_le_one p hhave h2 : 0 < n.factorization p := Nat.Prime.factorization_pos_of_dvd(Nat.prime_of_mem_primeFactors hp) hn (Nat.dvd_of_mem_primeFactors hp)omegarw [Finset.prod_congr rfl hc, Finset.prod_const]/-- **Splitting the prime factors.** -/theorem factor_split {V : ℕ} (hsq : Squarefree V) {k : ℕ}(hcard : V.primeFactors.card = k) :∃ V₁ V₂ : ℕ, 0 < V₁ ∧ 0 < V₂ ∧ V₁ * V₂ = V ∧ Nat.Coprime V₁ V₂ ∧V₁.primeFactors.card = k/2 ∧ V₂.primeFactors.card = k - k/2 := byclassicalobtain ⟨S, hS, hScard⟩ : ∃ S ⊆ V.primeFactors, S.card = k/2 :=Finset.le_card_iff_exists_subset_card.mp (by omega)refine ⟨∏ p ∈ S, p, ∏ p ∈ V.primeFactors \ S, p, ?_, ?_, ?_, ?_, ?_, ?_⟩· exact Finset.prod_pos (fun p hp => (Nat.prime_of_mem_primeFactors (hS hp)).pos)· exact Finset.prod_pos (fun p hp =>(Nat.prime_of_mem_primeFactors (Finset.mem_sdiff.mp hp).1).pos)· rw [mul_comm, Finset.prod_sdiff hS, Nat.prod_primeFactors_of_squarefree hsq]· refine Nat.Coprime.prod_left (fun p hp => Nat.Coprime.prod_right (fun q hq => ?_))have hpp := Nat.prime_of_mem_primeFactors (hS hp)have hqq := Nat.prime_of_mem_primeFactors (Finset.mem_sdiff.mp hq).1have hne : p ≠ q := byrintro rflexact (Finset.mem_sdiff.mp hq).2 hpexact (Nat.coprime_primes hpp hqq).mpr hne· rw [Nat.primeFactors_prod (fun p hp => Nat.prime_of_mem_primeFactors (hS hp)), hScard]· rw [Nat.primeFactors_prod (fun p hp =>Nat.prime_of_mem_primeFactors (Finset.mem_sdiff.mp hp).1),Finset.card_sdiff_of_subset hS, hScard, hcard]/-- **Differences of divisors**: at most `2k` primes larger than `Q` divide the differenceof two distinct divisors of `V`. -/theorem prime_divisors_of_diff {V Q k : ℕ} (hsq : Squarefree V)(hcard : V.primeFactors.card = k) (hle : ∀ p ∈ V.primeFactors, p ≤ 4*Q) (hQ4 : 4 ≤ Q){x y : ℕ} (hx : x ∈ V.divisors) (hy : y ∈ V.divisors) (hxy : x ≠ y){P : Finset ℕ} (hP : ∀ p ∈ P, p.Prime ∧ Q < p) :(P.filter (fun p : ℕ => (p:ℤ) ∣ (x:ℤ) - (y:ℤ))).card ≤ 2*k := byclassicalset T := P.filter (fun p : ℕ => (p:ℤ) ∣ (x:ℤ) - (y:ℤ)) with hThave hV0 : V ≠ 0 := (Nat.mem_divisors.mp hx).2have hxdvd := (Nat.mem_divisors.mp hx).1have hydvd := (Nat.mem_divisors.mp hy).1have hxle : x ≤ V := Nat.le_of_dvd (Nat.pos_of_ne_zero hV0) hxdvdhave hyle : y ≤ V := Nat.le_of_dvd (Nat.pos_of_ne_zero hV0) hydvdhave hx0 : 0 < x := Nat.pos_of_dvd_of_pos hxdvd (Nat.pos_of_ne_zero hV0)have hy0 : 0 < y := Nat.pos_of_dvd_of_pos hydvd (Nat.pos_of_ne_zero hV0)set M := ((x:ℤ) - (y:ℤ)).natAbs with hMhave hM0 : 0 < M := byrw [hM, Int.natAbs_pos]omegahave hMlt : M < V := byrw [hM]omegahave hdvd : ∀ p ∈ T, p ∣ M := byintro p hphave h := (Finset.mem_filter.mp hp).2have h2 := Int.natAbs_dvd_natAbs.mpr hsimpa using h2have hprod : (∏ p ∈ T, p) ∣ M :=Finset.prod_primes_dvd M (fun p hp => ((hP p (Finset.mem_filter.mp hp).1).1).prime) hdvdhave hQpow : Q ^ T.card ≤ ∏ p ∈ T, p := bycalc Q ^ T.card = ∏ _p ∈ T, Q := by rw [Finset.prod_const]_ ≤ ∏ p ∈ T, p := Finset.prod_le_prod' (fun p hp =>le_of_lt (hP p (Finset.mem_filter.mp hp).1).2)have hVle : V ≤ (4*Q) ^ k := byhave h := self_le_pow_of_primeFactors_le hsq hlerwa [hcard] at hhave h4Q : (4*Q) ^ k ≤ Q ^ (2*k) := byhave h1 : 4*Q ≤ Q * Q := by nlinarithcalc (4*Q)^k ≤ (Q*Q)^k := Nat.pow_le_pow_left h1 k_ = Q ^ (2*k) := by rw [← pow_two, ← pow_mul]have hchain : Q ^ T.card < Q ^ (2*k) := byhave h1 : ∏ p ∈ T, p ≤ M := Nat.le_of_dvd hM0 hprodomegahave hQ1 : 1 < Q := by omegaexact le_of_lt ((Nat.pow_lt_pow_iff_right hQ1).mp hchain)/-! ## Finite character sumsAdditive characters modulo `d`, the Fourier coefficients of a finite set of naturalnumbers, and the collision moments `M_d(X)`. The main result is `coverage`: a momentcondition guaranteeing that every residue class modulo an odd `A > 1` is of the form`z₀ + 2 z₁ + 4 z₂ + 8 z₃` with `zᵢ` divisors of `V`.-//-! ### Additive characters -//-- A primitive `d`-th root of unity. -/noncomputable def zet (d : ℕ) : ℂ := Complex.exp (2 * Real.pi * Complex.I / d)/-- The additive character `z ↦ exp (2πi z / d)`. -/noncomputable def ee (d : ℕ) (z : ℤ) : ℂ := (zet d) ^ ztheorem zet_ne_zero (d : ℕ) : zet d ≠ 0 := Complex.exp_ne_zero _theorem zet_norm (d : ℕ) : ‖zet d‖ = 1 := byrw [zet, Complex.norm_exp]; norm_numtheorem ee_norm (d : ℕ) (z : ℤ) : ‖ee d z‖ = 1 := bysimp [ee, norm_zpow, zet_norm]theorem ee_conj (d : ℕ) (z : ℤ) : (starRingEnd ℂ) (ee d z) = ee d (-z) := byhave h1 : (starRingEnd ℂ) (zet d) = (zet d)⁻¹ := by rw [← Complex.inv_eq_conj (zet_norm d)]simp [ee, map_zpow₀, h1, zpow_neg]theorem ee_add (d : ℕ) (z w : ℤ) : ee d (z + w) = ee d z * ee d w :=zpow_add₀ (zet_ne_zero d) z wtheorem ee_eq_one_iff {d : ℕ} (hd : d ≠ 0) (z : ℤ) : ee d z = 1 ↔ (d:ℤ) ∣ z :=(Complex.isPrimitiveRoot_exp d hd).zpow_eq_one_iff_dvd z/-- **Orthogonality.** -/theorem sum_ee {d : ℕ} (hd : d ≠ 0) (z : ℤ) :∑ h ∈ range d, ee d (h * z) = if (d:ℤ) ∣ z then (d:ℂ) else 0 := byhave hpow : ∀ h : ℕ, ee d (h * z) = (ee d z) ^ h := byintro hrw [ee, ee, ← zpow_natCast (zet d ^ z) h, ← zpow_mul]; ring_nfsimp only [hpow]by_cases hdvd : (d:ℤ) ∣ z· have h1 : ee d z = 1 := (ee_eq_one_iff hd z).mpr hdvdsimp [h1, hdvd]· have h1 : ee d z ≠ 1 := fun h => hdvd ((ee_eq_one_iff hd z).mp h)have h2 : (ee d z) ^ d = 1 := byrw [← ee_eq_one_iff hd ((d : ℤ) * z) |>.mpr ⟨z, rfl⟩, ee, ee,← zpow_natCast (zet d ^ z) d, ← zpow_mul]ring_nfrw [geom_sum_eq h1, h2]; simp [hdvd]/-! ### Moments and Fourier coefficients -//-- The collision moment `M_d(X)`. -/noncomputable def Mmom (X : Finset ℕ) (d : ℕ) : ℝ :=(((X ×ˢ X).filter (fun q => (d:ℤ) ∣ (q.1:ℤ) - (q.2:ℤ))).card : ℝ) / (X.card : ℝ) ^ 2/-- The Fourier coefficient `X̂_d(h)`. -/noncomputable def Xhat (X : Finset ℕ) (d : ℕ) (h : ℤ) : ℂ :=(X.card : ℂ)⁻¹ * ∑ x ∈ X, ee d (h * x)theorem Mmom_nonneg (X : Finset ℕ) (d : ℕ) : 0 ≤ Mmom X d := byunfold Mmom; positivitytheorem norm_sq_Xhat (X : Finset ℕ) (d : ℕ) (h : ℤ) :((‖Xhat X d h‖ : ℝ) : ℂ) ^ 2= (X.card : ℂ)⁻¹ * (X.card : ℂ)⁻¹ * ∑ q ∈ X ×ˢ X, ee d (h * ((q.1 : ℤ) - (q.2 : ℤ))) := byrw [← Complex.mul_conj' (Xhat X d h)]simp only [Xhat, map_mul, map_inv₀, Complex.conj_natCast, map_sum, ee_conj]rw [mul_mul_mul_comm, Finset.sum_product]congr 1rw [Finset.sum_mul_sum]refine Finset.sum_congr rfl fun x hx => Finset.sum_congr rfl fun y hy => ?_rw [← ee_add]ring_nf/-- **Plancherel identity.** -/theorem plancherel {X : Finset ℕ} (hX : X.Nonempty) {d : ℕ} (hd : d ≠ 0) :∑ h ∈ range d, ‖Xhat X d h‖ ^ 2 = d * Mmom X d := byhave hT0 : (X.card : ℝ) ≠ 0 := byhave : 0 < X.card := Finset.card_pos.mpr hXpositivityhave hTC : (X.card : ℂ) ≠ 0 := bysimpa using (Nat.cast_ne_zero (R := ℂ)).mpr (Finset.card_ne_zero.mpr hX)have key : ((∑ h ∈ range d, ‖Xhat X d h‖ ^ 2 : ℝ) : ℂ) = ((d * Mmom X d : ℝ) : ℂ) := bypush_castrw [Finset.sum_congr rfl (fun h (_ : h ∈ range d) => norm_sq_Xhat X d h), ← Finset.mul_sum,Finset.sum_comm]have hinner : ∀ q ∈ X ×ˢ X, (∑ h ∈ range d, ee d (h * ((q.1:ℤ) - (q.2:ℤ))))= if (d:ℤ) ∣ (q.1:ℤ) - (q.2:ℤ) then (d:ℂ) else 0 := fun q _ => sum_ee hd _rw [Finset.sum_congr rfl hinner, Finset.sum_ite, Finset.sum_const, Finset.sum_const_zero,Mmom]push_castfield_simpringexact_mod_cast key/-! ### Residue classes -//-- The number of elements of `X` in the residue class `b` modulo `d`. -/def resCount (X : Finset ℕ) (d b : ℕ) : ℕ := (X.filter (fun x => x % d = b)).cardtheorem sum_resCount_smul {M : Type*} [AddCommMonoid M] (X : Finset ℕ) {d : ℕ} (hd : d ≠ 0)(f : ℕ → M) : ∑ b ∈ range d, (resCount X d b) • f b = ∑ x ∈ X, f (x % d) := byclassicalrw [← Finset.sum_fiberwise_of_maps_to (t := range d) (g := fun x => x % d)(fun x _ => Finset.mem_range.mpr (Nat.mod_lt _ (Nat.pos_of_ne_zero hd)))(fun x => f (x % d))]refine Finset.sum_congr rfl fun b hb => ?_have : ∀ x ∈ X.filter (fun x => x % d = b), f (x % d) = f b := byintro x hxrw [(Finset.mem_filter.mp hx).2]rw [Finset.sum_congr rfl this, Finset.sum_const, resCount]theorem dvd_sub_iff_mod_eq {d x y : ℕ} : ((d:ℤ) ∣ (x:ℤ) - (y:ℤ)) ↔ x % d = y % d := byconstructor· intro hhave : (y:ℕ) ≡ x [MOD d] := (Nat.modEq_iff_dvd (n := d) (a := y) (b := x)).mpr (bysimpa using h)exact this.symm· intro hhave : (y:ℕ) ≡ x [MOD d] := h.symmhave := (Nat.modEq_iff_dvd (n := d) (a := y) (b := x)).mp thissimpa using thistheorem card_collision_eq (X : Finset ℕ) {d : ℕ} (hd : d ≠ 0) :((X ×ˢ X).filter (fun q => (d:ℤ) ∣ (q.1:ℤ) - (q.2:ℤ))).card= ∑ b ∈ range d, resCount X d b * resCount X d b := byclassicalhave h1 : ((X ×ˢ X).filter (fun q => (d:ℤ) ∣ (q.1:ℤ) - (q.2:ℤ))).card= ∑ x ∈ X, ∑ y ∈ X, if x % d = y % d then 1 else 0 := byrw [Finset.card_filter, Finset.sum_product]refine Finset.sum_congr rfl fun x hx => Finset.sum_congr rfl fun y hy => ?_by_cases h : x % d = y % d· rw [if_pos h, if_pos (dvd_sub_iff_mod_eq.mpr h)]· rw [if_neg h, if_neg (fun hc => h (dvd_sub_iff_mod_eq.mp hc))]have h2 : ∀ x : ℕ, (∑ y ∈ X, if x % d = y % d then 1 else 0) = resCount X d (x % d) := byintro xrw [resCount, Finset.card_filter]refine Finset.sum_congr rfl fun y hy => ?_by_cases h : x % d = y % d· rw [if_pos h, if_pos h.symm]· rw [if_neg h, if_neg (fun hc => h hc.symm)]rw [h1, Finset.sum_congr rfl (fun x _ => h2 x)]rw [← sum_resCount_smul X hd (fun b => resCount X d b)]exact Finset.sum_congr rfl fun b _ => by simp [smul_eq_mul]theorem Mmom_eq_sum_sq (X : Finset ℕ) {d : ℕ} (hd : d ≠ 0) :Mmom X d = ∑ b ∈ range d, ((resCount X d b : ℝ) / X.card) ^ 2 := byrw [Mmom, card_collision_eq X hd]push_castrw [Finset.sum_div]exact Finset.sum_congr rfl fun b _ => by ring/-! ### Invariance and permutations -/theorem ee_congr {d : ℕ} {z w : ℤ} (h : (d:ℤ) ∣ z - w) : ee d z = ee d w := byrcases Nat.eq_zero_or_pos d with rfl | hd· simp only [Nat.cast_zero, zero_dvd_iff, sub_eq_zero] at hrw [h]· have hd0 : d ≠ 0 := by omegahave h1 : ee d (z - w) = 1 := (ee_eq_one_iff hd0 _).mpr hhave h2 : ee d z = ee d (z - w) * ee d w := by rw [← ee_add]; ring_nfrw [h2, h1, one_mul]theorem Xhat_congr (X : Finset ℕ) {d : ℕ} {z w : ℤ} (h : (d:ℤ) ∣ z - w) :Xhat X d z = Xhat X d w := byunfold Xhatcongr 1refine Finset.sum_congr rfl fun x hx => ee_congr ?_have : z * x - w * x = (z - w) * x := by ringrw [this]exact Dvd.dvd.mul_right h _theorem sum_range_unit_perm {M : Type*} [AddCommMonoid M] {d u : ℕ} (hd : d ≠ 0)(hu : Nat.Coprime u d) (G : ℕ → M) :∑ b ∈ range d, G ((u * b) % d) = ∑ b ∈ range d, G b := byclassicalhave hinj : Set.InjOn (fun b => (u * b) % d) ((range d : Finset ℕ) : Set ℕ) := byintro x hx y hy hxysimp only [Finset.coe_range, Set.mem_Iio] at hx hyhave h1 : (u * x) ≡ (u * y) [MOD d] := hxyhave h2 : x ≡ y [MOD d] :=Nat.ModEq.cancel_left_of_coprime (by simpa [Nat.Coprime, Nat.gcd_comm] using hu) h1exact Nat.ModEq.eq_of_lt_of_lt h2 hx hyhave himage : (range d).image (fun b => (u * b) % d) = range d := byrefine Finset.eq_of_subset_of_card_le ?_ ?_· intro y hyrw [Finset.mem_image] at hyobtain ⟨b, hb, rfl⟩ := hyexact Finset.mem_range.mpr (Nat.mod_lt _ (Nat.pos_of_ne_zero hd))· rw [Finset.card_image_of_injOn hinj]calc ∑ b ∈ range d, G ((u * b) % d)= ∑ y ∈ (range d).image (fun b => (u * b) % d), G y := (Finset.sum_image hinj).symm_ = ∑ b ∈ range d, G b := by rw [himage]/-- Plancherel along a dilated frequency. -/theorem plancherel_unit {X : Finset ℕ} (hX : X.Nonempty) {d u : ℕ} (hd : d ≠ 0)(hu : Nat.Coprime u d) :∑ b ∈ range d, ‖Xhat X d (u * b)‖ ^ 2 = d * Mmom X d := byhave hstep : ∀ b : ℕ, ‖Xhat X d (u * b)‖ ^ 2 = ‖Xhat X d (((u * b) % d : ℕ) : ℤ)‖ ^ 2 := byintro bcongr 2refine Xhat_congr X ?_have hz : ((u : ℤ) * (b : ℤ)) = ((u * b : ℕ) : ℤ) := by push_cast; ringrw [hz]exact dvd_sub_iff_mod_eq.mpr (by simp)rw [Finset.sum_congr rfl (fun b _ => hstep b)]rw [sum_range_unit_perm hd hu (fun b => ‖Xhat X d b‖ ^ 2)]exact plancherel hX hd/-! ### The bilinear estimate -/theorem sum_divisors_mul_coprime {M : Type*} [AddCommMonoid M] {m n : ℕ} (hm : 0 < m) (hn : 0 < n)(h : Nat.Coprime m n) (f : ℕ → M) :∑ q ∈ m.divisors ×ˢ n.divisors, f (q.1 * q.2) = ∑ x ∈ (m * n).divisors, f x := byrefine Finset.sum_nbij' (i := fun q => q.1 * q.2) (j := fun x => (Nat.gcd x m, Nat.gcd x n))?_ ?_ ?_ ?_ ?_· rintro ⟨a, b⟩ habsimp only [Finset.mem_product, Nat.mem_divisors] at hab ⊢exact ⟨mul_dvd_mul hab.1.1 hab.2.1, by positivity⟩· intro x hxsimp only [Nat.mem_divisors, Finset.mem_product] at hx ⊢exact ⟨⟨Nat.gcd_dvd_right x m, hm.ne'⟩, ⟨Nat.gcd_dvd_right x n, hn.ne'⟩⟩· rintro ⟨a, b⟩ habsimp only [Finset.mem_product, Nat.mem_divisors] at habhave h1 : Nat.gcd (a * b) m = a := byhave hb : Nat.Coprime b m := Nat.Coprime.coprime_dvd_left hab.2.1 h.symmrw [Nat.Coprime.gcd_mul_right_cancel a hb]exact Nat.gcd_eq_left hab.1.1have h2 : Nat.gcd (a * b) n = b := byhave ha : Nat.Coprime a n := Nat.Coprime.coprime_dvd_left hab.1.1 hrw [Nat.Coprime.gcd_mul_left_cancel b ha]exact Nat.gcd_eq_left hab.2.1simp [h1, h2]· intro x hxsimp only [Nat.mem_divisors] at hxshow Nat.gcd x m * Nat.gcd x n = xrw [← Nat.Coprime.gcd_mul x h]exact Nat.gcd_eq_left hx.1· intro q hqrfltheorem Xhat_mod (X : Finset ℕ) (d xi a : ℕ) :Xhat X d ((xi : ℤ) * (a : ℤ)) = Xhat X d ((xi : ℤ) * ((a % d : ℕ) : ℤ)) := byrefine Xhat_congr X ?_have hrw : (xi:ℤ) * (a:ℤ) - (xi:ℤ) * ((a % d : ℕ):ℤ)= (xi:ℤ) * ((a:ℤ) - ((a % d : ℕ):ℤ)) := by ringrw [hrw]exact Dvd.dvd.mul_left (dvd_sub_iff_mod_eq.mpr (by simp)) _/-- **Bilinear estimate.** -/theorem bilinear {V₁ V₂ : ℕ} (hV₁ : 0 < V₁) (hV₂ : 0 < V₂) (hcop : Nat.Coprime V₁ V₂){d : ℕ} (hd : d ≠ 0) {xi : ℕ} (hxi : Nat.Coprime xi d) :‖Xhat (V₁ * V₂).divisors d xi‖ ^ 2≤ d * (Mmom V₁.divisors d * Mmom V₂.divisors d) := byclassicalset X₁ := V₁.divisors with hX₁set X₂ := V₂.divisors with hX₂have hX₁ne : X₁.Nonempty := ⟨1, Nat.one_mem_divisors.mpr hV₁.ne'⟩have hX₂ne : X₂.Nonempty := ⟨1, Nat.one_mem_divisors.mpr hV₂.ne'⟩have hT₁ : (0:ℝ) < (X₁.card : ℝ) := byhave := Finset.card_pos.mpr hX₁ne; exact_mod_cast thishave hT₂ : (0:ℝ) < (X₂.card : ℝ) := byhave := Finset.card_pos.mpr hX₂ne; exact_mod_cast thishave hT₁C : (X₁.card : ℂ) ≠ 0 := bysimpa using (Nat.cast_ne_zero (R := ℂ)).mpr (Finset.card_ne_zero.mpr hX₁ne)have hT₂C : (X₂.card : ℂ) ≠ 0 := bysimpa using (Nat.cast_ne_zero (R := ℂ)).mpr (Finset.card_ne_zero.mpr hX₂ne)-- the Fourier coefficient factors as an averagehave hcard : ((V₁ * V₂).divisors.card : ℂ) = (X₁.card : ℂ) * (X₂.card : ℂ) := byrw [hX₁, hX₂, ← Nat.cast_mul, Nat.Coprime.card_divisors_mul hcop]have h1 : Xhat (V₁ * V₂).divisors d xi= (X₁.card : ℂ)⁻¹ * ∑ a ∈ X₁, Xhat X₂ d ((xi : ℤ) * (a : ℤ)) := byunfold Xhatrw [hcard, ← sum_divisors_mul_coprime hV₁ hV₂ hcop (fun x => ee d ((xi:ℤ) * (x:ℤ))),Finset.sum_product, Finset.mul_sum, Finset.mul_sum]refine Finset.sum_congr rfl fun a ha => ?_rw [Finset.mul_sum, Finset.mul_sum, Finset.mul_sum]refine Finset.sum_congr rfl fun b hb => ?_have hee : ee d ((xi:ℤ) * ((a * b : ℕ):ℤ)) = ee d ((xi:ℤ) * (a:ℤ) * (b:ℤ)) := bycongr 1; push_cast; ringrw [hee, mul_inv]ring-- triangle inequality and grouping into residue classeshave h2 : ‖Xhat (V₁ * V₂).divisors d xi‖≤ ∑ b ∈ range d, ((resCount X₁ d b : ℝ) / X₁.card) * ‖Xhat X₂ d ((xi : ℤ) * (b : ℤ))‖ := byrw [h1, norm_mul, norm_inv, Complex.norm_natCast]have hstep : ‖∑ a ∈ X₁, Xhat X₂ d ((xi : ℤ) * (a : ℤ))‖≤ ∑ a ∈ X₁, ‖Xhat X₂ d ((xi : ℤ) * ((a % d : ℕ) : ℤ))‖ := byrefine le_trans (norm_sum_le _ _) (le_of_eq ?_)exact Finset.sum_congr rfl fun a _ => by rw [Xhat_mod]rw [← sum_resCount_smul X₁ hd (fun b => ‖Xhat X₂ d ((xi : ℤ) * (b : ℤ))‖)] at hstephave hrw : ∑ b ∈ range d, ((resCount X₁ d b : ℝ) / X₁.card)* ‖Xhat X₂ d ((xi : ℤ) * (b : ℤ))‖= (X₁.card : ℝ)⁻¹ * ∑ b ∈ range d,(resCount X₁ d b) • ‖Xhat X₂ d ((xi : ℤ) * (b : ℤ))‖ := byrw [Finset.mul_sum]refine Finset.sum_congr rfl fun b _ => ?_rw [nsmul_eq_mul, div_eq_inv_mul, mul_assoc]rw [hrw]exact mul_le_mul_of_nonneg_left hstep (by positivity)-- Cauchy-Schwarzhave hCS : (∑ b ∈ range d, ((resCount X₁ d b : ℝ) / X₁.card)* ‖Xhat X₂ d ((xi : ℤ) * (b : ℤ))‖) ^ 2≤ (∑ b ∈ range d, ((resCount X₁ d b : ℝ) / X₁.card) ^ 2)* ∑ b ∈ range d, ‖Xhat X₂ d ((xi : ℤ) * (b : ℤ))‖ ^ 2 :=Finset.sum_mul_sq_le_sq_mul_sq _ _ _have hM₁ : ∑ b ∈ range d, ((resCount X₁ d b : ℝ) / X₁.card) ^ 2 = Mmom X₁ d :=(Mmom_eq_sum_sq X₁ hd).symmhave hM₂ : ∑ b ∈ range d, ‖Xhat X₂ d ((xi : ℤ) * (b : ℤ))‖ ^ 2 = d * Mmom X₂ d := byhave := plancherel_unit hX₂ne hd hxisimpa using thishave hnn : 0 ≤ ∑ b ∈ range d, ((resCount X₁ d b : ℝ) / X₁.card)* ‖Xhat X₂ d ((xi : ℤ) * (b : ℤ))‖ :=Finset.sum_nonneg fun b _ => by positivitycalc ‖Xhat (V₁ * V₂).divisors d xi‖ ^ 2≤ (∑ b ∈ range d, ((resCount X₁ d b : ℝ) / X₁.card)* ‖Xhat X₂ d ((xi : ℤ) * (b : ℤ))‖) ^ 2 := byapply pow_le_pow_left₀ (norm_nonneg _) h2_ ≤ Mmom X₁ d * ((d : ℝ) * Mmom X₂ d) := by rw [← hM₁, ← hM₂]; exact hCS_ = d * (Mmom X₁ d * Mmom X₂ d) := by ring/-! ### Four factors at one modulus -/theorem bilinear' {V₁ V₂ : ℕ} (hV₁ : 0 < V₁) (hV₂ : 0 < V₂) (hcop : Nat.Coprime V₁ V₂){d : ℕ} (hd : d ≠ 0) {m xi : ℕ} (hxi : Nat.Coprime (m * xi) d) :‖Xhat (V₁ * V₂).divisors d ((m : ℤ) * (xi : ℤ))‖ ^ 2≤ d * (Mmom V₁.divisors d * Mmom V₂.divisors d) := byhave h := bilinear hV₁ hV₂ hcop hd hxihave hcast : ((m * xi : ℕ) : ℤ) = (m : ℤ) * (xi : ℤ) := by push_cast; ringrwa [hcast] at h/-- **Four factors at one modulus.** -/theorem four_factors {V₁ V₂ : ℕ} (hV₁ : 0 < V₁) (hV₂ : 0 < V₂) (hcop : Nat.Coprime V₁ V₂){d : ℕ} (hd1 : 1 < d) (hdodd : Odd d) :∑ xi ∈ (range d).filter (fun xi => Nat.Coprime xi d),‖Xhat (V₁ * V₂).divisors d ((1:ℤ) * (xi:ℤ))‖* ‖Xhat (V₁ * V₂).divisors d ((2:ℤ) * (xi:ℤ))‖* (‖Xhat (V₁ * V₂).divisors d ((4:ℤ) * (xi:ℤ))‖* ‖Xhat (V₁ * V₂).divisors d ((8:ℤ) * (xi:ℤ))‖)≤ (d:ℝ) ^ 2 * (Mmom V₁.divisors d * Mmom V₂.divisors d* Mmom (V₁ * V₂).divisors d) := byclassicalhave hd : d ≠ 0 := by omegaset X := (V₁ * V₂).divisors with hXhave hXne : X.Nonempty := ⟨1, Nat.one_mem_divisors.mpr (by positivity)⟩set M₁ := Mmom V₁.divisors d with hM₁set M₂ := Mmom V₂.divisors d with hM₂set M := Mmom X d with hMhave hM₁0 : 0 ≤ M₁ := Mmom_nonneg _ _have hM₂0 : 0 ≤ M₂ := Mmom_nonneg _ _have hM0 : 0 ≤ M := Mmom_nonneg _ _have hd0 : (0:ℝ) ≤ (d:ℝ) := by positivityhave h2cop : Nat.Coprime 2 d := Nat.coprime_two_left.mpr hdodd-- the first two factors are uniformly boundedhave hpair : ∀ xi ∈ (range d).filter (fun xi => Nat.Coprime xi d),‖Xhat X d ((1:ℤ) * (xi:ℤ))‖ * ‖Xhat X d ((2:ℤ) * (xi:ℤ))‖ ≤ (d:ℝ) * (M₁ * M₂) := byintro xi hxihave hxic : Nat.Coprime xi d := (Finset.mem_filter.mp hxi).2have hb1 : ‖Xhat X d ((1:ℤ) * (xi:ℤ))‖ ^ 2 ≤ (d:ℝ) * (M₁ * M₂) :=bilinear' hV₁ hV₂ hcop hd (m := 1) (by simpa using hxic)have hb2 : ‖Xhat X d ((2:ℤ) * (xi:ℤ))‖ ^ 2 ≤ (d:ℝ) * (M₁ * M₂) :=bilinear' hV₁ hV₂ hcop hd (m := 2) (Nat.Coprime.mul_left h2cop hxic)have hnn1 : (0:ℝ) ≤ ‖Xhat X d ((1:ℤ) * (xi:ℤ))‖ := norm_nonneg _have hnn2 : (0:ℝ) ≤ ‖Xhat X d ((2:ℤ) * (xi:ℤ))‖ := norm_nonneg _nlinarith [sq_nonneg (‖Xhat X d ((1:ℤ) * (xi:ℤ))‖ - ‖Xhat X d ((2:ℤ) * (xi:ℤ))‖)]-- Cauchy-Schwarz for the last two factorshave hCS : ∑ xi ∈ range d, ‖Xhat X d ((4:ℤ) * (xi:ℤ))‖ * ‖Xhat X d ((8:ℤ) * (xi:ℤ))‖≤ (d:ℝ) * M := byhave h1 := Finset.sum_mul_sq_le_sq_mul_sq (range d)(fun xi => ‖Xhat X d ((4:ℤ) * (xi:ℤ))‖) (fun xi => ‖Xhat X d ((8:ℤ) * (xi:ℤ))‖)have h4 : ∑ xi ∈ range d, ‖Xhat X d ((4:ℤ) * (xi:ℤ))‖ ^ 2 = (d:ℝ) * M := byhave := plancherel_unit (X := X) hXne hd (u := 4) (Nat.Coprime.mul_left h2cop h2cop)simpa using thishave h8 : ∑ xi ∈ range d, ‖Xhat X d ((8:ℤ) * (xi:ℤ))‖ ^ 2 = (d:ℝ) * M := byhave := plancherel_unit (X := X) hXne hd (u := 8)(Nat.Coprime.mul_left h2cop (Nat.Coprime.mul_left h2cop h2cop))simpa using thisrw [h4, h8] at h1have hnn : 0 ≤ ∑ xi ∈ range d, ‖Xhat X d ((4:ℤ) * (xi:ℤ))‖ * ‖Xhat X d ((8:ℤ) * (xi:ℤ))‖ :=Finset.sum_nonneg fun xi _ => by positivitynlinarith [mul_nonneg hd0 hM0]calc ∑ xi ∈ (range d).filter (fun xi => Nat.Coprime xi d),‖Xhat X d ((1:ℤ) * (xi:ℤ))‖ * ‖Xhat X d ((2:ℤ) * (xi:ℤ))‖* (‖Xhat X d ((4:ℤ) * (xi:ℤ))‖ * ‖Xhat X d ((8:ℤ) * (xi:ℤ))‖)≤ ∑ xi ∈ (range d).filter (fun xi => Nat.Coprime xi d),((d:ℝ) * (M₁ * M₂)) * (‖Xhat X d ((4:ℤ) * (xi:ℤ))‖ * ‖Xhat X d ((8:ℤ) * (xi:ℤ))‖) := byrefine Finset.sum_le_sum fun xi hxi => ?_exact mul_le_mul_of_nonneg_right (hpair xi hxi) (by positivity)_ = ((d:ℝ) * (M₁ * M₂)) * ∑ xi ∈ (range d).filter (fun xi => Nat.Coprime xi d),(‖Xhat X d ((4:ℤ) * (xi:ℤ))‖ * ‖Xhat X d ((8:ℤ) * (xi:ℤ))‖) := byrw [Finset.mul_sum]_ ≤ ((d:ℝ) * (M₁ * M₂)) * ∑ xi ∈ range d,(‖Xhat X d ((4:ℤ) * (xi:ℤ))‖ * ‖Xhat X d ((8:ℤ) * (xi:ℤ))‖) := byrefine mul_le_mul_of_nonneg_left ?_ (by positivity)exact Finset.sum_le_sum_of_subset_of_nonneg (Finset.filter_subset _ _)(fun i _ _ => by positivity)_ ≤ ((d:ℝ) * (M₁ * M₂)) * ((d:ℝ) * M) := byexact mul_le_mul_of_nonneg_left hCS (by positivity)_ = (d:ℝ) ^ 2 * (M₁ * M₂ * M) := by ring/-! ### Counting four-term representations -//-- The number of four-term representations of `c` modulo `A` by elements of `X`. -/def Ncount (X : Finset ℕ) (A c : ℕ) : ℕ :=((X ×ˢ X ×ˢ X ×ˢ X).filter(fun z => (z.1 + 2 * z.2.1 + 4 * z.2.2.1 + 8 * z.2.2.2) % A = c % A)).cardtheorem sum_mul_sum_product {R α β : Type*} [CommRing R] (s : Finset α) (t : Finset β)(f : α → R) (g : β → R) :(∑ i ∈ s, f i) * (∑ j ∈ t, g j) = ∑ q ∈ s ×ˢ t, f q.1 * g q.2 := byrw [Finset.sum_product, Finset.sum_mul_sum]theorem sum_prod4 {R : Type*} [CommRing R] (X : Finset ℕ) (f₀ f₁ f₂ f₃ : ℕ → R) :(∑ z ∈ X, f₀ z) * (∑ z ∈ X, f₁ z) * (∑ z ∈ X, f₂ z) * (∑ z ∈ X, f₃ z)= ∑ q ∈ X ×ˢ X ×ˢ X ×ˢ X, f₀ q.1 * f₁ q.2.1 * f₂ q.2.2.1 * f₃ q.2.2.2 := bycalc (∑ z ∈ X, f₀ z) * (∑ z ∈ X, f₁ z) * (∑ z ∈ X, f₂ z) * (∑ z ∈ X, f₃ z)= (∑ z ∈ X, f₀ z) * ((∑ z ∈ X, f₁ z) * ((∑ z ∈ X, f₂ z) * (∑ z ∈ X, f₃ z))) := by ring_ = (∑ z ∈ X, f₀ z) * ((∑ z ∈ X, f₁ z) * ∑ r ∈ X ×ˢ X, f₂ r.1 * f₃ r.2) := byrw [sum_mul_sum_product]_ = (∑ z ∈ X, f₀ z) * ∑ p ∈ X ×ˢ X ×ˢ X, f₁ p.1 * (f₂ p.2.1 * f₃ p.2.2) := byrw [sum_mul_sum_product]_ = ∑ q ∈ X ×ˢ X ×ˢ X ×ˢ X, f₀ q.1 * (f₁ q.2.1 * (f₂ q.2.2.1 * f₃ q.2.2.2)) := byrw [sum_mul_sum_product]_ = ∑ q ∈ X ×ˢ X ×ˢ X ×ˢ X, f₀ q.1 * f₁ q.2.1 * f₂ q.2.2.1 * f₃ q.2.2.2 :=Finset.sum_congr rfl fun q _ => by ring/-- **Counting four-term representations.** -/theorem four_count {X : Finset ℕ} (hX : X.Nonempty) {A : ℕ} (hA : A ≠ 0) (c : ℕ) :∑ h ∈ range A, ee A (-((h : ℤ) * (c : ℤ)))* (Xhat X A ((1:ℤ) * (h:ℤ)) * Xhat X A ((2:ℤ) * (h:ℤ))* Xhat X A ((4:ℤ) * (h:ℤ)) * Xhat X A ((8:ℤ) * (h:ℤ)))= (A : ℂ) * (Ncount X A c : ℂ) / (X.card : ℂ) ^ 4 := byclassicalhave hT : (X.card : ℂ) ≠ 0 := bysimpa using (Nat.cast_ne_zero (R := ℂ)).mpr (Finset.card_ne_zero.mpr hX)have hterm : ∀ h ∈ range A, ee A (-((h : ℤ) * (c : ℤ)))* (Xhat X A ((1:ℤ) * (h:ℤ)) * Xhat X A ((2:ℤ) * (h:ℤ))* Xhat X A ((4:ℤ) * (h:ℤ)) * Xhat X A ((8:ℤ) * (h:ℤ)))= ((X.card : ℂ)⁻¹) ^ 4 * ∑ q ∈ X ×ˢ X ×ˢ X ×ˢ X,ee A ((h:ℤ) * (((q.1 + 2 * q.2.1 + 4 * q.2.2.1 + 8 * q.2.2.2 : ℕ) : ℤ) - (c:ℤ))) := byintro h _unfold Xhathave hS : (∑ z ∈ X, ee A ((1:ℤ) * (h:ℤ) * (z:ℤ))) * (∑ z ∈ X, ee A ((2:ℤ) * (h:ℤ) * (z:ℤ)))* (∑ z ∈ X, ee A ((4:ℤ) * (h:ℤ) * (z:ℤ))) * (∑ z ∈ X, ee A ((8:ℤ) * (h:ℤ) * (z:ℤ)))= ∑ q ∈ X ×ˢ X ×ˢ X ×ˢ X,ee A ((h:ℤ) * ((q.1 + 2 * q.2.1 + 4 * q.2.2.1 + 8 * q.2.2.2 : ℕ) : ℤ)) := byrw [sum_prod4]refine Finset.sum_congr rfl fun q _ => ?_rw [← ee_add, ← ee_add, ← ee_add]congr 1push_castringcalc ee A (-((h : ℤ) * (c : ℤ)))* (((X.card : ℂ)⁻¹ * ∑ z ∈ X, ee A ((1:ℤ) * (h:ℤ) * (z:ℤ)))* ((X.card : ℂ)⁻¹ * ∑ z ∈ X, ee A ((2:ℤ) * (h:ℤ) * (z:ℤ)))* ((X.card : ℂ)⁻¹ * ∑ z ∈ X, ee A ((4:ℤ) * (h:ℤ) * (z:ℤ)))* ((X.card : ℂ)⁻¹ * ∑ z ∈ X, ee A ((8:ℤ) * (h:ℤ) * (z:ℤ))))= ((X.card : ℂ)⁻¹) ^ 4 * (ee A (-((h : ℤ) * (c : ℤ)))* ((∑ z ∈ X, ee A ((1:ℤ) * (h:ℤ) * (z:ℤ)))* (∑ z ∈ X, ee A ((2:ℤ) * (h:ℤ) * (z:ℤ)))* (∑ z ∈ X, ee A ((4:ℤ) * (h:ℤ) * (z:ℤ)))* (∑ z ∈ X, ee A ((8:ℤ) * (h:ℤ) * (z:ℤ))))) := by ring_ = ((X.card : ℂ)⁻¹) ^ 4 * ∑ q ∈ X ×ˢ X ×ˢ X ×ˢ X,ee A ((h:ℤ) * (((q.1 + 2 * q.2.1 + 4 * q.2.2.1 + 8 * q.2.2.2 : ℕ) : ℤ) - (c:ℤ))) := byrw [hS, Finset.mul_sum]congr 1refine Finset.sum_congr rfl fun q _ => ?_rw [← ee_add]congr 1ringrw [Finset.sum_congr rfl hterm, ← Finset.mul_sum, Finset.sum_comm]have hinner : ∀ q ∈ X ×ˢ X ×ˢ X ×ˢ X,(∑ h ∈ range A,ee A ((h:ℤ) * (((q.1 + 2 * q.2.1 + 4 * q.2.2.1 + 8 * q.2.2.2 : ℕ) : ℤ) - (c:ℤ))))= if (A:ℤ) ∣ (((q.1 + 2 * q.2.1 + 4 * q.2.2.1 + 8 * q.2.2.2 : ℕ) : ℤ) - (c:ℤ))then (A:ℂ) else 0 := fun q _ => sum_ee hA _rw [Finset.sum_congr rfl hinner]have hfilter : ((X ×ˢ X ×ˢ X ×ˢ X).filter(fun q => (A:ℤ) ∣ (((q.1 + 2 * q.2.1 + 4 * q.2.2.1 + 8 * q.2.2.2 : ℕ) : ℤ) - (c:ℤ))))= ((X ×ˢ X ×ˢ X ×ˢ X).filter(fun z => (z.1 + 2 * z.2.1 + 4 * z.2.2.1 + 8 * z.2.2.2) % A = c % A)) := byrefine Finset.filter_congr fun q _ => ?_constructor· intro h; exact dvd_sub_iff_mod_eq.mp h· intro h; exact dvd_sub_iff_mod_eq.mpr hrw [Finset.sum_ite, Finset.sum_const, Finset.sum_const_zero, hfilter, ← Ncount]field_simpring/-! ### Grouping the frequencies by their conductor -/theorem ee_conductor {A d g : ℕ} (hg : g ≠ 0) (hd : d ≠ 0) (hA : A = g * d) (z : ℤ) :ee A ((g : ℤ) * z) = ee d z := byhave hzet : zet A ^ (g : ℕ) = zet d := byrw [zet, zet, ← Complex.exp_nat_mul]congr 1have hg0 : (g : ℂ) ≠ 0 := Nat.cast_ne_zero.mpr hghave hd0 : (d : ℂ) ≠ 0 := Nat.cast_ne_zero.mpr hdrw [hA]push_castfield_simprw [ee, ee, ← hzet, ← zpow_natCast (zet A) g, ← zpow_mul]theorem Xhat_conductor (X : Finset ℕ) {A d g : ℕ} (hg : g ≠ 0) (hd : d ≠ 0) (hA : A = g * d)(m : ℤ) (xi : ℕ) : Xhat X A (m * ((g * xi : ℕ) : ℤ)) = Xhat X d (m * (xi:ℤ)) := byunfold Xhatcongr 1refine Finset.sum_congr rfl fun x hx => ?_have hrw : m * ((g * xi : ℕ):ℤ) * (x:ℤ) = (g:ℤ) * (m * (xi:ℤ) * (x:ℤ)) := bypush_cast; ringrw [hrw, ee_conductor hg hd hA]/-- The four-factor sum over all nonzero frequencies, grouped by conductor. -/theorem sum_by_conductor {V₁ V₂ : ℕ} (hV₁ : 0 < V₁) (hV₂ : 0 < V₂) (hcop : Nat.Coprime V₁ V₂){A : ℕ} (hA1 : 1 < A) (hAodd : Odd A) :∑ h ∈ (range A).erase 0,(‖Xhat (V₁ * V₂).divisors A ((1:ℤ) * (h:ℤ))‖* ‖Xhat (V₁ * V₂).divisors A ((2:ℤ) * (h:ℤ))‖* (‖Xhat (V₁ * V₂).divisors A ((4:ℤ) * (h:ℤ))‖* ‖Xhat (V₁ * V₂).divisors A ((8:ℤ) * (h:ℤ))‖))≤ ∑ d ∈ A.divisors.filter (fun d => 1 < d),(d:ℝ) ^ 2 * (Mmom V₁.divisors d * Mmom V₂.divisors d* Mmom (V₁ * V₂).divisors d) := byclassicalhave hA0 : A ≠ 0 := by omegaset X := (V₁ * V₂).divisors with hXdefset F : ℕ → ℝ := fun h => ‖Xhat X A ((1:ℤ) * (h:ℤ))‖ * ‖Xhat X A ((2:ℤ) * (h:ℤ))‖* (‖Xhat X A ((4:ℤ) * (h:ℤ))‖ * ‖Xhat X A ((8:ℤ) * (h:ℤ))‖) with hFhave hmaps : ∀ h ∈ (range A).erase 0, A / Nat.gcd A h ∈ A.divisors.filter (fun d => 1 < d) := byintro h hhhave hh0 : h ≠ 0 := (Finset.mem_erase.mp hh).1have hhA : h < A := Finset.mem_range.mp (Finset.mem_erase.mp hh).2have hgcddvd : Nat.gcd A h ∣ A := Nat.gcd_dvd_left A hhave hgcdpos : 0 < Nat.gcd A h := Nat.gcd_pos_of_pos_left _ (by omega)have hgcdle : Nat.gcd A h ≤ h := Nat.le_of_dvd (by omega) (Nat.gcd_dvd_right A h)refine Finset.mem_filter.mpr ⟨Nat.mem_divisors.mpr ⟨Nat.div_dvd_of_dvd hgcddvd, hA0⟩, ?_⟩by_contra hconpush_neg at hconhave heq : A / Nat.gcd A h * Nat.gcd A h = A := Nat.div_mul_cancel hgcddvdhave hle : A ≤ Nat.gcd A h := bycalc A = A / Nat.gcd A h * Nat.gcd A h := heq.symm_ ≤ 1 * Nat.gcd A h := Nat.mul_le_mul_right _ hcon_ = Nat.gcd A h := one_mul _omegarw [← Finset.sum_fiberwise_of_maps_to hmaps F]refine Finset.sum_le_sum fun d hd => ?_have hdmem := Finset.mem_filter.mp hdhave hdA : d ∣ A := (Nat.mem_divisors.mp hdmem.1).1have hd1 : 1 < d := hdmem.2have hd0 : d ≠ 0 := by omegahave hdodd : Odd d := byobtain ⟨k, hk⟩ := hdArw [hk, Nat.odd_mul] at hAoddexact hAodd.1set g := A / d with hgdefhave hgd : A = g * d := byrw [hgdef]exact (Nat.div_mul_cancel hdA).symmhave hg0 : g ≠ 0 := byintro h0rw [h0, zero_mul] at hgdomega-- every frequency in the fibre is `g` times a reduced frequencyhave hfib : ∀ h ∈ (range A).erase 0, A / Nat.gcd A h = d →(h / g ∈ (range d).filter (fun xi => Nat.Coprime xi d) ∧ h = g * (h / g)) := byintro h hh hgchave hh0 : h ≠ 0 := (Finset.mem_erase.mp hh).1have hhA : h < A := Finset.mem_range.mp (Finset.mem_erase.mp hh).2have hgcddvd : Nat.gcd A h ∣ A := Nat.gcd_dvd_left A hhave hgcdpos : 0 < Nat.gcd A h := Nat.gcd_pos_of_pos_left _ (by omega)have hgg : Nat.gcd A h = g := byrw [hgdef, ← hgc]exact (Nat.div_div_self hgcddvd hA0).symmhave hdvdh : g ∣ h := hgg ▸ Nat.gcd_dvd_right A hhave hhg : h = g * (h / g) := (Nat.mul_div_cancel' hdvdh).symmrefine ⟨Finset.mem_filter.mpr ⟨Finset.mem_range.mpr ?_, ?_⟩, hhg⟩· have hlt : g * (h / g) < g * d := by rw [← hhg, ← hgd]; exact hhAexact lt_of_mul_lt_mul_left hlt (Nat.zero_le g)· have hcopd := Nat.coprime_div_gcd_div_gcd (m := A) (n := h) hgcdposrw [hgc, hgg] at hcopdexact hcopd.symm-- the injection into the reduced frequencieshave hinj : Set.InjOn (fun h => h / g)(((range A).erase 0).filter (fun h => A / Nat.gcd A h = d) : Finset ℕ) := byintro x hx y hy hxysimp only [Finset.coe_filter, Set.mem_setOf_eq] at hx hyhave hx' := hfib x hx.1 hx.2have hy' := hfib y hy.1 hy.2have hxy' : x / g = y / g := hxyrw [hx'.2, hy'.2, hxy']have hterm : ∀ h ∈ ((range A).erase 0).filter (fun h => A / Nat.gcd A h = d),F h = ‖Xhat X d ((1:ℤ) * ((h / g : ℕ):ℤ))‖ * ‖Xhat X d ((2:ℤ) * ((h / g : ℕ):ℤ))‖* (‖Xhat X d ((4:ℤ) * ((h / g : ℕ):ℤ))‖ * ‖Xhat X d ((8:ℤ) * ((h / g : ℕ):ℤ))‖) := byintro h hhhave hmem := Finset.mem_filter.mp hhobtain ⟨-, hhg⟩ := hfib h hmem.1 hmem.2have hconv : ∀ m : ℤ, Xhat X A (m * (h:ℤ)) = Xhat X d (m * ((h / g : ℕ):ℤ)) := byintro mhave hcast : ((h : ℕ) : ℤ) = ((g * (h / g) : ℕ) : ℤ) := by rw [← hhg]rw [hcast, Xhat_conductor X hg0 hd0 hgd]simp only [hF]rw [hconv 1, hconv 2, hconv 4, hconv 8]calc ∑ h ∈ ((range A).erase 0).filter (fun h => A / Nat.gcd A h = d), F h= ∑ h ∈ ((range A).erase 0).filter (fun h => A / Nat.gcd A h = d),(‖Xhat X d ((1:ℤ) * ((h / g : ℕ):ℤ))‖ * ‖Xhat X d ((2:ℤ) * ((h / g : ℕ):ℤ))‖* (‖Xhat X d ((4:ℤ) * ((h / g : ℕ):ℤ))‖* ‖Xhat X d ((8:ℤ) * ((h / g : ℕ):ℤ))‖)) := Finset.sum_congr rfl hterm_ = ∑ xi ∈ (((range A).erase 0).filter (fun h => A / Nat.gcd A h = d)).image(fun h => h / g),(‖Xhat X d ((1:ℤ) * (xi:ℤ))‖ * ‖Xhat X d ((2:ℤ) * (xi:ℤ))‖* (‖Xhat X d ((4:ℤ) * (xi:ℤ))‖ * ‖Xhat X d ((8:ℤ) * (xi:ℤ))‖)) := byrw [Finset.sum_image hinj]_ ≤ ∑ xi ∈ (range d).filter (fun xi => Nat.Coprime xi d),(‖Xhat X d ((1:ℤ) * (xi:ℤ))‖ * ‖Xhat X d ((2:ℤ) * (xi:ℤ))‖* (‖Xhat X d ((4:ℤ) * (xi:ℤ))‖ * ‖Xhat X d ((8:ℤ) * (xi:ℤ))‖)) := byrefine Finset.sum_le_sum_of_subset_of_nonneg ?_ (fun i _ _ => by positivity)intro xi hxirw [Finset.mem_image] at hxiobtain ⟨h, hh, rfl⟩ := hxiexact (hfib h (Finset.mem_filter.mp hh).1 (Finset.mem_filter.mp hh).2).1_ ≤ (d:ℝ) ^ 2 * (Mmom V₁.divisors d * Mmom V₂.divisors d * Mmom X d) :=four_factors hV₁ hV₂ hcop hd1 hdodd/-! ### The moment condition -/theorem cube_rpow {x y : ℝ} (hx : 0 ≤ x) (hy : 0 ≤ y) :(x ^ ((2:ℝ)/3) * y ^ ((1:ℝ)/3)) ^ (3:ℕ) = x ^ (2:ℕ) * y := byrw [mul_pow, ← Real.rpow_natCast (x ^ ((2:ℝ)/3)) 3, ← Real.rpow_natCast (y ^ ((1:ℝ)/3)) 3,← Real.rpow_mul hx, ← Real.rpow_mul hy]norm_numtheorem sum_cube_le {s : Finset ℕ} {a : ℕ → ℝ} (ha : ∀ i ∈ s, 0 ≤ a i) :∑ i ∈ s, (a i) ^ (3:ℕ) ≤ (∑ i ∈ s, a i) ^ (3:ℕ) := byset S := ∑ i ∈ s, a i with hShave hS0 : 0 ≤ S := Finset.sum_nonneg hahave h1 : ∀ i ∈ s, (a i) ^ (3:ℕ) ≤ a i * S ^ 2 := byintro i hihave hai : a i ≤ S := Finset.single_le_sum ha hihave h2 : 0 ≤ a i * (S - a i) * (S + a i) :=mul_nonneg (mul_nonneg (ha i hi) (by linarith)) (by linarith [ha i hi])nlinarithcalc ∑ i ∈ s, (a i) ^ (3:ℕ) ≤ ∑ i ∈ s, a i * S ^ 2 := Finset.sum_le_sum h1_ = S * S ^ 2 := by rw [← Finset.sum_mul]_ = S ^ (3:ℕ) := by ringtheorem Xhat_zero {X : Finset ℕ} (hX : X.Nonempty) (d : ℕ) : Xhat X d 0 = 1 := byhave hT : (X.card : ℂ) ≠ 0 := bysimpa using (Nat.cast_ne_zero (R := ℂ)).mpr (Finset.card_ne_zero.mpr hX)unfold Xhathave hone : ∀ x ∈ X, ee d ((0:ℤ) * (x:ℤ)) = 1 := byintro x _simp [ee]rw [Finset.sum_congr rfl hone, Finset.sum_const, nsmul_eq_mul, mul_one, inv_mul_cancel₀ hT]/-- **A sufficient moment condition.** -/theorem coverage {V₁ V₂ : ℕ} (hV₁ : 0 < V₁) (hV₂ : 0 < V₂) (hcop : Nat.Coprime V₁ V₂){A : ℕ} (hA1 : 1 < A) (hAodd : Odd A)(hS : ∑ d ∈ A.divisors.filter (fun d => 1 < d),(d:ℝ) ^ ((2:ℝ)/3) * (Mmom V₁.divisors d * Mmom V₂.divisors d* Mmom (V₁ * V₂).divisors d) ^ ((1:ℝ)/3) < 1) (c : ℕ) :0 < Ncount (V₁ * V₂).divisors A c := byclassicalset X := (V₁ * V₂).divisors with hXdefhave hXne : X.Nonempty := ⟨1, Nat.one_mem_divisors.mpr (by positivity)⟩have hA0 : A ≠ 0 := by omegaby_contra hconpush_neg at hconhave hN0 : Ncount X A c = 0 := Nat.le_zero.mp hconhave hid := four_count hXne hA0 crw [hN0] at hidsimp only [Nat.cast_zero, mul_zero, zero_div] at hidset G : ℕ → ℂ := fun h => ee A (-((h : ℤ) * (c : ℤ)))* (Xhat X A ((1:ℤ) * (h:ℤ)) * Xhat X A ((2:ℤ) * (h:ℤ))* Xhat X A ((4:ℤ) * (h:ℤ)) * Xhat X A ((8:ℤ) * (h:ℤ))) with hGhave h0mem : (0:ℕ) ∈ range A := Finset.mem_range.mpr (by omega)have hsplit : ∑ h ∈ (range A).erase 0, G h + G 0 = ∑ h ∈ range A, G h :=Finset.sum_erase_add _ _ h0memhave hG0 : G 0 = 1 := bysimp only [hG]rw [show ((0:ℕ) : ℤ) = 0 by norm_num]simp only [mul_zero, zero_mul, neg_zero]rw [show ee A 0 = 1 by simp [ee], Xhat_zero hXne]ringrw [hG0, hid] at hsplithave hnorm1 : ‖∑ h ∈ (range A).erase 0, G h‖ = 1 := byhave : ∑ h ∈ (range A).erase 0, G h = -1 := by linear_combination (norm := ring_nf) hsplitrw [this]simp-- the triangle inequalityhave htri : (1:ℝ) ≤ ∑ h ∈ (range A).erase 0,(‖Xhat X A ((1:ℤ) * (h:ℤ))‖ * ‖Xhat X A ((2:ℤ) * (h:ℤ))‖* (‖Xhat X A ((4:ℤ) * (h:ℤ))‖ * ‖Xhat X A ((8:ℤ) * (h:ℤ))‖)) := byhave h1 : ‖∑ h ∈ (range A).erase 0, G h‖ ≤ ∑ h ∈ (range A).erase 0, ‖G h‖ :=norm_sum_le _ _have h2 : ∀ h : ℕ, ‖G h‖ = ‖Xhat X A ((1:ℤ) * (h:ℤ))‖ * ‖Xhat X A ((2:ℤ) * (h:ℤ))‖* (‖Xhat X A ((4:ℤ) * (h:ℤ))‖ * ‖Xhat X A ((8:ℤ) * (h:ℤ))‖) := byintro hsimp only [hG, norm_mul, ee_norm, one_mul]ringrw [hnorm1] at h1calc (1:ℝ) ≤ ∑ h ∈ (range A).erase 0, ‖G h‖ := h1_ = _ := Finset.sum_congr rfl fun h _ => h2 h-- the conductor boundhave hgroup := sum_by_conductor hV₁ hV₂ hcop hA1 hAodd-- the moment conditionhave hcube : ∑ d ∈ A.divisors.filter (fun d => 1 < d),(d:ℝ) ^ (2:ℕ) * (Mmom V₁.divisors d * Mmom V₂.divisors d * Mmom X d)≤ (∑ d ∈ A.divisors.filter (fun d => 1 < d),(d:ℝ) ^ ((2:ℝ)/3) * (Mmom V₁.divisors d * Mmom V₂.divisors d* Mmom X d) ^ ((1:ℝ)/3)) ^ (3:ℕ) := byhave hterm : ∀ d ∈ A.divisors.filter (fun d => 1 < d),(d:ℝ) ^ (2:ℕ) * (Mmom V₁.divisors d * Mmom V₂.divisors d * Mmom X d)= ((d:ℝ) ^ ((2:ℝ)/3) * (Mmom V₁.divisors d * Mmom V₂.divisors d* Mmom X d) ^ ((1:ℝ)/3)) ^ (3:ℕ) := byintro d _rw [cube_rpow (by positivity)(mul_nonneg (mul_nonneg (Mmom_nonneg _ _) (Mmom_nonneg _ _)) (Mmom_nonneg _ _))]rw [Finset.sum_congr rfl hterm]refine sum_cube_le (fun d _ => ?_)exact mul_nonneg (Real.rpow_nonneg (by positivity) _)(Real.rpow_nonneg (mul_nonneg (mul_nonneg (Mmom_nonneg _ _) (Mmom_nonneg _ _))(Mmom_nonneg _ _)) _)have hlt : (∑ d ∈ A.divisors.filter (fun d => 1 < d),(d:ℝ) ^ ((2:ℝ)/3) * (Mmom V₁.divisors d * Mmom V₂.divisors d* Mmom X d) ^ ((1:ℝ)/3)) ^ (3:ℕ) < 1 := byhave hnn : 0 ≤ ∑ d ∈ A.divisors.filter (fun d => 1 < d),(d:ℝ) ^ ((2:ℝ)/3) * (Mmom V₁.divisors d * Mmom V₂.divisors d* Mmom X d) ^ ((1:ℝ)/3) := Finset.sum_nonneg fun d _ =>mul_nonneg (Real.rpow_nonneg (by positivity) _)(Real.rpow_nonneg (mul_nonneg (mul_nonneg (Mmom_nonneg _ _) (Mmom_nonneg _ _))(Mmom_nonneg _ _)) _)exact pow_lt_one₀ hnn hS (by norm_num)have hsq : ∑ d ∈ A.divisors.filter (fun d => 1 < d),(d:ℝ) ^ 2 * (Mmom V₁.divisors d * Mmom V₂.divisors d * Mmom X d)= ∑ d ∈ A.divisors.filter (fun d => 1 < d),(d:ℝ) ^ (2:ℕ) * (Mmom V₁.divisors d * Mmom V₂.divisors d * Mmom X d) := rflrw [hsq] at hgrouplinarith [htri, hgroup, hcube, hlt]/-! ## Finite averagingThe combinatorial heart of the selection argument. We average the moment quantity`∑_{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`obtained from `t`-element subsets `I` of a fixed set `P` of primes, and deduce from anumerical inequality that some modulus satisfies the moment condition of `coverage`.-//-! ### Products of distinct primes -/theorem squarefree_prod_primes {s : Finset ℕ} (hs : ∀ p ∈ s, p.Prime) :Squarefree (∏ p ∈ s, p) := byclassicalinduction s using Finset.induction with| empty => simp| insert a s ha ih =>rw [Finset.prod_insert ha]have hap := hs a (Finset.mem_insert_self a s)have hsq := ih (fun p hp => hs p (Finset.mem_insert_of_mem hp))refine (Nat.squarefree_mul_iff).mpr ⟨?_, hap.squarefree, hsq⟩have hnd : ¬ (a ∣ ∏ p ∈ s, p) := byintro hdvdobtain ⟨p, hp, hpa⟩ := Prime.exists_mem_finset_dvd hap.prime hdvdhave := (Nat.prime_dvd_prime_iff_eq hap (hs p (Finset.mem_insert_of_mem hp))).mp hpaexact ha (this ▸ hp)exact (Nat.Prime.coprime_iff_not_dvd hap).mpr hnd/-- The divisors of a product of distinct primes are exactly the subproducts. -/theorem sum_divisors_prod_primes {I : Finset ℕ} (hI : ∀ p ∈ I, p.Prime) (g : ℕ → ℝ) :∑ d ∈ (∏ p ∈ I, p).divisors, g d = ∑ J ∈ I.powerset, g (∏ p ∈ J, p) := byclassicalhave hsq : Squarefree (∏ p ∈ I, p) := squarefree_prod_primes hIhave hA0 : (∏ p ∈ I, p) ≠ 0 := hsq.ne_zerohave hpf : (∏ p ∈ I, p).primeFactors = I := Nat.primeFactors_prod hIrefine (Finset.sum_nbij' (i := fun J => ∏ p ∈ J, p) (j := fun d => d.primeFactors)?_ ?_ ?_ ?_ ?_).symm· intro J hJrw [Finset.mem_powerset] at hJexact Nat.mem_divisors.mpr ⟨Finset.prod_dvd_prod_of_subset _ _ _ hJ, hA0⟩· intro d hdrw [Nat.mem_divisors] at hdrw [Finset.mem_powerset, ← hpf]exact Nat.primeFactors_mono hd.1 hA0· intro J hJrw [Finset.mem_powerset] at hJexact Nat.primeFactors_prod (fun p hp => hI p (hJ hp))· intro d hdrw [Nat.mem_divisors] at hdexact Nat.prod_primeFactors_of_squarefree (hsq.squarefree_of_dvd hd.1)· intro J hJrfl/-- The nontrivial divisors of `∏_{p ∈ I} p`, organised by the size of the subset. -/theorem sum_divisors_prod_primes_filter {I : Finset ℕ} (hI : ∀ p ∈ I, p.Prime) (g : ℕ → ℝ) :∑ d ∈ (∏ p ∈ I, p).divisors.filter (fun d => 1 < d), g d= ∑ s ∈ Finset.Icc 1 I.card, ∑ J ∈ I.powersetCard s, g (∏ p ∈ J, p) := byclassicalset h : Finset ℕ → ℝ := fun J => g (∏ p ∈ J, p) with hhhave h2 : ∑ d ∈ (∏ p ∈ I, p).divisors.filter (fun d => ¬ (1 < d)), g d = g 1 := byhave hfil : (∏ p ∈ I, p).divisors.filter (fun d => ¬ (1 < d)) = {1} := byext dsimp only [Finset.mem_filter, Nat.mem_divisors, Finset.mem_singleton, not_lt]constructor· rintro ⟨⟨hdvd, h0⟩, hd1⟩have : d ≠ 0 := byrintro rflexact h0 (Nat.eq_zero_of_zero_dvd hdvd)omega· rintro rflexact ⟨⟨one_dvd _, (Finset.prod_pos (fun q hq => (hI q hq).pos)).ne'⟩, le_refl 1⟩rw [hfil, Finset.sum_singleton]have h3 : ∑ J ∈ I.powerset.filter (fun J => ¬ J.Nonempty), h J = g 1 := byhave hfil : I.powerset.filter (fun J => ¬ J.Nonempty) = {∅} := byext Jsimp only [Finset.mem_filter, Finset.mem_powerset, Finset.mem_singleton,Finset.not_nonempty_iff_eq_empty]constructor· rintro ⟨-, rfl⟩; rfl· rintro rfl; exact ⟨Finset.empty_subset _, rfl⟩rw [hfil, Finset.sum_singleton]simp [hh]have e1 := Finset.sum_filter_add_sum_filter_not (∏ p ∈ I, p).divisors (fun d => 1 < d) ghave e2 := Finset.sum_filter_add_sum_filter_not I.powerset (fun J => J.Nonempty) hhave hpow : ∑ J ∈ I.powerset, h J= ∑ j ∈ range (I.card + 1), ∑ J ∈ I.powersetCard j, h J := Finset.sum_powerset I hhave hshift : ∑ j ∈ range (I.card + 1), (∑ J ∈ I.powersetCard j, h J)= ∑ s ∈ Finset.Icc 1 I.card, (∑ J ∈ I.powersetCard s, h J)+ ∑ J ∈ I.powersetCard 0, h J := byrw [Finset.sum_range_succ']congr 1rw [← Finset.Ico_add_one_right_eq_Icc, Finset.sum_Ico_eq_sum_range]simp [add_comm]have hzero : ∑ J ∈ I.powersetCard 0, h J = g 1 := byrw [Finset.powersetCard_zero, Finset.sum_singleton]simp [hh]have hdiv : ∑ d ∈ (∏ p ∈ I, p).divisors, g d = ∑ J ∈ I.powerset, h J :=sum_divisors_prod_primes hI grw [hzero] at hshiftlinarith [e1, e2, hpow, hshift, hdiv, h2, h3]/-! ### Binomial ratios -/theorem descFactorial_mul_pow_le {N R s : ℕ} (hRN : R ≤ N) :R.descFactorial s * N ^ s ≤ N.descFactorial s * R ^ s := byhave hc : ∀ c : ℕ, c ^ s = ∏ _i ∈ range s, c := by intro c; simprw [Nat.descFactorial_eq_prod_range, Nat.descFactorial_eq_prod_range, hc N, hc R,← Finset.prod_mul_distrib, ← Finset.prod_mul_distrib]refine Finset.prod_le_prod' ?_intro i _rcases le_or_gt i R with h | h· obtain ⟨a, ha⟩ : ∃ a, R = i + a := ⟨R - i, by omega⟩obtain ⟨b, hb⟩ : ∃ b, N = R + b := ⟨N - R, by omega⟩subst ha; subst hbhave h1 : i + a + b - i = a + b := by omegahave h2 : i + a - i = a := by omegarw [h1, h2]nlinarith· have : R - i = 0 := by omegasimp [this]/-- **Binomial ratio.** -/theorem binom_ratio {N R s : ℕ} (hRN : R ≤ N) (hN : 0 < N) :((R.choose s : ℝ)) / (N.choose s) ≤ ((R:ℝ)/N) ^ s := byhave hNR : (0:ℝ) < (N:ℝ) := by exact_mod_cast hNrcases Nat.eq_zero_or_pos (N.choose s) with h0 | hpos· rw [h0]simp only [Nat.cast_zero, div_zero]positivity· have key := descFactorial_mul_pow_le (N := N) (R := R) (s := s) hRNrw [Nat.descFactorial_eq_factorial_mul_choose, Nat.descFactorial_eq_factorial_mul_choose] at keyhave keyR : (R.choose s : ℝ) * (N:ℝ) ^ s ≤ (N.choose s : ℝ) * (R:ℝ) ^ s := byhave h' : ((s.factorial * R.choose s * N ^ s : ℕ) : ℝ)≤ ((s.factorial * N.choose s * R ^ s : ℕ):ℝ) := by exact_mod_cast keypush_cast at h'have hf : (0:ℝ) < s.factorial := by positivitynlinarith [h']rw [div_pow, div_le_div_iff₀ (by exact_mod_cast hpos) (by positivity)]linarith [keyR]/-! ### Nested subsets -/theorem card_nested {α : Type*} [DecidableEq α] {P : Finset α} {s t : ℕ} (hst : s ≤ t){J : Finset α} (hJP : J ⊆ P) (hJcard : J.card = s) :((P.powersetCard t).filter (fun I => J ⊆ I)).card = (P.card - s).choose (t - s) := byclassicalhave hcard : ((P \ J).powersetCard (t - s)).card = (P.card - s).choose (t - s) := byrw [Finset.card_powersetCard, Finset.card_sdiff_of_subset hJP, hJcard]rw [← hcard]refine Finset.card_bij' (fun I _ => I \ J) (fun K _ => K ∪ J) ?_ ?_ ?_ ?_· intro I hIsimp only [Finset.mem_filter, Finset.mem_powersetCard] at hIobtain ⟨⟨hIP, hIcard⟩, hJI⟩ := hIrefine Finset.mem_powersetCard.mpr ⟨?_, ?_⟩· intro x hxsimp only [Finset.mem_sdiff] at hx ⊢exact ⟨hIP hx.1, hx.2⟩· rw [Finset.card_sdiff_of_subset hJI, hIcard, hJcard]· intro K hKsimp only [Finset.mem_powersetCard] at hKobtain ⟨hKP, hKcard⟩ := hKhave hdisj : Disjoint K J := Finset.disjoint_right.mpr (fun x hxJ hxK =>(Finset.mem_sdiff.mp (hKP hxK)).2 hxJ)refine Finset.mem_filter.mpr ⟨Finset.mem_powersetCard.mpr ⟨?_, ?_⟩, Finset.subset_union_right⟩· exact Finset.union_subset (hKP.trans Finset.sdiff_subset) hJP· rw [Finset.card_union_of_disjoint hdisj, hKcard, hJcard]omega· intro I hIsimp only [Finset.mem_filter] at hIexact Finset.sdiff_union_of_subset hI.2· intro K hKsimp only [Finset.mem_powersetCard] at hKhave hdisj : Disjoint K J := Finset.disjoint_right.mpr (fun x hxJ hxK =>(Finset.mem_sdiff.mp (hK.1 hxK)).2 hxJ)exact Finset.union_sdiff_cancel_right hdisj/-- **Nested subsets.** -/theorem sum_nested {α : Type*} [DecidableEq α] {P : Finset α} {s t : ℕ} (hst : s ≤ t)(f : Finset α → ℝ) :∑ I ∈ P.powersetCard t, ∑ J ∈ I.powersetCard s, f J= ((P.card - s).choose (t - s) : ℝ) * ∑ J ∈ P.powersetCard s, f J := byclassicalhave hinner : ∀ I ∈ P.powersetCard t, ∑ J ∈ I.powersetCard s, f J= ∑ J ∈ P.powersetCard s, if J ⊆ I then f J else 0 := byintro I hIhave hIP : I ⊆ P := (Finset.mem_powersetCard.mp hI).1rw [Finset.sum_ite, Finset.sum_const_zero, add_zero]refine Finset.sum_congr ?_ (fun _ _ => rfl)ext Jsimp only [Finset.mem_powersetCard, Finset.mem_filter]constructor· rintro ⟨hJI, hJc⟩; exact ⟨⟨hJI.trans hIP, hJc⟩, hJI⟩· rintro ⟨⟨-, hJc⟩, hJI⟩; exact ⟨hJI, hJc⟩rw [Finset.sum_congr rfl hinner, Finset.sum_comm, Finset.mul_sum]refine Finset.sum_congr rfl fun J hJ => ?_obtain ⟨hJP, hJc⟩ := Finset.mem_powersetCard.mp hJrw [Finset.sum_ite, Finset.sum_const_zero, add_zero, Finset.sum_const,card_nested hst hJP hJc, nsmul_eq_mul]/-! ### The three-factor Hölder inequality -/theorem holder3 {α : Type*} (s : Finset α) {f₁ f₂ f₃ : α → ℝ}(h₁ : ∀ x ∈ s, 0 ≤ f₁ x) (h₂ : ∀ x ∈ s, 0 ≤ f₂ x) (h₃ : ∀ x ∈ s, 0 ≤ f₃ x) :∑ x ∈ s, (f₁ x * f₂ x * f₃ x) ^ ((1:ℝ)/3)≤ ((∑ x ∈ s, f₁ x) * (∑ x ∈ s, f₂ x) * (∑ x ∈ s, f₃ x)) ^ ((1:ℝ)/3) := byset F₁ := ∑ x ∈ s, f₁ x with hF₁set F₂ := ∑ x ∈ s, f₂ x with hF₂set F₃ := ∑ x ∈ s, f₃ x with hF₃have hF₁0 : 0 ≤ F₁ := Finset.sum_nonneg h₁have hF₂0 : 0 ≤ F₂ := Finset.sum_nonneg h₂have hF₃0 : 0 ≤ F₃ := Finset.sum_nonneg h₃by_cases hz : F₁ = 0 ∨ F₂ = 0 ∨ F₃ = 0· have hzero : ∀ x ∈ s, (f₁ x * f₂ x * f₃ x) ^ ((1:ℝ)/3) = 0 := byintro x hxhave hx0 : f₁ x * f₂ x * f₃ x = 0 := byrcases hz with h | h | h· have := (Finset.sum_eq_zero_iff_of_nonneg h₁).mp h x hxrw [this]; ring· have := (Finset.sum_eq_zero_iff_of_nonneg h₂).mp h x hxrw [this]; ring· have := (Finset.sum_eq_zero_iff_of_nonneg h₃).mp h x hxrw [this]; ringrw [hx0, Real.zero_rpow (by norm_num)]rw [Finset.sum_congr rfl hzero]simp only [Finset.sum_const_zero]positivitypush_neg at hzobtain ⟨hz1, hz2, hz3⟩ := hzhave hp1 : 0 < F₁ := lt_of_le_of_ne hF₁0 (Ne.symm hz1)have hp2 : 0 < F₂ := lt_of_le_of_ne hF₂0 (Ne.symm hz2)have hp3 : 0 < F₃ := lt_of_le_of_ne hF₃0 (Ne.symm hz3)have hstep : ∀ x ∈ s, (f₁ x * f₂ x * f₃ x) ^ ((1:ℝ)/3)≤ (F₁ * F₂ * F₃) ^ ((1:ℝ)/3)* ((1/3) * (f₁ x / F₁) + (1/3) * (f₂ x / F₂) + (1/3) * (f₃ x / F₃)) := byintro x hxhave hq1 : 0 ≤ f₁ x / F₁ := div_nonneg (h₁ x hx) hF₁0have hq2 : 0 ≤ f₂ x / F₂ := div_nonneg (h₂ x hx) hF₂0have hq3 : 0 ≤ f₃ x / F₃ := div_nonneg (h₃ x hx) hF₃0have hgm := Real.geom_mean_le_arith_mean3_weighted (by norm_num : (0:ℝ) ≤ 1/3)(by norm_num : (0:ℝ) ≤ 1/3) (by norm_num : (0:ℝ) ≤ 1/3) hq1 hq2 hq3 (by norm_num)have hfac : f₁ x * f₂ x * f₃ x= (F₁ * F₂ * F₃) * ((f₁ x / F₁) * (f₂ x / F₂) * (f₃ x / F₃)) := byfield_simprw [hfac, Real.mul_rpow (by positivity) (by positivity),Real.mul_rpow (by positivity) hq3, Real.mul_rpow hq1 hq2, mul_assoc]exact mul_le_mul_of_nonneg_left hgm (by positivity)calc ∑ x ∈ s, (f₁ x * f₂ x * f₃ x) ^ ((1:ℝ)/3)≤ ∑ x ∈ s, (F₁ * F₂ * F₃) ^ ((1:ℝ)/3)* ((1/3) * (f₁ x / F₁) + (1/3) * (f₂ x / F₂) + (1/3) * (f₃ x / F₃)) :=Finset.sum_le_sum hstep_ = (F₁ * F₂ * F₃) ^ ((1:ℝ)/3) := byrw [← Finset.mul_sum]have hone : ∑ x ∈ s, ((1/3) * (f₁ x / F₁) + (1/3) * (f₂ x / F₂) + (1/3) * (f₃ x / F₃))= 1 := byrw [Finset.sum_add_distrib, Finset.sum_add_distrib, ← Finset.mul_sum, ← Finset.mul_sum,← Finset.mul_sum, ← Finset.sum_div, ← Finset.sum_div, ← Finset.sum_div,← hF₁, ← hF₂, ← hF₃]field_simpnorm_numrw [hone, mul_one]/-! ### The average collision bound -/theorem card_filter_dvd_prod_le {P : Finset ℕ} {s R : ℕ} {d : ℤ}(hR : (P.filter (fun p : ℕ => (p:ℤ) ∣ d)).card ≤ R) :((P.powersetCard s).filter (fun J => ((∏ p ∈ J, p : ℕ) : ℤ) ∣ d)).card ≤ R.choose s := byclassicalhave hsub : (P.powersetCard s).filter (fun J => ((∏ p ∈ J, p : ℕ) : ℤ) ∣ d)⊆ (P.filter (fun p : ℕ => (p:ℤ) ∣ d)).powersetCard s := byintro J hJsimp only [Finset.mem_filter, Finset.mem_powersetCard] at hJ ⊢obtain ⟨⟨hJP, hJc⟩, hdvd⟩ := hJrefine ⟨?_, hJc⟩intro p hprefine Finset.mem_filter.mpr ⟨hJP hp, ?_⟩have h1 : (p:ℤ) ∣ ((∏ q ∈ J, q : ℕ) : ℤ) :=Int.natCast_dvd_natCast.mpr (Finset.dvd_prod_of_mem _ hp)exact h1.trans hdvdcalc ((P.powersetCard s).filter (fun J => ((∏ p ∈ J, p : ℕ) : ℤ) ∣ d)).card≤ ((P.filter (fun p : ℕ => (p:ℤ) ∣ d)).powersetCard s).card := Finset.card_le_card hsub_ = ((P.filter (fun p : ℕ => (p:ℤ) ∣ d)).card).choose s := Finset.card_powersetCard _ __ ≤ R.choose s := Nat.choose_le_choose s hR/-- **Average collision bound.** -/theorem collision_average {P : Finset ℕ} {Y : Finset ℕ} (hY : Y.Nonempty) {R s : ℕ}(hcol : ∀ x ∈ Y, ∀ y ∈ Y, x ≠ y →(P.filter (fun p : ℕ => (p:ℤ) ∣ (x:ℤ) - (y:ℤ))).card ≤ R) :∑ J ∈ P.powersetCard s, Mmom Y (∏ p ∈ J, p)≤ ((P.powersetCard s).card : ℝ) * (1 / (Y.card:ℝ)) + ((R.choose s : ℝ)) := byclassicalset T := Y.card with hThave hT0 : 0 < T := Finset.card_pos.mpr hYhave hTR : (0:ℝ) < (T:ℝ) := by exact_mod_cast hT0set c : Finset ℕ → ℕ := fun J =>((Y ×ˢ Y).filter (fun q => ((∏ p ∈ J, p : ℕ):ℤ) ∣ (q.1:ℤ) - (q.2:ℤ))).card with hchave hMmom : ∀ J, Mmom Y (∏ p ∈ J, p) = (c J : ℝ) / (T:ℝ)^2 := byintro J; rflhave hswap : ∑ J ∈ P.powersetCard s, c J= ∑ q ∈ Y ×ˢ Y, ((P.powersetCard s).filter(fun J => ((∏ p ∈ J, p : ℕ):ℤ) ∣ (q.1:ℤ) - (q.2:ℤ))).card := bysimp only [hc, Finset.card_filter]rw [Finset.sum_comm]have hpoint : ∀ q ∈ Y ×ˢ Y, ((P.powersetCard s).filter(fun J => ((∏ p ∈ J, p : ℕ):ℤ) ∣ (q.1:ℤ) - (q.2:ℤ))).card≤ (if q.1 = q.2 then (P.powersetCard s).card else 0) + R.choose s := byintro q hqsimp only [Finset.mem_product] at hqby_cases hqe : q.1 = q.2· rw [if_pos hqe]exact le_trans (Finset.card_le_card (Finset.filter_subset _ _)) (Nat.le_add_right _ _)· rw [if_neg hqe, zero_add]exact card_filter_dvd_prod_le (hcol q.1 hq.1 q.2 hq.2 hqe)have hdiag : ((Y ×ˢ Y).filter (fun q => q.1 = q.2)).card ≤ T := byrefine Finset.card_le_card_of_injOn (fun q => q.1) (fun q hq => (Finset.mem_product.mp(Finset.mem_filter.mp hq).1).1) ?_intro a ha b hb habsimp only [Finset.coe_filter, Set.mem_setOf_eq] at ha hbexact Prod.ext hab (by rw [← ha.2, ← hb.2]; exact hab)have hsum : (∑ J ∈ P.powersetCard s, c J : ℕ)≤ T * (P.powersetCard s).card + T * T * R.choose s := byrw [hswap]calc ∑ q ∈ Y ×ˢ Y, ((P.powersetCard s).filter(fun J => ((∏ p ∈ J, p : ℕ):ℤ) ∣ (q.1:ℤ) - (q.2:ℤ))).card≤ ∑ q ∈ Y ×ˢ Y, ((if q.1 = q.2 then (P.powersetCard s).card else 0) + R.choose s) :=Finset.sum_le_sum hpoint_ = ((Y ×ˢ Y).filter (fun q => q.1 = q.2)).card * (P.powersetCard s).card+ (Y ×ˢ Y).card * R.choose s := byrw [Finset.sum_add_distrib, Finset.sum_ite, Finset.sum_const, Finset.sum_const_zero,add_zero, Finset.sum_const, smul_eq_mul, smul_eq_mul]_ ≤ T * (P.powersetCard s).card + T * T * R.choose s := byhave hcard : (Y ×ˢ Y).card = T * T := by rw [Finset.card_product]rw [hcard]exact Nat.add_le_add_right (Nat.mul_le_mul_right _ hdiag) _have hsumR : (∑ J ∈ P.powersetCard s, (c J : ℝ))≤ (T:ℝ) * (P.powersetCard s).card + (T:ℝ) * T * R.choose s := byhave h' := (Nat.cast_le (α := ℝ)).mpr hsumpush_cast at h'exact h'calc ∑ J ∈ P.powersetCard s, Mmom Y (∏ p ∈ J, p)= (∑ J ∈ P.powersetCard s, (c J : ℝ)) / (T:ℝ)^2 := byrw [Finset.sum_div]exact Finset.sum_congr rfl fun J _ => hMmom J_ ≤ ((T:ℝ) * (P.powersetCard s).card + (T:ℝ) * T * R.choose s) / (T:ℝ)^2 :=div_le_div_of_nonneg_right hsumR (by positivity)_ = ((P.powersetCard s).card : ℝ) * (1 / (T:ℝ)) + (R.choose s : ℝ) := byfield_simp/-! ### Numerical preparation -/theorem two_thirds_prod_le {x y r : ℝ} (hx : 0 ≤ x) (hy : 0 ≤ y) (hr : 0 ≤ r) :(x + r) ^ ((2:ℝ)/3) * (y + r) ^ ((1:ℝ)/3)≤ x ^ ((2:ℝ)/3) * y ^ ((1:ℝ)/3) + x ^ ((2:ℝ)/3) * r ^ ((1:ℝ)/3)+ r ^ ((2:ℝ)/3) * y ^ ((1:ℝ)/3) + r := byhave h1 : (x + r) ^ ((2:ℝ)/3) ≤ x ^ ((2:ℝ)/3) + r ^ ((2:ℝ)/3) :=Real.rpow_add_le_add_rpow hx hr (by norm_num) (by norm_num)have h2 : (y + r) ^ ((1:ℝ)/3) ≤ y ^ ((1:ℝ)/3) + r ^ ((1:ℝ)/3) :=Real.rpow_add_le_add_rpow hy hr (by norm_num) (by norm_num)have hrr : r ^ ((2:ℝ)/3) * r ^ ((1:ℝ)/3) = r := byrw [← Real.rpow_add' hr (by norm_num)]norm_numcalc (x + r) ^ ((2:ℝ)/3) * (y + r) ^ ((1:ℝ)/3)≤ (x ^ ((2:ℝ)/3) + r ^ ((2:ℝ)/3)) * (y ^ ((1:ℝ)/3) + r ^ ((1:ℝ)/3)) :=mul_le_mul h1 h2 (by positivity) (by positivity)_ = x ^ ((2:ℝ)/3) * y ^ ((1:ℝ)/3) + x ^ ((2:ℝ)/3) * r ^ ((1:ℝ)/3)+ r ^ ((2:ℝ)/3) * y ^ ((1:ℝ)/3) + r ^ ((2:ℝ)/3) * r ^ ((1:ℝ)/3) := by ring_ = _ := by rw [hrr]theorem sum_Icc_choose_pow (t : ℕ) (x : ℝ) :∑ s ∈ Finset.Icc 1 t, (t.choose s : ℝ) * x ^ s = (1 + x) ^ t - 1 := byhave hbin : (x + 1) ^ t = ∑ s ∈ range (t+1), x ^ s * (t.choose s : ℝ) := byrw [add_pow]exact Finset.sum_congr rfl fun s _ => by rw [one_pow, mul_one]have hsplit : ∑ s ∈ range (t+1), x ^ s * (t.choose s : ℝ)= ∑ s ∈ Finset.Icc 1 t, x ^ s * (t.choose s : ℝ) + x ^ 0 * (t.choose 0 : ℝ) := byrw [Finset.sum_range_succ']congr 1rw [← Finset.Ico_add_one_right_eq_Icc, Finset.sum_Ico_eq_sum_range]simp [add_comm]have hcomm : ∑ s ∈ Finset.Icc 1 t, (t.choose s : ℝ) * x ^ s= ∑ s ∈ Finset.Icc 1 t, x ^ s * (t.choose s : ℝ) :=Finset.sum_congr rfl fun s _ => by ringhave hx1 : (1 + x) ^ t = (x + 1) ^ t := by ring_nfrw [hcomm, hx1, hbin, hsplit]simptheorem cube_root_mul {c a b : ℝ} (hc : 0 ≤ c) (ha : 0 ≤ a) (hb : 0 ≤ b) :((2*c*a) * (2*c*a) * (c*b)) ^ ((1:ℝ)/3) ≤ 2 * c * (a ^ ((2:ℝ)/3) * b ^ ((1:ℝ)/3)) := byhave hdiff : (2*c)^(3:ℕ) * (a^(2:ℕ) * b) - (2*c*a) * (2*c*a) * (c*b) = 4*(c^3*(a^2*b)) := byringhave hnn : (0:ℝ) ≤ 4*(c^3*(a^2*b)) := by positivityhave hle : (2*c*a) * (2*c*a) * (c*b) ≤ (2*c)^(3:ℕ) * (a^(2:ℕ) * b) := by linarithhave h1 : ((2*c*a) * (2*c*a) * (c*b)) ^ ((1:ℝ)/3) ≤ ((2*c)^(3:ℕ) * (a^(2:ℕ) * b)) ^ ((1:ℝ)/3) :=Real.rpow_le_rpow (by positivity) hle (by norm_num)have h2 : ((2*c)^(3:ℕ) * (a^(2:ℕ) * b)) ^ ((1:ℝ)/3)= (2*c) * (a ^ ((2:ℝ)/3) * b ^ ((1:ℝ)/3)) := byrw [Real.mul_rpow (by positivity) (by positivity), Real.mul_rpow (by positivity) hb]congr 1· rw [← Real.rpow_natCast (2*c) 3, ← Real.rpow_mul (by positivity)]norm_num· congr 1rw [← Real.rpow_natCast a 2, ← Real.rpow_mul ha]norm_numrw [h2] at h1calc ((2*c*a) * (2*c*a) * (c*b)) ^ ((1:ℝ)/3) ≤ (2*c) * (a ^ ((2:ℝ)/3) * b ^ ((1:ℝ)/3)) := h1_ = 2 * c * (a ^ ((2:ℝ)/3) * b ^ ((1:ℝ)/3)) := by ringtheorem prod_le_pow_of_le {J : Finset ℕ} {U : ℝ} (hU0 : 0 ≤ U) (hJ : ∀ p ∈ J, (p:ℝ) ≤ U) :((∏ p ∈ J, p : ℕ) : ℝ) ^ ((2:ℝ)/3) ≤ (U ^ ((2:ℝ)/3)) ^ J.card := byhave hcast : ((∏ p ∈ J, p : ℕ) : ℝ) = ∏ p ∈ J, (p:ℝ) := by push_cast; ringhave hprod : ∏ p ∈ J, (p:ℝ) ≤ U ^ J.card := bycalc ∏ p ∈ J, (p:ℝ) ≤ ∏ _p ∈ J, U :=Finset.prod_le_prod (fun p _ => by positivity) hJ_ = U ^ J.card := by rw [Finset.prod_const]have h1 : ((∏ p ∈ J, p : ℕ) : ℝ) ^ ((2:ℝ)/3) ≤ (U ^ J.card) ^ ((2:ℝ)/3) := byrw [hcast]exact Real.rpow_le_rpow (by positivity) hprod (by norm_num)have h2 : (U ^ J.card) ^ ((2:ℝ)/3) = (U ^ ((2:ℝ)/3)) ^ J.card := byrw [← Real.rpow_natCast U J.card, ← Real.rpow_mul hU0,← Real.rpow_natCast (U ^ ((2:ℝ)/3)) J.card, ← Real.rpow_mul hU0, mul_comm]rw [h2] at h1exact h1/-- The average moment over all `s`-element subsets of `P`. -/theorem moment_avg_le {P Y : Finset ℕ} (hY : Y.Nonempty) {R s : ℕ} (hR : R ≤ P.card)(hN : 0 < P.card)(hcol : ∀ x ∈ Y, ∀ y ∈ Y, x ≠ y →(P.filter (fun p : ℕ => (p:ℤ) ∣ (x:ℤ) - (y:ℤ))).card ≤ R) :∑ J ∈ P.powersetCard s, Mmom Y (∏ p ∈ J, p)≤ (P.card.choose s : ℝ) * (1 / (Y.card:ℝ) + ((R:ℝ)/P.card) ^ s) := byhave hca := collision_average hY hcol (P := P) (R := R) (s := s)rw [Finset.card_powersetCard] at hcahave hchoose : (R.choose s : ℝ) ≤ (P.card.choose s : ℝ) * ((R:ℝ)/P.card) ^ s := byrcases Nat.eq_zero_or_pos (P.card.choose s) with h0 | hpos· have hz : R.choose s = 0 := Nat.le_zero.mp (h0 ▸ Nat.choose_le_choose s hR)rw [hz, h0]simp· have hb := binom_ratio hR hN (s := s)rw [div_le_iff₀ (by exact_mod_cast hpos)] at hblinarith [hb]linarith [hca, hchoose]/-- The average of the full moment product over all `s`-element subsets of `P`. -/theorem moment_prod_sum_bound {P X₁ X₂ X : Finset ℕ}(hX₁ : X₁.Nonempty) (hX₂ : X₂.Nonempty) (hX : X.Nonempty){U a b : ℝ} (hU0 : 0 ≤ U) (hPU : ∀ p ∈ P, (p:ℝ) ≤ U) (ha : 0 ≤ a) (hb : 0 ≤ b)(hb₁ : 1 / (X₁.card:ℝ) ≤ 2 * a) (hb₂ : 1 / (X₂.card:ℝ) ≤ 2 * a)(hbX : 1 / (X.card:ℝ) ≤ b){R s : ℕ} (hR : R ≤ P.card) (hN : 0 < P.card)(hcol₁ : ∀ x ∈ X₁, ∀ y ∈ X₁, x ≠ y →(P.filter (fun p : ℕ => (p:ℤ) ∣ (x:ℤ) - (y:ℤ))).card ≤ R)(hcol₂ : ∀ x ∈ X₂, ∀ y ∈ X₂, x ≠ y →(P.filter (fun p : ℕ => (p:ℤ) ∣ (x:ℤ) - (y:ℤ))).card ≤ R)(hcolX : ∀ x ∈ X, ∀ y ∈ X, x ≠ y →(P.filter (fun p : ℕ => (p:ℤ) ∣ (x:ℤ) - (y:ℤ))).card ≤ R) :∑ J ∈ P.powersetCard s, (((∏ p ∈ J, p : ℕ)):ℝ) ^ ((2:ℝ)/3)* (Mmom X₁ (∏ p ∈ J, p) * Mmom X₂ (∏ p ∈ J, p) * Mmom X (∏ p ∈ J, p)) ^ ((1:ℝ)/3)≤ (P.card.choose s : ℝ) * (2 * (U ^ ((2:ℝ)/3)) ^ s* ((a + ((R:ℝ)/P.card) ^ s) ^ ((2:ℝ)/3)* (b + ((R:ℝ)/P.card) ^ s) ^ ((1:ℝ)/3))) := byclassicalset C : ℝ := (P.card.choose s : ℝ) with hCset ρ : ℝ := (R:ℝ)/P.card with hρhave hρ0 : 0 ≤ ρ := by positivityhave hC0 : 0 ≤ C := by positivityset w : ℝ := U ^ ((2:ℝ)/3) with hwhave hw0 : 0 ≤ w := Real.rpow_nonneg hU0 _have hstep1 : ∀ J ∈ P.powersetCard s, (((∏ p ∈ J, p : ℕ)):ℝ) ^ ((2:ℝ)/3)* (Mmom X₁ (∏ p ∈ J, p) * Mmom X₂ (∏ p ∈ J, p) * Mmom X (∏ p ∈ J, p)) ^ ((1:ℝ)/3)≤ w ^ s* (Mmom X₁ (∏ p ∈ J, p) * Mmom X₂ (∏ p ∈ J, p) * Mmom X (∏ p ∈ J, p)) ^ ((1:ℝ)/3) := byintro J hJobtain ⟨hJP, hJc⟩ := Finset.mem_powersetCard.mp hJhave h1 := prod_le_pow_of_le (J := J) hU0 (fun p hp => hPU p (hJP hp))rw [hJc] at h1exact mul_le_mul_of_nonneg_right h1 (Real.rpow_nonneg(mul_nonneg (mul_nonneg (Mmom_nonneg _ _) (Mmom_nonneg _ _)) (Mmom_nonneg _ _)) _)have hholder := holder3 (P.powersetCard s)(f₁ := fun J => Mmom X₁ (∏ p ∈ J, p)) (f₂ := fun J => Mmom X₂ (∏ p ∈ J, p))(f₃ := fun J => Mmom X (∏ p ∈ J, p))(fun J _ => Mmom_nonneg _ _) (fun J _ => Mmom_nonneg _ _) (fun J _ => Mmom_nonneg _ _)have hm₁ : ∑ J ∈ P.powersetCard s, Mmom X₁ (∏ p ∈ J, p) ≤ 2 * C * (a + ρ ^ s) := byhave hbnd := moment_avg_le hX₁ hR hN hcol₁ (s := s)have h2 : C * (1 / (X₁.card:ℝ) + ρ ^ s) ≤ 2 * C * (a + ρ ^ s) := byhave hsum : 1 / (X₁.card:ℝ) + ρ ^ s ≤ 2 * (a + ρ ^ s) := byhave : (0:ℝ) ≤ ρ ^ s := by positivitylinarithnlinarith [hC0]linarithhave hm₂ : ∑ J ∈ P.powersetCard s, Mmom X₂ (∏ p ∈ J, p) ≤ 2 * C * (a + ρ ^ s) := byhave hbnd := moment_avg_le hX₂ hR hN hcol₂ (s := s)have h2 : C * (1 / (X₂.card:ℝ) + ρ ^ s) ≤ 2 * C * (a + ρ ^ s) := byhave hsum : 1 / (X₂.card:ℝ) + ρ ^ s ≤ 2 * (a + ρ ^ s) := byhave : (0:ℝ) ≤ ρ ^ s := by positivitylinarithnlinarith [hC0]linarithhave hmX : ∑ J ∈ P.powersetCard s, Mmom X (∏ p ∈ J, p) ≤ C * (b + ρ ^ s) := byhave hbnd := moment_avg_le hX hR hN hcolX (s := s)have h2 : C * (1 / (X.card:ℝ) + ρ ^ s) ≤ C * (b + ρ ^ s) := byhave hsum : 1 / (X.card:ℝ) + ρ ^ s ≤ b + ρ ^ s := by linarithnlinarith [hC0]linarithhave hnn₁ : 0 ≤ ∑ J ∈ P.powersetCard s, Mmom X₁ (∏ p ∈ J, p) :=Finset.sum_nonneg fun J _ => Mmom_nonneg _ _have hnn₂ : 0 ≤ ∑ J ∈ P.powersetCard s, Mmom X₂ (∏ p ∈ J, p) :=Finset.sum_nonneg fun J _ => Mmom_nonneg _ _have hnnX : 0 ≤ ∑ J ∈ P.powersetCard s, Mmom X (∏ p ∈ J, p) :=Finset.sum_nonneg fun J _ => Mmom_nonneg _ _have hA0 : 0 ≤ a + ρ ^ s := by positivityhave hB0 : 0 ≤ b + ρ ^ s := by positivityhave hprodle : (∑ J ∈ P.powersetCard s, Mmom X₁ (∏ p ∈ J, p))* (∑ J ∈ P.powersetCard s, Mmom X₂ (∏ p ∈ J, p))* (∑ J ∈ P.powersetCard s, Mmom X (∏ p ∈ J, p))≤ (2 * C * (a + ρ ^ s)) * (2 * C * (a + ρ ^ s)) * (C * (b + ρ ^ s)) := byhave h12 : (∑ J ∈ P.powersetCard s, Mmom X₁ (∏ p ∈ J, p))* (∑ J ∈ P.powersetCard s, Mmom X₂ (∏ p ∈ J, p))≤ (2 * C * (a + ρ ^ s)) * (2 * C * (a + ρ ^ s)) :=mul_le_mul hm₁ hm₂ hnn₂ (by positivity)exact mul_le_mul h12 hmX hnnX (by positivity)have hcube : ((∑ J ∈ P.powersetCard s, Mmom X₁ (∏ p ∈ J, p))* (∑ J ∈ P.powersetCard s, Mmom X₂ (∏ p ∈ J, p))* (∑ J ∈ P.powersetCard s, Mmom X (∏ p ∈ J, p))) ^ ((1:ℝ)/3)≤ 2 * C * ((a + ρ ^ s) ^ ((2:ℝ)/3) * (b + ρ ^ s) ^ ((1:ℝ)/3)) := byrefine le_trans (Real.rpow_le_rpow (mul_nonneg (mul_nonneg hnn₁ hnn₂) hnnX)hprodle (by norm_num)) ?_exact cube_root_mul hC0 hA0 hB0calc ∑ J ∈ P.powersetCard s, (((∏ p ∈ J, p : ℕ)):ℝ) ^ ((2:ℝ)/3)* (Mmom X₁ (∏ p ∈ J, p) * Mmom X₂ (∏ p ∈ J, p) * Mmom X (∏ p ∈ J, p)) ^ ((1:ℝ)/3)≤ ∑ J ∈ P.powersetCard s, w ^ s* (Mmom X₁ (∏ p ∈ J, p) * Mmom X₂ (∏ p ∈ J, p) * Mmom X (∏ p ∈ J, p)) ^ ((1:ℝ)/3) :=Finset.sum_le_sum hstep1_ = w ^ s * ∑ J ∈ P.powersetCard s,(Mmom X₁ (∏ p ∈ J, p) * Mmom X₂ (∏ p ∈ J, p) * Mmom X (∏ p ∈ J, p)) ^ ((1:ℝ)/3) := byrw [Finset.mul_sum]_ ≤ w ^ s * (2 * C * ((a + ρ ^ s) ^ ((2:ℝ)/3) * (b + ρ ^ s) ^ ((1:ℝ)/3))) :=mul_le_mul_of_nonneg_left (le_trans hholder hcube) (by positivity)_ = C * (2 * w ^ s * ((a + ρ ^ s) ^ ((2:ℝ)/3) * (b + ρ ^ s) ^ ((1:ℝ)/3))) := by ring/-! ### The finite selection criterion -/theorem rpow_npow_comm {x : ℝ} (hx : 0 ≤ x) (c : ℝ) (s : ℕ) : (x ^ s) ^ c = (x ^ c) ^ s := byrw [← Real.rpow_natCast x s, ← Real.rpow_mul hx, ← Real.rpow_natCast (x ^ c) s,← Real.rpow_mul hx, mul_comm]theorem one_div_card_le {X : Finset ℕ} {k : ℕ}(h : (2:ℝ) ^ ((k:ℝ)/2 - 1) ≤ X.card) : 1 / (X.card:ℝ) ≤ 2 * (2:ℝ) ^ (-(k:ℝ)/2) := byhave hpos : (0:ℝ) < (2:ℝ) ^ ((k:ℝ)/2 - 1) := Real.rpow_pos_of_pos (by norm_num) _have h1 : 1 / (X.card:ℝ) ≤ 1 / (2:ℝ) ^ ((k:ℝ)/2 - 1) := one_div_le_one_div_of_le hpos hhave h2 : (1:ℝ) / (2:ℝ)^((k:ℝ)/2 - 1) = 2 * (2:ℝ)^(-(k:ℝ)/2) := byhave hA : (1:ℝ) / (2:ℝ)^((k:ℝ)/2 - 1) = (2:ℝ)^(-((k:ℝ)/2 - 1)) := byrw [Real.rpow_neg (by norm_num), one_div]rw [hA, show -((k:ℝ)/2 - 1) = 1 + (-(k:ℝ)/2) by ring, Real.rpow_add (by norm_num),Real.rpow_one]linariththeorem one_div_card_le' {X : Finset ℕ} {k : ℕ}(h : (2:ℝ) ^ (k:ℝ) ≤ X.card) : 1 / (X.card:ℝ) ≤ (2:ℝ) ^ (-(k:ℝ)) := byhave hpos : (0:ℝ) < (2:ℝ) ^ (k:ℝ) := Real.rpow_pos_of_pos (by norm_num) _have h1 : 1 / (X.card:ℝ) ≤ 1 / (2:ℝ) ^ (k:ℝ) := one_div_le_one_div_of_le hpos hhave h2 : (2:ℝ)^(-(k:ℝ)) = 1/(2:ℝ)^(k:ℝ) := by rw [Real.rpow_neg (by norm_num), one_div]linarith/-- **A finite selection criterion.** -/theorem finite_selection {P : Finset ℕ} (hP : ∀ p ∈ P, p.Prime){U : ℝ} (hU0 : 0 ≤ U) (hPU : ∀ p ∈ P, (p:ℝ) ≤ U){X₁ X₂ X : Finset ℕ} (hX₁ : X₁.Nonempty) (hX₂ : X₂.Nonempty) (hX : X.Nonempty){k : ℕ}(hX₁card : (2:ℝ) ^ ((k:ℝ)/2 - 1) ≤ X₁.card)(hX₂card : (2:ℝ) ^ ((k:ℝ)/2 - 1) ≤ X₂.card)(hXcard : (2:ℝ) ^ (k:ℝ) ≤ X.card){R t : ℕ} (hR : R ≤ P.card) (ht1 : 1 ≤ t) (htN : t ≤ P.card)(hcol₁ : ∀ x ∈ X₁, ∀ y ∈ X₁, x ≠ y →(P.filter (fun p : ℕ => (p:ℤ) ∣ (x:ℤ) - (y:ℤ))).card ≤ R)(hcol₂ : ∀ x ∈ X₂, ∀ y ∈ X₂, x ≠ y →(P.filter (fun p : ℕ => (p:ℤ) ∣ (x:ℤ) - (y:ℤ))).card ≤ R)(hcolX : ∀ x ∈ X, ∀ y ∈ X, x ≠ y →(P.filter (fun p : ℕ => (p:ℤ) ∣ (x:ℤ) - (y:ℤ))).card ≤ R)(hnum : (2:ℝ) ^ (-(2*(k:ℝ))/3) * (1 + U ^ ((2:ℝ)/3)) ^ t+ (2:ℝ) ^ (-(k:ℝ)/3) * (1 + U ^ ((2:ℝ)/3) * ((R:ℝ)/P.card) ^ ((1:ℝ)/3)) ^ t+ (2:ℝ) ^ (-(k:ℝ)/3) * (1 + U ^ ((2:ℝ)/3) * ((R:ℝ)/P.card) ^ ((2:ℝ)/3)) ^ t+ (1 + U ^ ((2:ℝ)/3) * ((R:ℝ)/P.card)) ^ t - 1 < 1/2) :∃ I ∈ P.powersetCard t, ∑ d ∈ (∏ p ∈ I, p).divisors.filter (fun d => 1 < d),(d:ℝ) ^ ((2:ℝ)/3) * (Mmom X₁ d * Mmom X₂ d * Mmom X d) ^ ((1:ℝ)/3) < 1 := byclassicalhave hN : 0 < P.card := lt_of_lt_of_le ht1 htNset N := P.card with hNdefset g : ℕ → ℝ := fun d => (d:ℝ) ^ ((2:ℝ)/3)* (Mmom X₁ d * Mmom X₂ d * Mmom X d) ^ ((1:ℝ)/3) with hgset ρ : ℝ := (R:ℝ)/N with hρdefhave hρ0 : 0 ≤ ρ := by positivityset w : ℝ := U ^ ((2:ℝ)/3) with hwdefhave hw0 : 0 ≤ w := Real.rpow_nonneg hU0 _set r1 : ℝ := ρ ^ ((1:ℝ)/3) with hr1set r2 : ℝ := ρ ^ ((2:ℝ)/3) with hr2set a : ℝ := (2:ℝ) ^ (-(k:ℝ)/2) with hadefset b : ℝ := (2:ℝ) ^ (-(k:ℝ)) with hbdefhave ha0 : 0 ≤ a := Real.rpow_nonneg (by norm_num) _have hb0 : 0 ≤ b := Real.rpow_nonneg (by norm_num) _set c1 : ℝ := (2:ℝ) ^ (-(2*(k:ℝ))/3) with hc1set c2 : ℝ := (2:ℝ) ^ (-(k:ℝ)/3) with hc2have hc10 : 0 ≤ c1 := Real.rpow_nonneg (by norm_num) _have hc20 : 0 ≤ c2 := Real.rpow_nonneg (by norm_num) _have ha23 : a ^ ((2:ℝ)/3) = c2 := byrw [hadef, hc2, ← Real.rpow_mul (by norm_num : (0:ℝ) ≤ 2)]ring_nfhave hb13 : b ^ ((1:ℝ)/3) = c2 := byrw [hbdef, hc2, ← Real.rpow_mul (by norm_num : (0:ℝ) ≤ 2)]ring_nfhave hc1eq : c2 * c2 = c1 := byrw [hc1, hc2, ← Real.rpow_add (by norm_num : (0:ℝ) < 2)]ring_nfset D : ℕ → ℝ := fun s => 2 * (c1 * w ^ s + c2 * (w * r1) ^ s + c2 * (w * r2) ^ s+ (w * ρ) ^ s) with hDhave hkey : ∀ s : ℕ, ∑ J ∈ P.powersetCard s, g (∏ p ∈ J, p) ≤ (N.choose s : ℝ) * D s := byintro shave hmain := moment_prod_sum_bound hX₁ hX₂ hX hU0 hPU ha0 hb0(one_div_card_le hX₁card) (one_div_card_le hX₂card) (one_div_card_le' hXcard)hR hN hcol₁ hcol₂ hcolX (s := s)refine le_trans hmain ?_have hprod : (a + ρ ^ s) ^ ((2:ℝ)/3) * (b + ρ ^ s) ^ ((1:ℝ)/3)≤ c1 + c2 * r1 ^ s + r2 ^ s * c2 + ρ ^ s := byhave h := two_thirds_prod_le (x := a) (y := b) (r := ρ ^ s) ha0 hb0 (by positivity)rw [ha23, hb13] at hhave e1 : (ρ ^ s) ^ ((1:ℝ)/3) = r1 ^ s := by rw [hr1, rpow_npow_comm hρ0]have e2 : (ρ ^ s) ^ ((2:ℝ)/3) = r2 ^ s := by rw [hr2, rpow_npow_comm hρ0]rw [e1, e2, hc1eq] at hexact hhave hws : (0:ℝ) ≤ 2 * w ^ s := by positivityhave hstep : 2 * w ^ s * ((a + ρ ^ s) ^ ((2:ℝ)/3) * (b + ρ ^ s) ^ ((1:ℝ)/3)) ≤ D s := byrefine le_trans (mul_le_mul_of_nonneg_left hprod hws) (le_of_eq ?_)simp only [hD, mul_pow]ringexact mul_le_mul_of_nonneg_left hstep (by positivity)have hcardpos : 0 < N.choose t := Nat.choose_pos htNhave htotal : ∑ I ∈ P.powersetCard t,(∑ d ∈ (∏ p ∈ I, p).divisors.filter (fun d => 1 < d), g d)≤ (N.choose t : ℝ) * ∑ s ∈ Finset.Icc 1 t, (t.choose s : ℝ) * D s := byhave hIeq : ∀ I ∈ P.powersetCard t,(∑ d ∈ (∏ p ∈ I, p).divisors.filter (fun d => 1 < d), g d)= ∑ s ∈ Finset.Icc 1 t, ∑ J ∈ I.powersetCard s, g (∏ p ∈ J, p) := byintro I hI'obtain ⟨hIP, hIc⟩ := Finset.mem_powersetCard.mp hI'have hx := sum_divisors_prod_primes_filter (I := I) (fun p hp => hP p (hIP hp)) grw [hIc] at hxexact hxrw [Finset.sum_congr rfl hIeq, Finset.sum_comm, Finset.mul_sum]refine Finset.sum_le_sum ?_intro s hs'have hst : s ≤ t := (Finset.mem_Icc.mp hs').2rw [sum_nested hst (fun J => g (∏ p ∈ J, p))]have hnn : (0:ℝ) ≤ ((N - s).choose (t - s) : ℝ) := by positivityrefine le_trans (mul_le_mul_of_nonneg_left (hkey s) hnn) (le_of_eq ?_)have hid : (N.choose t) * (t.choose s) = (N.choose s) * ((N - s).choose (t - s)) :=Nat.choose_mul hsthave heq : ((N - s).choose (t - s) : ℝ) * (N.choose s : ℝ)= (N.choose t : ℝ) * (t.choose s : ℝ) := byhave hcast := congrArg (fun n : ℕ => (n : ℝ)) hidpush_cast at hcastlinarithlinear_combination (D s) * heqhave hexpand : ∑ s ∈ Finset.Icc 1 t, (t.choose s : ℝ) * D s= 2 * (c1 * ((1+w)^t - 1) + c2 * ((1+w*r1)^t - 1) + c2 * ((1+w*r2)^t - 1)+ ((1+w*ρ)^t - 1)) := byhave hterm : ∀ s ∈ Finset.Icc 1 t, (t.choose s : ℝ) * D s= 2 * (c1 * ((t.choose s:ℝ) * w^s) + c2 * ((t.choose s:ℝ) * (w*r1)^s)+ c2 * ((t.choose s:ℝ) * (w*r2)^s) + ((t.choose s:ℝ) * (w*ρ)^s)) := byintro s _simp only [hD]ringrw [Finset.sum_congr rfl hterm, ← Finset.mul_sum]congr 1rw [Finset.sum_add_distrib, Finset.sum_add_distrib, Finset.sum_add_distrib,← Finset.mul_sum, ← Finset.mul_sum, ← Finset.mul_sum,sum_Icc_choose_pow, sum_Icc_choose_pow, sum_Icc_choose_pow, sum_Icc_choose_pow]have hlt : ∑ s ∈ Finset.Icc 1 t, (t.choose s : ℝ) * D s < 1 := byrw [hexpand]linarith [hnum, hc10, hc20]by_contra hconpush_neg at hconhave hge : ((P.powersetCard t).card : ℝ)≤ ∑ I ∈ P.powersetCard t, (∑ d ∈ (∏ p ∈ I, p).divisors.filter (fun d => 1 < d), g d) := bycalc ((P.powersetCard t).card : ℝ) = ∑ _I ∈ P.powersetCard t, (1:ℝ) := byrw [Finset.sum_const, nsmul_eq_mul, mul_one]_ ≤ _ := Finset.sum_le_sum hconrw [Finset.card_powersetCard] at hgehave hpos : (0:ℝ) < (N.choose t : ℝ) := by exact_mod_cast hcardposnlinarith [htotal, hlt, hge, hpos]/-! ## The numerical thresholdFor all large `k` the four terms occurring in the finite selection criterion are small,uniformly for `0 ≤ ρ ≤ 32 k⁻⁵`.-/section Numericalset_option maxRecDepth 10000set_option maxHeartbeats 2000000/-- The package of numerical bounds valid for all large `k`. -/def NumP (k : ℕ) : Prop :=K1P k ∧ 84 ≤ k ∧ 100 ≤ Real.log (Real.log k) ∧(Real.log k) ^ ((2:ℝ)/3) * 10 ^ 6 ≤ Real.log ktheorem exp_hundred_large : (10:ℝ)^(18:ℕ) ≤ Real.exp 100 := byhave h1 : Real.exp 100 = (Real.exp 1) ^ (100:ℕ) := byrw [← Real.exp_nat_mul]norm_numhave h2 : (2:ℝ) ^ (100:ℕ) ≤ (Real.exp 1) ^ (100:ℕ) :=pow_le_pow_left₀ (by norm_num) (by linarith [Real.exp_one_gt_d9]) 100have h3 : (10:ℝ)^(18:ℕ) ≤ (2:ℝ)^(100:ℕ) := by norm_numlinarith [h1 ▸ h2]theorem cube_root_big {u : ℝ} (hu : (10:ℝ)^(18:ℕ) ≤ u) : u ^ ((2:ℝ)/3) * 10 ^ 6 ≤ u := byhave hu0 : (0:ℝ) < u := lt_of_lt_of_le (by norm_num) huhave h1 : ((10:ℝ)^(18:ℕ)) ^ ((1:ℝ)/3) ≤ u ^ ((1:ℝ)/3) :=Real.rpow_le_rpow (by norm_num) hu (by norm_num)have h2 : ((10:ℝ)^(18:ℕ)) ^ ((1:ℝ)/3) = 10 ^ 6 := byrw [← Real.rpow_natCast (10:ℝ) 18, ← Real.rpow_mul (by norm_num)]norm_numrw [h2] at h1have h3 : u ^ ((2:ℝ)/3) * u ^ ((1:ℝ)/3) = u := byrw [← Real.rpow_add hu0]norm_numcalc u ^ ((2:ℝ)/3) * 10 ^ 6 ≤ u ^ ((2:ℝ)/3) * u ^ ((1:ℝ)/3) :=mul_le_mul_of_nonneg_left h1 (Real.rpow_nonneg hu0.le _)_ = u := h3theorem exists_NumP : ∃ K : ℕ, 3 ≤ K ∧ ∀ k : ℕ, K ≤ k → NumP k := byobtain ⟨K₁, hK₁3, hK₁⟩ := exists_K1refine ⟨max (max K₁ 84) ⌈Real.exp (Real.exp 100)⌉₊,le_trans hK₁3 (le_trans (le_max_left _ _) (le_max_left _ _)), ?_⟩intro k hkhave hk1 : K₁ ≤ k := le_trans (le_trans (le_max_left _ _) (le_max_left _ _)) hkhave hk84 : 84 ≤ k := le_trans (le_trans (le_max_right _ _) (le_max_left _ _)) hkhave hkexp : Real.exp (Real.exp 100) ≤ (k:ℝ) := byhave h1 : ⌈Real.exp (Real.exp 100)⌉₊ ≤ k := le_trans (le_max_right _ _) hkexact le_trans (Nat.le_ceil _) (by exact_mod_cast h1)have hu : Real.exp 100 ≤ Real.log k := byhave h := Real.log_le_log (Real.exp_pos _) hkexprwa [Real.log_exp] at hhave hu0 : (0:ℝ) < Real.log k := lt_of_lt_of_le (Real.exp_pos _) huhave hloglog : 100 ≤ Real.log (Real.log k) := byhave h := Real.log_le_log (Real.exp_pos 100) hurwa [Real.log_exp] at hexact ⟨hK₁ k hk1, hk84, hloglog, cube_root_big (le_trans exp_hundred_large hu)⟩theorem exp_neg_four_lam : Real.exp (-(4*lam)) = 1/16 := byhave h : Real.log ((1:ℝ)/16) = -(4 * Real.log 2) := byrw [Real.log_div one_ne_zero (by norm_num), Real.log_one,show (16:ℝ) = 2^(4:ℕ) by norm_num, Real.log_pow]push_cast; ringrw [lam, ← h, Real.exp_log (by norm_num)]theorem two_rpow_eq_exp (x : ℝ) : (2:ℝ) ^ x = Real.exp (lam * x) := byrw [Real.rpow_def_of_pos (by norm_num), lam]/-- If `1 + w X` is at most `exp c`, then its `t`-th power is at most `exp A`. -/theorem term_bound {w X c A : ℝ} {t : ℕ} (hw : 0 < w) (hX : 0 < X)(hlog : Real.log w + Real.log X + lam ≤ c) (hc1 : 1 ≤ c)(hA : (t:ℝ) * c ≤ A) :(1 + w * X) ^ t ≤ Real.exp A := byhave hexpc : Real.exp 1 ≤ Real.exp c := Real.exp_le_exp.mpr hc1have he2 : (2:ℝ) ≤ Real.exp c := le_trans (by linarith [Real.exp_one_gt_d9]) hexpchave hwX : w * X ≤ Real.exp c / 2 := byhave hlogwX : Real.log (w * X) = Real.log w + Real.log X := Real.log_mul hw.ne' hX.ne'have h1 : w * X = Real.exp (Real.log (w * X)) := (Real.exp_log (by positivity)).symmrw [h1, hlogwX]have h2 : Real.exp c / 2 = Real.exp (c - lam) := byrw [Real.exp_sub, lam, Real.exp_log (by norm_num)]rw [h2]exact Real.exp_le_exp.mpr (by linarith)have hstep : 1 + w * X ≤ Real.exp c := by linarithcalc (1 + w * X) ^ t ≤ (Real.exp c) ^ t := pow_le_pow_left₀ (by positivity) hstep t_ = Real.exp ((t:ℝ) * c) := by rw [← Real.exp_nat_mul]_ ≤ Real.exp A := Real.exp_le_exp.mpr hA/-- **Bounds for the four numerical terms.** -/theorem numeric_terms {k : ℕ} (hK : NumP k) {ρ : ℝ} (hρ0 : 0 ≤ ρ) (hρ : ρ ≤ 32 / (k:ℝ)^5) :(2:ℝ) ^ (-(2*(k:ℝ))/3) * (1 + (4*(Qp k:ℝ)) ^ ((2:ℝ)/3)) ^ (tp k)+ (2:ℝ) ^ (-(k:ℝ)/3) * (1 + (4*(Qp k:ℝ)) ^ ((2:ℝ)/3) * ρ ^ ((1:ℝ)/3)) ^ (tp k)+ (2:ℝ) ^ (-(k:ℝ)/3) * (1 + (4*(Qp k:ℝ)) ^ ((2:ℝ)/3) * ρ ^ ((2:ℝ)/3)) ^ (tp k)+ (1 + (4*(Qp k:ℝ)) ^ ((2:ℝ)/3) * ρ) ^ (tp k) - 1 < 1/2 := byobtain ⟨hK1, hk84, hloglog, hcube⟩ := hKobtain ⟨hu3, h800, -, -, hQlow, hQhigh, hlog8u, hlogden, hap1, hap2, -, -, ht1, htk⟩ := hK1have hlam1 := lam_lt_onehave hlam0 := lam_posset u := Real.log k with huhave hu0 : (0:ℝ) < u := by linarithhave hlogu0 : (0:ℝ) ≤ Real.log u := Real.log_nonneg (by linarith)have hu100 : (100:ℝ) ≤ Real.log u := hlogloghave hubig : (1000:ℝ) ≤ u := byhave hexp : Real.exp 100 ≤ u := byhave h := Real.exp_le_exp.mpr hloglogrwa [Real.exp_log hu0] at hhave h1 : (1000:ℝ) ≤ Real.exp 100 := byhave h2 : Real.exp 100 = (Real.exp 1) ^ (100:ℕ) := byrw [← Real.exp_nat_mul]; norm_numhave h3 : (2:ℝ) ^ (100:ℕ) ≤ (Real.exp 1) ^ (100:ℕ) :=pow_le_pow_left₀ (by norm_num) (by linarith [Real.exp_one_gt_d9]) 100have h4 : (1000:ℝ) ≤ (2:ℝ)^(100:ℕ) := by norm_numlinarith [h2 ▸ h3]linarithhave hu23 : u ^ ((2:ℝ)/3) ≤ u / 10^6 := by linarith [hcube]have hlu : Real.log u ≤ u / 10^5 := byhave h1 : Real.log (u ^ ((2:ℝ)/3)) ≤ u ^ ((2:ℝ)/3) - 1 :=Real.log_le_sub_one_of_pos (Real.rpow_pos_of_pos hu0 _)rw [Real.log_rpow hu0] at h1linarith [h1, hu23]have hk3 : 3 ≤ k := by omegahave hkR : (0:ℝ) < (k:ℝ) := by positivityhave hk84R : (84:ℝ) ≤ (k:ℝ) := by exact_mod_cast hk84have hQ0 : (0:ℝ) < (Qp k : ℝ) := by exact_mod_cast Qp_pos khave hU0 : (0:ℝ) < 4 * (Qp k : ℝ) := by linarithset W : ℝ := (4 * (Qp k:ℝ)) ^ ((2:ℝ)/3) with hWdefhave hW0 : 0 < W := Real.rpow_pos_of_pos hU0 _have hlogW : Real.log W = (2/3) * Real.log (4 * (Qp k:ℝ)) := byrw [hWdef, Real.log_rpow hU0]have hlog4Q : Real.log (4 * (Qp k:ℝ)) ≤ 3 + 6*u + Real.log u := byhave h1 : 4 * (Qp k:ℝ) ≤ 8 * (k:ℝ)^6 * u := by linarithhave h2 : Real.log (4 * (Qp k:ℝ)) ≤ Real.log (8 * (k:ℝ)^6 * u) := Real.log_le_log hU0 h1have h3 : Real.log (8 * (k:ℝ)^6 * u) = Real.log 8 + 6*u + Real.log u := byrw [Real.log_mul (by positivity) (by positivity), Real.log_mul (by norm_num) (by positivity),Real.log_pow, ← hu]push_castringhave h4 : Real.log 8 ≤ 3 := log_three_lt_log_eightlinarith [h3 ▸ h2]have hlogW' : Real.log W ≤ 2 + 4*u + (2/3)*Real.log u := byrw [hlogW]; linarithset t := tp k with htdefhave htnn : (0:ℝ) ≤ (t:ℝ) := by positivityhave hden : (0:ℝ) < 7*u + 3*Real.log u := by linarithhave hapeq : ap k = lam / (7*u + 3*Real.log u) := rflhave ht : (t:ℝ) ≤ lam * k / (7*u + 3*Real.log u) := byhave h1 : (t:ℝ) ≤ ap k * k := tp_le_ap_mul hk3rw [hapeq] at h1calc (t:ℝ) ≤ lam / (7*u + 3*Real.log u) * k := h1_ = lam * k / (7*u + 3*Real.log u) := by ringhave ht7 : (t:ℝ) ≤ lam * k / (7*u) := byhave h1 : lam * (k:ℝ) / (7*u + 3*Real.log u) ≤ lam * k / (7*u) :=div_le_div_of_nonneg_left (by positivity) (by positivity) (by linarith)linarith-- the powers of `32 / k^5`have hpow0 : (0:ℝ) < 32 / (k:ℝ)^5 := by positivityset Y1 : ℝ := (32 / (k:ℝ)^5) ^ ((1:ℝ)/3) with hY1defset Y2 : ℝ := (32 / (k:ℝ)^5) ^ ((2:ℝ)/3) with hY2defhave hY10 : 0 < Y1 := Real.rpow_pos_of_pos hpow0 _have hY20 : 0 < Y2 := Real.rpow_pos_of_pos hpow0 _have hlog32 : Real.log (32 / (k:ℝ)^5) = 5*lam - 5*u := byhave e1 : Real.log (32 / (k:ℝ)^5) = Real.log 32 - Real.log ((k:ℝ)^5) :=Real.log_div (by norm_num) (by positivity)have e2 : Real.log (32:ℝ) = 5 * lam := byrw [show (32:ℝ) = 2^(5:ℕ) by norm_num, Real.log_pow, lam]push_cast; ringhave e3 : Real.log ((k:ℝ)^5) = 5 * u := byrw [Real.log_pow, ← hu]push_cast; ringrw [e1, e2, e3]have hlogY1 : Real.log Y1 = (1/3) * (5*lam - 5*u) := byrw [hY1def, Real.log_rpow hpow0, hlog32]have hlogY2 : Real.log Y2 = (2/3) * (5*lam - 5*u) := byrw [hY2def, Real.log_rpow hpow0, hlog32]-- Term 1have hT1 : (2:ℝ) ^ (-(2*(k:ℝ))/3) * (1 + W) ^ t ≤ 1/16 := byhave hpow : (1 + W) ^ t ≤ Real.exp (13*lam*(k:ℝ)/21) := byhave hrw : (1:ℝ) + W = 1 + W * 1 := by ringrw [hrw]refine term_bound (c := 13*u/3) hW0 one_pos ?_ ?_ ?_· rw [Real.log_one]linarith· linarith· have h := mul_le_mul_of_nonneg_right ht7 (by positivity : (0:ℝ) ≤ 13*u/3)calc (t:ℝ) * (13*u/3) ≤ lam * (k:ℝ) / (7*u) * (13*u/3) := h_ = 13*lam*(k:ℝ)/21 := by field_simp; ringcalc (2:ℝ) ^ (-(2*(k:ℝ))/3) * (1 + W) ^ t= Real.exp (lam * (-(2*(k:ℝ))/3)) * (1 + W) ^ t := by rw [two_rpow_eq_exp]_ ≤ Real.exp (lam * (-(2*(k:ℝ))/3)) * Real.exp (13*lam*(k:ℝ)/21) :=mul_le_mul_of_nonneg_left hpow (Real.exp_pos _).le_ = Real.exp (lam * (-(2*(k:ℝ))/3) + 13*lam*(k:ℝ)/21) := (Real.exp_add _ _).symm_ ≤ Real.exp (-(4*lam)) := Real.exp_le_exp.mpr (by nlinarith)_ = 1/16 := exp_neg_four_lam-- Term 2have hT2 : (2:ℝ) ^ (-(k:ℝ)/3) * (1 + W * ρ ^ ((1:ℝ)/3)) ^ t ≤ 1/16 := byhave hmono : (1 + W * ρ ^ ((1:ℝ)/3)) ^ t ≤ (1 + W * Y1) ^ t := byrefine pow_le_pow_left₀ (by positivity) ?_ thave h1 : ρ ^ ((1:ℝ)/3) ≤ Y1 := Real.rpow_le_rpow hρ0 hρ (by norm_num)linarith [mul_le_mul_of_nonneg_left h1 hW0.le]have hpow : (1 + W * Y1) ^ t ≤ Real.exp (lam * (k:ℝ)/3 - 4*lam) := byrefine term_bound (c := 5 + (7/3)*u + (2/3)*Real.log u) hW0 hY10 ?_ ?_ ?_· rw [hlogY1]; linarith· linarith· have hc2den : 5 + (7/3)*u + (2/3)*Real.log u≤ (7*u + 3*Real.log u)/3 - (1/6)*Real.log u := by linarithhave hS : (0:ℝ) ≤ lam * (k:ℝ) / (7*u + 3*Real.log u) := by positivityhave h1 : (t:ℝ) * (5 + (7/3)*u + (2/3)*Real.log u)≤ (lam * (k:ℝ) / (7*u + 3*Real.log u))* ((7*u + 3*Real.log u)/3 - (1/6)*Real.log u) :=mul_le_mul ht hc2den (by linarith) hShave h2 : (lam * (k:ℝ) / (7*u + 3*Real.log u))* ((7*u + 3*Real.log u)/3 - (1/6)*Real.log u)= lam * (k:ℝ)/3 - lam * (k:ℝ) * Real.log u / (6*(7*u + 3*Real.log u)) := byfield_simphave h3 : 4*lam ≤ lam * (k:ℝ) * Real.log u / (6*(7*u + 3*Real.log u)) := byrw [le_div_iff₀ (by positivity)]have hLu : Real.log u ≤ u := by linarithhave hstep1 : 4*lam * (6*(7*u + 3*Real.log u)) ≤ lam * (240*u) := bynlinarith [mul_le_mul_of_nonneg_left hLu hlam0.le]have hB : 240*u ≤ (k:ℝ) * Real.log u := byhave hmul : 800*u^2 * 100 ≤ (k:ℝ) * Real.log u :=mul_le_mul h800 hu100 (by norm_num) (by positivity)nlinarith [hmul]have hstep2 : lam * (240*u) ≤ lam * ((k:ℝ) * Real.log u) :=mul_le_mul_of_nonneg_left hB hlam0.lenlinarith [hstep1, hstep2]linarithcalc (2:ℝ) ^ (-(k:ℝ)/3) * (1 + W * ρ ^ ((1:ℝ)/3)) ^ t= Real.exp (lam * (-(k:ℝ)/3)) * (1 + W * ρ ^ ((1:ℝ)/3)) ^ t := by rw [two_rpow_eq_exp]_ ≤ Real.exp (lam * (-(k:ℝ)/3)) * Real.exp (lam * (k:ℝ)/3 - 4*lam) :=mul_le_mul_of_nonneg_left (le_trans hmono hpow) (Real.exp_pos _).le_ = Real.exp (lam * (-(k:ℝ)/3) + (lam * (k:ℝ)/3 - 4*lam)) := (Real.exp_add _ _).symm_ ≤ Real.exp (-(4*lam)) := Real.exp_le_exp.mpr (by linarith)_ = 1/16 := exp_neg_four_lam-- Term 3have hT3 : (2:ℝ) ^ (-(k:ℝ)/3) * (1 + W * ρ ^ ((2:ℝ)/3)) ^ t ≤ 1/16 := byhave hmono : (1 + W * ρ ^ ((2:ℝ)/3)) ^ t ≤ (1 + W * Y2) ^ t := byrefine pow_le_pow_left₀ (by positivity) ?_ thave h1 : ρ ^ ((2:ℝ)/3) ≤ Y2 := Real.rpow_le_rpow hρ0 hρ (by norm_num)linarith [mul_le_mul_of_nonneg_left h1 hW0.le]have hpow : (1 + W * Y2) ^ t ≤ Real.exp (2*lam*(k:ℝ)/7) := byrefine term_bound (c := 2*u) hW0 hY20 ?_ ?_ ?_· rw [hlogY2]; linarith· linarith· have h := mul_le_mul_of_nonneg_right ht7 (by positivity : (0:ℝ) ≤ 2*u)calc (t:ℝ) * (2*u) ≤ lam * (k:ℝ) / (7*u) * (2*u) := h_ = 2*lam*(k:ℝ)/7 := by field_simpcalc (2:ℝ) ^ (-(k:ℝ)/3) * (1 + W * ρ ^ ((2:ℝ)/3)) ^ t= Real.exp (lam * (-(k:ℝ)/3)) * (1 + W * ρ ^ ((2:ℝ)/3)) ^ t := by rw [two_rpow_eq_exp]_ ≤ Real.exp (lam * (-(k:ℝ)/3)) * Real.exp (2*lam*(k:ℝ)/7) :=mul_le_mul_of_nonneg_left (le_trans hmono hpow) (Real.exp_pos _).le_ = Real.exp (lam * (-(k:ℝ)/3) + 2*lam*(k:ℝ)/7) := (Real.exp_add _ _).symm_ ≤ Real.exp (-(4*lam)) := Real.exp_le_exp.mpr (by nlinarith)_ = 1/16 := exp_neg_four_lam-- Term 4have hT4 : (1 + W * ρ) ^ t ≤ 17/16 := byhave hWle : W ≤ Real.exp 2 * (k:ℝ)^4 * u ^ ((2:ℝ)/3) := byhave h1 : W ≤ Real.exp (2 + 4*u + (2/3)*Real.log u) := byhave h := Real.exp_le_exp.mpr hlogW'rwa [Real.exp_log hW0] at hhave h2 : Real.exp (2 + 4*u + (2/3)*Real.log u)= Real.exp 2 * (k:ℝ)^4 * u ^ ((2:ℝ)/3) := byrw [Real.exp_add, Real.exp_add]congr 1· congr 1rw [hu, show (4:ℝ)*Real.log (k:ℝ) = ((4:ℕ):ℝ)*Real.log (k:ℝ) by push_cast; ring,Real.exp_nat_mul, Real.exp_log hkR]· rw [Real.rpow_def_of_pos hu0]ring_nflinarith [h2 ▸ h1]have hsmall : (t:ℝ) * (W * ρ) ≤ 1/100 := byhave hWρ : W * ρ ≤ (Real.exp 2 * (k:ℝ)^4 * u ^ ((2:ℝ)/3)) * (32 / (k:ℝ)^5) := byhave h1 : W * ρ ≤ W * (32 / (k:ℝ)^5) := mul_le_mul_of_nonneg_left hρ hW0.lehave h2 : W * (32 / (k:ℝ)^5) ≤ (Real.exp 2 * (k:ℝ)^4 * u ^ ((2:ℝ)/3)) * (32 / (k:ℝ)^5) :=mul_le_mul_of_nonneg_right hWle (by positivity)linarithhave htk7 : (t:ℝ) ≤ (k:ℝ) / (7*u) := byhave h1 : lam * (k:ℝ) / (7*u) ≤ (k:ℝ) / (7*u) := byapply div_le_div_of_nonneg_right ?_ (by positivity)nlinarithlinarithhave he2 : Real.exp 2 ≤ 8 := byhave h1 : Real.exp 2 = Real.exp 1 * Real.exp 1 := byrw [← Real.exp_add]; norm_numnlinarith [Real.exp_one_lt_d9, Real.exp_pos 1]have hfinal : ((k:ℝ) / (7*u)) * ((Real.exp 2 * (k:ℝ)^4 * u ^ ((2:ℝ)/3)) * (32 / (k:ℝ)^5))≤ 1/100 := byhave hid : ((k:ℝ) / (7*u)) * ((Real.exp 2 * (k:ℝ)^4 * u ^ ((2:ℝ)/3)) * (32 / (k:ℝ)^5))= (32 * Real.exp 2 / 7) * (u ^ ((2:ℝ)/3) / u) := byfield_simprw [hid]have h1 : u ^ ((2:ℝ)/3) / u ≤ 1 / 10^6 := byrw [div_le_div_iff₀ hu0 (by norm_num)]nlinarith [hu23]have h2 : (0:ℝ) ≤ 32 * Real.exp 2 / 7 := by positivitynlinarith [h1, h2, he2]calc (t:ℝ) * (W * ρ)≤ ((k:ℝ) / (7*u)) * ((Real.exp 2 * (k:ℝ)^4 * u ^ ((2:ℝ)/3)) * (32 / (k:ℝ)^5)) := byapply mul_le_mul htk7 hWρ (by positivity) (by positivity)_ ≤ 1/100 := hfinalhave hexp : (1 + W * ρ) ^ t ≤ Real.exp ((t:ℝ) * (W * ρ)) := bycalc (1 + W * ρ) ^ t ≤ (Real.exp (W * ρ)) ^ t := byrefine pow_le_pow_left₀ (by positivity) ?_ tlinarith [Real.add_one_le_exp (W * ρ)]_ = Real.exp ((t:ℝ) * (W * ρ)) := by rw [← Real.exp_nat_mul]have hup : Real.exp ((t:ℝ) * (W * ρ)) ≤ 17/16 := byhave h1 : Real.exp ((t:ℝ) * (W * ρ)) ≤ Real.exp (1/100) := Real.exp_le_exp.mpr hsmallhave h2 : Real.exp (-(1/100:ℝ)) * Real.exp (1/100 : ℝ) = 1 := byrw [← Real.exp_add]; norm_numhave h3 : (99/100 : ℝ) ≤ Real.exp (-(1/100:ℝ)) := bylinarith [Real.add_one_le_exp (-(1/100:ℝ))]have h4 : (0:ℝ) < Real.exp (1/100 : ℝ) := Real.exp_pos _nlinarith [h1, h2, h3, h4]linarith [hexp, hup]linarith [hT1, hT2, hT3, hT4]end Numerical/-! ## Selection of the modulusThe analytic core: for all large `k`, every odd squarefree `V` with `ω(V) = k` and primefactors at most `4Q(k)` admits a modulus `A`, a product of `t(k)` primes from`(Q(k), 4Q(k)]`, such that every residue class modulo `A` is represented as`z₀ + 2z₁ + 4z₂ + 8z₃` with divisors `zᵢ` of `V`.-//-- **Available primes**: the interval `(Q(k), 4Q(k)]` contains many primes. -/theorem exists_K2 : ∃ K₂ : ℕ, 3 ≤ K₂ ∧ ∀ k : ℕ, K₂ ≤ k →(k : ℝ) ^ 6 / 8 ≤ (((Finset.Ioc (Qp k) (4 * Qp k)).filter Nat.Prime).card : ℝ) := byobtain ⟨q₀, hq₀2, hq₀⟩ := exists_q0obtain ⟨K₁, hK₁3, hK₁⟩ := exists_K1refine ⟨max K₁ q₀, le_trans hK₁3 (le_max_left _ _), ?_⟩intro k hkhave hk1 : K₁ ≤ k := le_trans (le_max_left _ _) hkhave hkq : q₀ ≤ k := le_trans (le_max_right _ _) hkobtain ⟨hu3, -, -, -, hQlow, -, hvi, -⟩ := hK₁ k hk1set u := Real.log k with huhave hk3 : 3 ≤ k := le_trans hK₁3 hk1have hkR3 : (3:ℝ) ≤ (k:ℝ) := by exact_mod_cast hk3have hk0 : (0:ℝ) < (k:ℝ) := by linarithhave hk6 : (k:ℝ) ≤ (k:ℝ) ^ 6 := byhave h1 : (1:ℝ) ≤ (k:ℝ) := by linarithhave h5 : (1:ℝ) ≤ (k:ℝ) ^ 5 := one_le_pow₀ h1have hrw : (k:ℝ) ^ 6 = (k:ℝ) ^ 5 * (k:ℝ) := by ringrw [hrw]calc (k:ℝ) = 1 * (k:ℝ) := by ring_ ≤ (k:ℝ) ^ 5 * (k:ℝ) := mul_le_mul_of_nonneg_right h5 (le_of_lt hk0)have hQk : (k:ℝ) ≤ (Qp k : ℝ) := byhave h1 : (k:ℝ) ^ 6 * 1 ≤ (k:ℝ) ^ 6 * u :=mul_le_mul_of_nonneg_left (by linarith) (by positivity)linarithhave hQq₀ : q₀ ≤ Qp k := byhave h1 : (q₀ : ℝ) ≤ (k:ℝ) := by exact_mod_cast hkqhave : (q₀ : ℝ) ≤ (Qp k : ℝ) := le_trans h1 hQkexact_mod_cast thishave hcount := hq₀ (Qp k) hQq₀have hQ1 : (1:ℝ) ≤ (Qp k : ℝ) := byhave := Qp_pos kexact_mod_cast thishave hlogpos : 0 < Real.log (4 * (Qp k : ℝ)) := byrefine Real.log_pos ?_linarithrefine le_trans ?_ hcountrw [div_le_div_iff₀ (by norm_num) hlogpos]calc (k:ℝ) ^ 6 * Real.log (4 * (Qp k : ℝ)) ≤ (k:ℝ) ^ 6 * (8 * u) := byapply mul_le_mul_of_nonneg_left hvi (by positivity)_ = 8 * ((k:ℝ) ^ 6 * u) := by ring_ ≤ 8 * (Qp k : ℝ) := by linarith_ = (Qp k : ℝ) * 8 := by ring/-- The primes in `(Q(k), 4Q(k)]` that neither divide `V` nor equal `pstar`. -/theorem available_primes {k : ℕ}(hK2 : (k:ℝ)^6/8 ≤ (((Finset.Ioc (Qp k) (4 * Qp k)).filter Nat.Prime).card : ℝ))(h16 : 16 * ((k:ℝ) + 1) ≤ (k:ℝ)^6){V : ℕ} (hV0 : V ≠ 0) (hVcard : V.primeFactors.card = k) (pstar : ℕ) :∃ P : Finset ℕ, (∀ p ∈ P, p.Prime ∧ Qp k < p ∧ p ≤ 4 * Qp k ∧ ¬ (p ∣ V) ∧ p ≠ pstar) ∧(k:ℝ)^6/16 ≤ (P.card : ℝ) := byclassicalset S := (Finset.Ioc (Qp k) (4 * Qp k)).filter Nat.Prime with hSset P := S.filter (fun p => ¬ (p ∣ V) ∧ p ≠ pstar) with hPhave hPS : P ⊆ S := Finset.filter_subset _ _have hsub : S \ P ⊆ insert pstar V.primeFactors := byintro p hprw [Finset.mem_sdiff] at hpobtain ⟨hpS, hpP⟩ := hphave hpprime : p.Prime := (Finset.mem_filter.mp hpS).2rw [hP, Finset.mem_filter] at hpPpush_neg at hpPhave hcon := hpP hpSrw [Finset.mem_insert]by_cases hdvd : p ∣ V· exact Or.inr (Nat.mem_primeFactors.mpr ⟨hpprime, hdvd, hV0⟩)· exact Or.inl (hcon hdvd)have hcard1 : (S \ P).card ≤ k + 1 := bycalc (S \ P).card ≤ (insert pstar V.primeFactors).card := Finset.card_le_card hsub_ ≤ V.primeFactors.card + 1 := Finset.card_insert_le _ __ = k + 1 := by rw [hVcard]have hcard2 : S.card = P.card + (S \ P).card := byrw [add_comm]exact (Finset.card_sdiff_add_card_eq_card hPS).symmhave hcard3 : (S.card : ℝ) ≤ (P.card : ℝ) + ((k:ℝ) + 1) := byhave h1 : (S.card : ℝ) = (P.card : ℝ) + ((S \ P).card : ℝ) := by exact_mod_cast hcard2have h2 : (((S \ P).card : ℕ) : ℝ) ≤ (k:ℝ) + 1 := by exact_mod_cast hcard1linarithrefine ⟨P, ?_, ?_⟩· intro p hphave hpS := hPS hphave h1 := Finset.mem_filter.mp hpShave h2 := (Finset.mem_filter.mp hp).2have h3 := Finset.mem_Ioc.mp h1.1exact ⟨h1.2, h3.1, h3.2, h2.1, h2.2⟩· linarith/-- **Selection of the modulus.** -/theorem exists_K4 : ∃ K₄ : ℕ, 3 ≤ K₄ ∧ ∀ k : ℕ, K₄ ≤ k → ∀ pstar : ℕ, pstar.Prime → Odd pstar →∀ V : ℕ, 0 < V → Odd V → Squarefree V → V.primeFactors.card = k →(∀ p ∈ V.primeFactors, p ≤ 4 * Qp k) →∃ A : ℕ, 1 < A ∧ Odd A ∧ Squarefree A ∧ Nat.Coprime A V ∧ ¬ (pstar ∣ A) ∧A.primeFactors.card = tp k ∧(∀ p ∈ A.primeFactors, Qp k < p ∧ p ≤ 4 * Qp k) ∧∀ c : ℕ, c < A → ∃ z₀ ∈ V.divisors, ∃ z₁ ∈ V.divisors, ∃ z₂ ∈ V.divisors,∃ z₃ ∈ V.divisors, (z₀ + 2 * z₁ + 4 * z₂ + 8 * z₃) % A = c % A := byclassicalobtain ⟨K₂, hK₂3, hK₂⟩ := exists_K2obtain ⟨KN, hKN3, hKN⟩ := exists_NumPrefine ⟨max K₂ KN, le_trans hK₂3 (le_max_left _ _), ?_⟩intro k hk pstar hpstarP hpstarOdd V hV0 hVodd hVsq hVcard hVlehave hK2k := hK₂ k (le_trans (le_max_left _ _) hk)have hNum : NumP k := hKN k (le_trans (le_max_right _ _) hk)have hK1 := hNum.1have hk84 := hNum.2.1obtain ⟨hu3, h800, h16, h32, hQlow, hQhigh, -, -, -, -, -, -, ht1, htk⟩ := hK1have hkR : (0:ℝ) < (k:ℝ) := byhave : 0 < k := by omegaexact_mod_cast thishave hk84R : (84:ℝ) ≤ (k:ℝ) := by exact_mod_cast hk84have hQ4 : 4 ≤ Qp k := byhave h1 : (4:ℝ) ≤ (Qp k : ℝ) := by nlinarithexact_mod_cast h1-- the available primesobtain ⟨P, hPprop, hPcard⟩ := available_primes hK2k h16 hV0.ne' hVcard pstarhave hNpos : (0:ℝ) < (P.card : ℝ) := by nlinarithhave hkN : k ≤ P.card := byhave h1 : (k:ℝ) ≤ (P.card : ℝ) := by nlinarithexact_mod_cast h1have hRN : 2 * k ≤ P.card := byhave h1 : ((2*k : ℕ) : ℝ) ≤ (P.card : ℝ) := by push_cast; nlinarithexact_mod_cast h1have htN : tp k ≤ P.card := le_trans htk hkNhave hρ0 : (0:ℝ) ≤ ((2*k : ℕ) : ℝ) / (P.card : ℝ) := by positivityhave hρle : ((2*k : ℕ) : ℝ) / (P.card : ℝ) ≤ 32 / (k:ℝ)^5 := byrw [div_le_div_iff₀ hNpos (by positivity)]push_castnlinarith [hPcard]-- splitting the prime factorsobtain ⟨V₁, V₂, hV₁0, hV₂0, hV12, hcop, hV₁card, hV₂card⟩ := factor_split hVsq hVcardhave hV₁dvd : V₁ ∣ V := ⟨V₂, hV12.symm⟩have hV₂dvd : V₂ ∣ V := ⟨V₁, by rw [← hV12]; ring⟩have hsq₁ : Squarefree V₁ := hVsq.squarefree_of_dvd hV₁dvdhave hsq₂ : Squarefree V₂ := hVsq.squarefree_of_dvd hV₂dvdhave hX₁ne : (V₁.divisors).Nonempty := ⟨1, Nat.one_mem_divisors.mpr hV₁0.ne'⟩have hX₂ne : (V₂.divisors).Nonempty := ⟨1, Nat.one_mem_divisors.mpr hV₂0.ne'⟩have hXne : (V.divisors).Nonempty := ⟨1, Nat.one_mem_divisors.mpr hV0.ne'⟩have hcard₁ : (V₁.divisors).card = 2 ^ (k/2) := byrw [card_divisors_squarefree hV₁0.ne' hsq₁, hV₁card]have hcard₂ : (V₂.divisors).card = 2 ^ (k - k/2) := byrw [card_divisors_squarefree hV₂0.ne' hsq₂, hV₂card]have hcardV : (V.divisors).card = 2 ^ k := byrw [card_divisors_squarefree hV0.ne' hVsq, hVcard]have hpow : ∀ m : ℕ, ((2:ℝ))^((m:ℕ):ℝ) = ((2^m : ℕ) : ℝ) := byintro m; rw [Real.rpow_natCast]; push_cast; ringhave hX₁bound : (2:ℝ) ^ ((k:ℝ)/2 - 1) ≤ ((V₁.divisors).card : ℝ) := byrw [hcard₁, ← hpow (k/2)]refine (Real.rpow_le_rpow_left_iff (by norm_num)).mpr ?_have h1 : (k:ℝ) ≤ 2 * ((k/2 : ℕ) : ℝ) + 1 := byhave h2 : k ≤ 2 * (k/2) + 1 := by omegaexact_mod_cast h2linarithhave hX₂bound : (2:ℝ) ^ ((k:ℝ)/2 - 1) ≤ ((V₂.divisors).card : ℝ) := byrw [hcard₂, ← hpow (k - k/2)]refine (Real.rpow_le_rpow_left_iff (by norm_num)).mpr ?_have h1 : (k:ℝ) ≤ 2 * ((k - k/2 : ℕ) : ℝ) := byhave h2 : k ≤ 2 * (k - k/2) := by omegaexact_mod_cast h2linarithhave hXbound : (2:ℝ) ^ (k:ℝ) ≤ ((V.divisors).card : ℝ) := byrw [hcardV, ← hpow k]-- the collision hypotheseshave hPprime : ∀ p ∈ P, p.Prime ∧ Qp k < p := fun p hp => ⟨(hPprop p hp).1, (hPprop p hp).2.1⟩have hcolV : ∀ x ∈ V.divisors, ∀ y ∈ V.divisors, x ≠ y →(P.filter (fun p : ℕ => (p:ℤ) ∣ (x:ℤ) - (y:ℤ))).card ≤ 2 * k :=fun x hx y hy hxy => prime_divisors_of_diff hVsq hVcard hVle hQ4 hx hy hxy hPprimehave hsub₁ : V₁.divisors ⊆ V.divisors := Nat.divisors_subset_of_dvd hV0.ne' hV₁dvdhave hsub₂ : V₂.divisors ⊆ V.divisors := Nat.divisors_subset_of_dvd hV0.ne' hV₂dvdhave hcol₁ : ∀ x ∈ V₁.divisors, ∀ y ∈ V₁.divisors, x ≠ y →(P.filter (fun p : ℕ => (p:ℤ) ∣ (x:ℤ) - (y:ℤ))).card ≤ 2 * k :=fun x hx y hy hxy => hcolV x (hsub₁ hx) y (hsub₁ hy) hxyhave hcol₂ : ∀ x ∈ V₂.divisors, ∀ y ∈ V₂.divisors, x ≠ y →(P.filter (fun p : ℕ => (p:ℤ) ∣ (x:ℤ) - (y:ℤ))).card ≤ 2 * k :=fun x hx y hy hxy => hcolV x (hsub₂ hx) y (hsub₂ hy) hxy-- the numerical hypothesishave hnum := numeric_terms hNum hρ0 hρle-- the selectionobtain ⟨I, hI, hSI⟩ := finite_selection (P := P) (fun p hp => (hPprop p hp).1)(U := 4 * (Qp k : ℝ)) (by positivity)(fun p hp => byhave h1 : p ≤ 4 * Qp k := (hPprop p hp).2.2.1have h2 : ((p : ℕ) : ℝ) ≤ ((4 * Qp k : ℕ) : ℝ) := by exact_mod_cast h1push_cast at h2linarith)hX₁ne hX₂ne hXne hX₁bound hX₂bound hXbound (R := 2 * k) (t := tp k) hRN ht1 htNhcol₁ hcol₂ hcolV hnumobtain ⟨hIP, hIcard⟩ := Finset.mem_powersetCard.mp hIhave hIprime : ∀ p ∈ I, p.Prime := fun p hp => (hPprop p (hIP hp)).1set A := ∏ p ∈ I, p with hAdefhave hApos : 0 < A := Finset.prod_pos (fun p hp => (hIprime p hp).pos)have hApf : A.primeFactors = I := Nat.primeFactors_prod hIprimehave hIne : I.Nonempty := Finset.card_pos.mp (by omega)have hA1 : 1 < A := byobtain ⟨p, hp⟩ := hInehave h2 : 2 ≤ p := (hIprime p hp).two_lehave hdvd : p ∣ A := Finset.dvd_prod_of_mem _ hphave := Nat.le_of_dvd hApos hdvdomegahave hAsq : Squarefree A := squarefree_prod_primes hIprimehave hAodd : Odd A := byrw [Nat.odd_iff, ← Nat.two_dvd_ne_zero]intro hdvdobtain ⟨p, hp, hpd⟩ := Prime.exists_mem_finset_dvd Nat.prime_two.prime hdvdhave hpe : 2 = p := (Nat.prime_dvd_prime_iff_eq Nat.prime_two (hIprime p hp)).mp hpdhave hQp : Qp k < p := (hPprop p (hIP hp)).2.1omegahave hAcop : Nat.Coprime A V := byrefine Nat.Coprime.prod_left (fun p hp => ?_)exact (Nat.Prime.coprime_iff_not_dvd (hIprime p hp)).mpr (hPprop p (hIP hp)).2.2.2.1have hAstar : ¬ (pstar ∣ A) := byintro hdvdobtain ⟨p, hp, hpd⟩ := Prime.exists_mem_finset_dvd hpstarP.prime hdvdhave hpe : pstar = p := (Nat.prime_dvd_prime_iff_eq hpstarP (hIprime p hp)).mp hpdexact (hPprop p (hIP hp)).2.2.2.2 hpe.symmrefine ⟨A, hA1, hAodd, hAsq, hAcop, hAstar, by rw [hApf, hIcard], ?_, ?_⟩· intro p hprw [hApf] at hpexact ⟨(hPprop p (hIP hp)).2.1, (hPprop p (hIP hp)).2.2.1⟩· intro c hchave hS : ∑ d ∈ A.divisors.filter (fun d => 1 < d),(d:ℝ) ^ ((2:ℝ)/3) * (Mmom V₁.divisors d * Mmom V₂.divisors d* Mmom (V₁ * V₂).divisors d) ^ ((1:ℝ)/3) < 1 := byrw [hV12]exact hSIhave hcov := coverage hV₁0 hV₂0 hcop hA1 hAodd hS crw [hV12] at hcovobtain ⟨z, hz⟩ := Finset.card_pos.mp hcovrw [Finset.mem_filter] at hzobtain ⟨hzmem, hzeq⟩ := hzsimp only [Finset.mem_product] at hzmemexact ⟨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⟩/-! ## Divisor representations`Rep n H` says that every `m < n` is a sum of at most `H` distinct positive divisorsof `n`, together with the basic tools used to build integers with this property.-//-- `Rep n H` means `n ≥ 1` and every natural number `m < n` is the sum of a set ofat most `H` distinct positive divisors of `n`. -/def Rep (n H : ℕ) : Prop :=1 ≤ n ∧ ∀ m < n, ∃ F ⊆ n.divisors, F.card ≤ H ∧ ∑ d ∈ F, d = mtheorem Rep.mono {n H H' : ℕ} (h : Rep n H) (hH : H ≤ H') : Rep n H' :=⟨h.1, fun m hm => byobtain ⟨F, hF, hcard, hsum⟩ := h.2 m hmexact ⟨F, hF, hcard.trans hH, hsum⟩⟩/-- Every `1 ≤ m ≤ n` is a sum of at most `T` distinct divisors of `n`, for real `T ≥ H`, `T ≥ 1`. -/theorem Rep.all_le {n H : ℕ} (h : Rep n H) {T : ℝ} (hT : (H : ℝ) ≤ T) (hT1 : 1 ≤ T){m : ℕ} (hm1 : 1 ≤ m) (hmn : m ≤ n) :∃ F ⊆ n.divisors, (F.card : ℝ) ≤ T ∧ ∑ d ∈ F, d = m := byrcases lt_or_eq_of_le hmn with hlt | heq· obtain ⟨F, hF, hcard, hsum⟩ := h.2 m hltexact ⟨F, hF, le_trans (by exact_mod_cast hcard) hT, hsum⟩· refine ⟨{n}, ?_, ?_, ?_⟩· intro x hxsimp only [Finset.mem_singleton] at hxsubst hxexact Nat.mem_divisors_self _ (by have := h.1; omega)· simpa using hT1· simpa using heq.symm/-- Modular lifting. -/theorem Rep.lift {A B H L : ℕ} (hA : 2 ≤ A) (hRep : Rep B H)(hF : ∀ c < A, ∃ F ⊆ B.divisors, F.card ≤ L ∧ (∑ d ∈ F, d) ≤ B ∧(∑ d ∈ F, d) % A = c % A ∧ ∀ d ∈ F, ¬ A ∣ d) :Rep (A * B) (H + L) := byhave hB : 1 ≤ B := hRep.1refine ⟨Nat.one_le_iff_ne_zero.mpr (by positivity), fun m hm => ?_⟩by_cases hmB : m < B· obtain ⟨F, hF, hcard, hsum⟩ := hRep.2 m hmBrefine ⟨F, ?_, by omega, hsum⟩intro x hxhave := Nat.mem_divisors.mp (hF hx)exact Nat.mem_divisors.mpr ⟨this.1.trans (Dvd.intro_left A rfl), by positivity⟩· push_neg at hmBobtain ⟨F, hF, hcardF, hsumB, hsumc, hnd⟩ := hF (m % A) (Nat.mod_lt _ (by omega))set s := ∑ d ∈ F, d with hshave hsm : s % A = m % A := by rw [hsumc, Nat.mod_mod_of_dvd]; exact dvd_rflhave hsle : s ≤ m := le_trans hsumB hmBhave hdvd : A ∣ m - s := (Nat.modEq_iff_dvd' hsle).mp hsmobtain ⟨u, hu⟩ := hdvdhave hub : u < B := byby_contra hconpush_neg at hconhave : A * B ≤ A * u := Nat.mul_le_mul_left _ hconomegaobtain ⟨G, hG, hcardG, hsumG⟩ := hRep.2 u hubhave hinj : Set.InjOn (fun e => A * e) G := fun x _ y _ h => bysimpa [Nat.mul_left_cancel_iff (show 0 < A by omega)] using hhave hdisj : Disjoint F (G.image (fun e => A * e)) := byrw [Finset.disjoint_right]intro x hx hxFobtain ⟨e, _, rfl⟩ := Finset.mem_image.mp hxexact hnd _ hxF (Dvd.intro e rfl)refine ⟨F ∪ G.image (fun e => A * e), ?_, ?_, ?_⟩· intro x hxrcases Finset.mem_union.mp hx with hx | hx· have := Nat.mem_divisors.mp (hF hx)exact Nat.mem_divisors.mpr ⟨this.1.trans (Dvd.intro_left A rfl), by positivity⟩· obtain ⟨e, he, rfl⟩ := Finset.mem_image.mp hxhave := Nat.mem_divisors.mp (hG he)exact Nat.mem_divisors.mpr ⟨Nat.mul_dvd_mul_left A this.1, by positivity⟩· rw [Finset.card_union_of_disjoint hdisj, Finset.card_image_of_injOn hinj]omega· rw [Finset.sum_union hdisj, Finset.sum_image hinj, ← Finset.mul_sum, hsumG, ← hs]omega/-- Binary representations. -/theorem rep_two_pow (E : ℕ) : Rep (2 ^ E) E := byinduction E with| zero => exact ⟨le_refl 1, fun m hm => ⟨∅, by simp, by simp, by simpa using (by omega : (0:ℕ) = m)⟩⟩| succ E ih =>refine ⟨Nat.one_le_two_pow, fun m hm => ?_⟩by_cases hlt : m < 2 ^ E· obtain ⟨F, hF, hcard, hsum⟩ := ih.2 m hltrefine ⟨F, ?_, by omega, hsum⟩intro x hxhave := hF hxrw [Nat.mem_divisors] at this ⊢exact ⟨this.1.trans (pow_dvd_pow 2 (Nat.le_succ E)), by positivity⟩· have hm' : m - 2 ^ E < 2 ^ E := byhave : (2:ℕ) ^ (E + 1) = 2 ^ E + 2 ^ E := by ringomegaobtain ⟨F, hF, hcard, hsum⟩ := ih.2 _ hm'have hnot : 2 ^ E ∉ F := byintro hmemhave : 2 ^ E ≤ ∑ d ∈ F, d := Finset.single_le_sum (f := fun d => d) (by simp) hmemomegarefine ⟨insert (2 ^ E) F, ?_, ?_, ?_⟩· intro x hxrcases Finset.mem_insert.mp hx with rfl | hx· exact Nat.mem_divisors.mpr ⟨pow_dvd_pow 2 (Nat.le_succ E), by positivity⟩· have := hF hxrw [Nat.mem_divisors] at this ⊢exact ⟨this.1.trans (pow_dvd_pow 2 (Nat.le_succ E)), by positivity⟩· rw [Finset.card_insert_of_notMem hnot]; omega· rw [Finset.sum_insert hnot, hsum]; omega/-- Adjoining a prime. -/theorem Rep.adjoin_prime {B H E p : ℕ} (hRep : Rep B H) (h2 : 2 ^ E ∣ B)(hp : p.Prime) (hodd : Odd p) (hpE : p < 2 ^ E) : Rep (p * B) (H + E) := byhave hB : 1 ≤ B := hRep.1have h2EB : 2 ^ E ≤ B := Nat.le_of_dvd (by omega) h2refine Rep.lift (A := p) hp.two_le hRep ?_intro c hcobtain ⟨F, hF, hcard, hsum⟩ := (rep_two_pow E).2 c (lt_trans hc hpE)have hsub : ∀ d ∈ F, d ∣ 2 ^ E := fun d hd => (Nat.mem_divisors.mp (hF hd)).1refine ⟨F, ?_, hcard, ?_, ?_, ?_⟩· intro x hxexact Nat.mem_divisors.mpr ⟨(hsub x hx).trans h2, by omega⟩· omega· rw [hsum]· intro d hd hdvdhave : p ∣ 2 ^ E := hdvd.trans (hsub d hd)have hp2 : p ∣ 2 := hp.dvd_of_dvd_pow thishave : p = 2 := (Nat.prime_dvd_prime_iff_eq hp Nat.prime_two).mp hp2rw [this] at hoddexact (Nat.not_odd_iff_even.mpr (even_iff_two_dvd.mpr dvd_rfl)) hodd/-- The initial representation bound. -/theorem rep_seed {P : Finset ℕ} (hP : ∀ p ∈ P, p.Prime ∧ Odd p) {E : ℕ}(hlt : ∀ p ∈ P, p < 2 ^ E) :Rep (2 ^ E * ∏ p ∈ P, p) ((P.card + 1) * E) := byclassicalrevert hP hltinduction P using Finset.induction with| empty => intro _ _; simpa using rep_two_pow E| insert p P hp ih =>intro hP hlthave hstep := ih (fun q hq => hP q (Finset.mem_insert_of_mem hq))(fun q hq => hlt q (Finset.mem_insert_of_mem hq))have hpdata := hP p (Finset.mem_insert_self p P)have hdvd : (2:ℕ) ^ E ∣ 2 ^ E * ∏ q ∈ P, q := Dvd.intro _ rflhave hres := Rep.adjoin_prime hstep hdvd hpdata.1 hpdata.2(hlt p (Finset.mem_insert_self p P))rw [Finset.prod_insert hp, Finset.card_insert_of_notMem hp]have heq : p * (2 ^ E * ∏ q ∈ P, q) = 2 ^ E * (p * ∏ q ∈ P, q) := by ringrw [heq] at hresexact hres.mono (le_of_eq (by ring))/-- Distinct powers of two times odd numbers are distinct. -/theorem two_pow_mul_odd_ne {i j a b : ℕ} (hij : i < j) (ha : Odd a) :2 ^ i * a ≠ 2 ^ j * b := byintro hhave hj : 2 ^ j = 2 ^ i * 2 ^ (j - i) := by rw [← pow_add]; congr 1; omegarw [hj, mul_assoc] at hhave h2 : a = 2 ^ (j - i) * b := Nat.eq_of_mul_eq_mul_left (by positivity) hhave : (2:ℕ) ∣ a := byrw [h2]exact Dvd.dvd.mul_right (dvd_pow_self 2 (by omega)) brw [Nat.odd_iff] at haomega/-- Four residues give one extension. -/theorem Rep.four_lift {V E H A : ℕ} (hV : 0 < V) (hVodd : Odd V) (hE : 4 ≤ E)(hRep : Rep (2 ^ E * V) H) (hA : 1 < A) (hAodd : Odd A) (hcop : Nat.Coprime A V)(hres : ∀ c < A, ∃ z₀ ∈ V.divisors, ∃ z₁ ∈ V.divisors, ∃ z₂ ∈ V.divisors, ∃ z₃ ∈ V.divisors,(z₀ + 2 * z₁ + 4 * z₂ + 8 * z₃) % A = c % A) :Rep (2 ^ E * V * A) (H + 4) := byhave key : Rep (A * (2 ^ E * V)) (H + 4) := byrefine Rep.lift hA hRep ?_intro c hcobtain ⟨z₀, hz₀, z₁, hz₁, z₂, hz₂, z₃, hz₃, hcong⟩ := hres c hchave hdvd : ∀ z ∈ V.divisors, z ∣ V := fun z hz => (Nat.mem_divisors.mp hz).1have hodd : ∀ z ∈ V.divisors, Odd z := fun z hz => hVodd.of_dvd_nat (hdvd z hz)have hle : ∀ z ∈ V.divisors, z ≤ V := fun z hz => Nat.le_of_dvd hV (hdvd z hz)have h01 : z₀ ≠ 2 * z₁ := bysimpa using two_pow_mul_odd_ne (i := 0) (j := 1) (b := z₁) (by norm_num) (hodd _ hz₀)have h02 : z₀ ≠ 4 * z₂ := byhave := two_pow_mul_odd_ne (i := 0) (j := 2) (b := z₂) (by norm_num) (hodd _ hz₀)norm_num at this; exact thishave h03 : z₀ ≠ 8 * z₃ := byhave := two_pow_mul_odd_ne (i := 0) (j := 3) (b := z₃) (by norm_num) (hodd _ hz₀)norm_num at this; exact thishave h12 : 2 * z₁ ≠ 4 * z₂ := byhave := two_pow_mul_odd_ne (i := 1) (j := 2) (b := z₂) (by norm_num) (hodd _ hz₁)norm_num at this; exact thishave h13 : 2 * z₁ ≠ 8 * z₃ := byhave := two_pow_mul_odd_ne (i := 1) (j := 3) (b := z₃) (by norm_num) (hodd _ hz₁)norm_num at this; exact thishave h23 : 4 * z₂ ≠ 8 * z₃ := byhave := two_pow_mul_odd_ne (i := 2) (j := 3) (b := z₃) (by norm_num) (hodd _ hz₂)norm_num at this; exact thishave hcard : ({z₀, 2 * z₁, 4 * z₂, 8 * z₃} : Finset ℕ).card = 4 := byrw [Finset.card_insert_of_notMem (by simp [h01, h02, h03]),Finset.card_insert_of_notMem (by simp [h12, h13]),Finset.card_insert_of_notMem (by simp [h23])]simphave hsum : ∑ d ∈ ({z₀, 2 * z₁, 4 * z₂, 8 * z₃} : Finset ℕ), d= z₀ + 2 * z₁ + 4 * z₂ + 8 * z₃ := byrw [Finset.sum_insert (by simp [h01, h02, h03]),Finset.sum_insert (by simp [h12, h13]),Finset.sum_insert (by simp [h23])]simp [add_assoc]have h16 : (16 : ℕ) ≤ 2 ^ E := bycalc (16:ℕ) = 2 ^ 4 := by norm_num_ ≤ 2 ^ E := Nat.pow_le_pow_right (by norm_num) hErefine ⟨{z₀, 2 * z₁, 4 * z₂, 8 * z₃}, ?_, by omega, ?_, ?_, ?_⟩· intro x hxhave hVpos : 0 < 2 ^ E * V := by positivitysimp only [Finset.mem_insert, Finset.mem_singleton] at hxrefine Nat.mem_divisors.mpr ⟨?_, by omega⟩have hd2 : (2:ℕ) ∣ 2 ^ E := dvd_pow_self 2 (by omega)have hd4 : (4:ℕ) ∣ 2 ^ E := byrw [show (4:ℕ) = 2 ^ 2 by norm_num]; exact pow_dvd_pow 2 (by omega)have hd8 : (8:ℕ) ∣ 2 ^ E := byrw [show (8:ℕ) = 2 ^ 3 by norm_num]; exact pow_dvd_pow 2 (by omega)rcases hx with rfl | rfl | rfl | rfl· exact (hdvd _ hz₀).trans (Dvd.intro_left _ rfl)· exact mul_dvd_mul hd2 (hdvd _ hz₁)· exact mul_dvd_mul hd4 (hdvd _ hz₂)· exact mul_dvd_mul hd8 (hdvd _ hz₃)· rw [hsum]have := hle _ hz₀; have := hle _ hz₁; have := hle _ hz₂; have := hle _ hz₃have : 16 * V ≤ 2 ^ E * V := Nat.mul_le_mul_right V h16omega· rw [hsum]; exact hcong· intro d hd hAdvdsimp only [Finset.mem_insert, Finset.mem_singleton] at hdhave hA2 : Nat.Coprime A 2 := Nat.coprime_two_right.mpr hAoddhave hcopd : Nat.Coprime A d := byrcases hd with rfl | rfl | rfl | rfl· exact hcop.coprime_dvd_right (hdvd _ hz₀)· exact Nat.Coprime.mul_right hA2 (hcop.coprime_dvd_right (hdvd _ hz₁))· exact Nat.Coprime.mul_right (by simpa using hA2.pow_right 2)(hcop.coprime_dvd_right (hdvd _ hz₂))· exact Nat.Coprime.mul_right (byhave : (8:ℕ) = 2 ^ 3 := by norm_numrw [this]; exact hA2.pow_right 3) (hcop.coprime_dvd_right (hdvd _ hz₃))have := Nat.Coprime.eq_one_of_dvd hcopd hAdvdomegahave heq : A * (2 ^ E * V) = 2 ^ E * V * A := by ringrwa [heq] at key/-! ## The recursive constructionStarting from a seed `2^E V₀` we repeatedly multiply by a modulus supplied by`exists_K4`, obtaining integers `B_j` with `Rep (B_j) (H₀ + 4j)` whose sizes grow atmost quadratically. The first `B_j` beyond a given `x` then satisfies the uniformrepresentation bound.-//-! ### Products of distinct positive integers -//-- A product of `k` distinct positive integers is at least `k!`. -/theorem factorial_le_prod {S : Finset ℕ} (hpos : ∀ p ∈ S, 1 ≤ p) :Nat.factorial S.card ≤ ∏ p ∈ S, p := byclassicalinduction hn : S.card generalizing S with| zero =>rw [Finset.card_eq_zero] at hnsubst hnsimp| succ n ih =>have hne : S.Nonempty := Finset.card_pos.mp (by omega)set m := S.max' hne with hmhave hmS : m ∈ S := S.max'_mem hnehave hsub : S ⊆ Finset.Icc 1 m := byintro x hxexact Finset.mem_Icc.mpr ⟨hpos x hx, S.le_max' x hx⟩have hcard : S.card ≤ m := byhave := Finset.card_le_card hsubsimpa [Nat.card_Icc] using thishave herase : (S.erase m).card = n := byrw [Finset.card_erase_of_mem hmS, hn]omegahave hih : Nat.factorial n ≤ ∏ p ∈ S.erase m, p :=ih (fun p hp => hpos p (Finset.mem_of_mem_erase hp)) herasehave hprod : ∏ p ∈ S, p = m * ∏ p ∈ S.erase m, p :=(Finset.mul_prod_erase S _ hmS).symmrw [hprod, Nat.factorial_succ]exact Nat.mul_le_mul (by omega) hih/-! ### The selection step -//-- The property of `A` produced by `exists_K4` for the integer `V`. -/def SelSpec (pstar V A : ℕ) : Prop :=1 < A ∧ Odd A ∧ Squarefree A ∧ Nat.Coprime A V ∧ ¬ (pstar ∣ A) ∧A.primeFactors.card = tp V.primeFactors.card ∧(∀ p ∈ A.primeFactors, Qp V.primeFactors.card < p ∧ p ≤ 4 * Qp V.primeFactors.card) ∧∀ c : ℕ, c < A → ∃ z₀ ∈ V.divisors, ∃ z₁ ∈ V.divisors, ∃ z₂ ∈ V.divisors,∃ z₃ ∈ V.divisors, (z₀ + 2 * z₁ + 4 * z₂ + 8 * z₃) % A = c % Aopen Classical in/-- The chosen modulus at one step. -/noncomputable def stepA (pstar V : ℕ) : ℕ :=if h : ∃ A, SelSpec pstar V A then Classical.choose h else 1theorem stepA_spec {pstar V : ℕ} (h : ∃ A, SelSpec pstar V A) :SelSpec pstar V (stepA pstar V) := byclassicalrw [stepA, dif_pos h]exact Classical.choose_spec h/-- The sequence of squarefree odd integers. -/noncomputable def Vseq (pstar V₀ : ℕ) : ℕ → ℕ| 0 => V₀| (j + 1) => Vseq pstar V₀ j * stepA pstar (Vseq pstar V₀ j)/-- **The seed.** -/theorem exists_seed : ∃ k₀ E H₀ : ℕ, 4 ≤ E ∧ 1 ≤ H₀ ∧ 4 * Qp k₀ < 2 ^ E ∧(∀ pstar : ℕ, pstar.Prime → Odd pstar →∃ V₀ : ℕ, 0 < V₀ ∧ Odd V₀ ∧ Squarefree V₀ ∧ V₀.primeFactors.card = k₀ ∧¬ (pstar ∣ V₀) ∧ (∀ p ∈ V₀.primeFactors, Qp k₀ < p ∧ p ≤ 4 * Qp k₀) ∧Rep (2 ^ E * V₀) H₀ ∧ 2 ^ E * V₀ ≤ 2 ^ E * (4 * Qp k₀) ^ k₀) ∧(∀ k : ℕ, k₀ ≤ k → ∀ pstar : ℕ, pstar.Prime → Odd pstar →∀ V : ℕ, 0 < V → Odd V → Squarefree V → V.primeFactors.card = k →(∀ p ∈ V.primeFactors, p ≤ 4 * Qp k) → ∃ A, SelSpec pstar V A) ∧(∀ k : ℕ, k₀ ≤ k → K1P k) := byclassicalobtain ⟨K₄, hK₄3, hK₄⟩ := exists_K4obtain ⟨K₂, hK₂3, hK₂⟩ := exists_K2obtain ⟨K₁, hK₁3, hK₁⟩ := exists_K1set k₀ := max (max K₄ K₂) K₁ with hk₀have hk₀K₄ : K₄ ≤ k₀ := le_trans (le_max_left _ _) (le_max_left _ _)have hk₀K₂ : K₂ ≤ k₀ := le_trans (le_max_right _ _) (le_max_left _ _)have hk₀K₁ : K₁ ≤ k₀ := le_max_right _ _have hk₀3 : 3 ≤ k₀ := le_trans hK₄3 hk₀K₄set E := max 4 (4 * Qp k₀) with hEhave hE4 : 4 ≤ E := le_max_left _ _have hE2 : 4 * Qp k₀ < 2 ^ E := bycalc 4 * Qp k₀ < 2 ^ (4 * Qp k₀) := Nat.lt_two_pow_self_ ≤ 2 ^ E := Nat.pow_le_pow_right (by norm_num) (le_max_right _ _)-- there are at least `k₀ + 1` primes in the intervalhave hmany : k₀ + 1 ≤ ((Finset.Ioc (Qp k₀) (4 * Qp k₀)).filter Nat.Prime).card := byhave h1 := hK₂ k₀ hk₀K₂have hk₀R : (3:ℝ) ≤ (k₀:ℝ) := by exact_mod_cast hk₀3have h2 : ((k₀:ℝ) + 1) ≤ (k₀:ℝ) ^ 6 / 8 := byhave h5 : (3:ℝ) ^ 5 ≤ (k₀:ℝ) ^ 5 := pow_le_pow_left₀ (by norm_num) hk₀R 5have hrw : (k₀:ℝ) ^ 6 = (k₀:ℝ) ^ 5 * (k₀:ℝ) := by ringrw [hrw]nlinarithhave h3 : ((k₀:ℝ) + 1) ≤ (((Finset.Ioc (Qp k₀) (4 * Qp k₀)).filter Nat.Prime).card : ℝ) :=le_trans h2 h1exact_mod_cast h3have hH₀ : 1 ≤ (k₀ + 1) * E := Nat.one_le_iff_ne_zero.mpr(Nat.mul_ne_zero (by omega) (by omega))refine ⟨k₀, E, (k₀ + 1) * E, hE4, hH₀, hE2, ?_, ?_, fun k hk => hK₁ k (le_trans hk₀K₁ hk)⟩· -- the seed itselfintro pstar hpstar hpoddset S := ((Finset.Ioc (Qp k₀) (4 * Qp k₀)).filter Nat.Prime).erase pstar with hShave hScard : k₀ ≤ S.card := byhave hpc := Finset.pred_card_le_card_erase(s := (Finset.Ioc (Qp k₀) (4 * Qp k₀)).filter Nat.Prime) (a := pstar)rw [hS]omegaobtain ⟨P, hPS, hPcard⟩ := Finset.exists_subset_card_eq hScardhave hPprime : ∀ p ∈ P, p.Prime := byintro p hphave := hPS hprw [hS, Finset.mem_erase, Finset.mem_filter] at thisexact this.2.2have hPmem : ∀ p ∈ P, Qp k₀ < p ∧ p ≤ 4 * Qp k₀ := byintro p hphave := hPS hprw [hS, Finset.mem_erase, Finset.mem_filter, Finset.mem_Ioc] at thisexact this.2.1have hPne : ∀ p ∈ P, p ≠ pstar := byintro p hphave := hPS hprw [hS, Finset.mem_erase] at thisexact this.1have hQ3 : 3 ≤ Qp k₀ := byby_contra hconpush_neg at hconhave hQ : (Qp k₀ : ℝ) < 3 := by exact_mod_cast hconhave : False := byhave hk₀R : (3:ℝ) ≤ (k₀:ℝ) := by exact_mod_cast hk₀3have h1 := hK₂ k₀ hk₀K₂have hcard : (((Finset.Ioc (Qp k₀) (4 * Qp k₀)).filter Nat.Prime).card : ℝ)≤ ((4 * Qp k₀ : ℕ) : ℝ) := byhave hsub : ((Finset.Ioc (Qp k₀) (4 * Qp k₀)).filter Nat.Prime).card ≤ 4 * Qp k₀ := bycalc ((Finset.Ioc (Qp k₀) (4 * Qp k₀)).filter Nat.Prime).card≤ (Finset.Ioc (Qp k₀) (4 * Qp k₀)).card := Finset.card_filter_le _ __ ≤ 4 * Qp k₀ := by rw [Nat.card_Ioc]; omegaexact_mod_cast hsubhave h5 : (3:ℝ) ^ 6 ≤ (k₀:ℝ) ^ 6 := pow_le_pow_left₀ (by norm_num) hk₀R 6push_cast at hcardnlinarithexact thishave hodd : ∀ p ∈ P, Odd p := byintro p hphave hp2 : 2 < p := lt_of_le_of_lt (by omega) (hPmem p hp).1exact (hPprime p hp).odd_of_ne_two (by omega)refine ⟨∏ p ∈ P, p, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_⟩· exact Finset.prod_pos (fun p hp => (hPprime p hp).pos)· rw [Nat.odd_iff, Finset.prod_nat_mod]have : ∀ p ∈ P, p % 2 = 1 := fun p hp => Nat.odd_iff.mp (hodd p hp)rw [Finset.prod_congr rfl this]simp· exact squarefree_prod_primes hPprime· rw [Nat.primeFactors_prod hPprime]; exact hPcard· intro hdvdobtain ⟨p, hp, hpa⟩ := Prime.exists_mem_finset_dvd hpstar.prime hdvdhave := (Nat.prime_dvd_prime_iff_eq hpstar (hPprime p hp)).mp hpaexact hPne p hp this.symm· intro p hprw [Nat.primeFactors_prod hPprime] at hpexact hPmem p hp· refine rep_seed (fun p hp => ⟨hPprime p hp, hodd p hp⟩) ?_ |>.mono ?_· intro p hpexact lt_of_le_of_lt (hPmem p hp).2 hE2· rw [hPcard]· have hprodle : ∏ p ∈ P, p ≤ (4 * Qp k₀) ^ k₀ := bycalc ∏ p ∈ P, p ≤ ∏ _p ∈ P, (4 * Qp k₀) :=Finset.prod_le_prod' (fun p hp => (hPmem p hp).2)_ = (4 * Qp k₀) ^ k₀ := by rw [Finset.prod_const, hPcard]exact Nat.mul_le_mul_left _ hprodle· -- the inductive step is availableintro k hk pstar hpstar hpodd V hV hVodd hVsq hVcard hVprimesobtain ⟨A, hA⟩ := hK₄ k (le_trans hk₀K₄ hk) pstar hpstar hpodd V hV hVodd hVsq hVcard hVprimesrefine ⟨A, ?_⟩rw [SelSpec, hVcard]exact hA/-! ### Size bounds for squarefree integers -//-- A squarefree integer with `k` prime factors satisfies `log V ≥ k log k / 12`. -/theorem log_self_lower {V : ℕ} (hsq : Squarefree V) {k : ℕ}(hcard : V.primeFactors.card = k) (hu : 3 ≤ Real.log k) :(k : ℝ) * Real.log k / 12 ≤ Real.log V := byhave hk3 : 3 ≤ k := byby_contra hconpush_neg at hconhave hkle : (k:ℝ) ≤ 2 := byhave : k ≤ 2 := by omegaexact_mod_cast thishave hlogle : Real.log k ≤ Real.log 2 := byrcases Nat.eq_zero_or_pos k with rfl | hkpos· simp [Real.log_nonneg]· exact Real.log_le_log (by positivity) hklehave h2 : Real.log 2 < 1 := byhave h := lam_lt_oneunfold lam at hexact hlinarithhave hkR : (3:ℝ) ≤ (k:ℝ) := by exact_mod_cast hk3-- V is at least k!have hfac : Nat.factorial k ≤ V := byhave h := factorial_le_prod (S := V.primeFactors)(fun p hp => (Nat.prime_of_mem_primeFactors hp).one_lt.le)rw [hcard, Nat.prod_primeFactors_of_squarefree hsq] at hexact h-- k! is at least (k/2)^(k/2)set h := k / 2 with hhhave hhk : h ≤ k := by omegahave hpow : h ^ (k - h) ≤ Nat.factorial k := byhave := Nat.factorial_mul_pow_sub_le_factorial (n := h) (m := k) hhkcalc h ^ (k - h) ≤ Nat.factorial h * h ^ (k - h) :=Nat.le_mul_of_pos_left _ (Nat.factorial_pos h)_ ≤ Nat.factorial k := thishave hple : h ^ h ≤ h ^ (k - h) := Nat.pow_le_pow_right (by omega) (by omega)have hVge : (h : ℝ) ^ h ≤ (V : ℝ) := byhave : h ^ h ≤ V := le_trans hple (le_trans hpow hfac)exact_mod_cast thishave hh0 : (1:ℝ) ≤ (h:ℝ) := byhave : 1 ≤ h := by omegaexact_mod_cast thishave hlogV : (h:ℝ) * Real.log h ≤ Real.log V := byhave h1 : Real.log ((h:ℝ) ^ h) ≤ Real.log V :=Real.log_le_log (by positivity) hVgerwa [Real.log_pow] at h1-- and (h : ℝ) ≥ k/3, log h ≥ log k - log 3have hhge : (k:ℝ) / 3 ≤ (h:ℝ) := byhave h3 : k ≤ 3 * h := by omegahave h4 : (k:ℝ) ≤ 3 * (h:ℝ) := by exact_mod_cast h3linarithhave hlog3 : Real.log 3 ≤ (12:ℝ)/10 := byhave he : (3:ℝ) ≤ Real.exp (12/10) := byhave h1 : Real.exp (12/10) = Real.exp 1 * Real.exp (2/10) := byrw [← Real.exp_add]; norm_numhave h2 : (1:ℝ) + 2/10 ≤ Real.exp (2/10) := byhave := Real.add_one_le_exp (2/10 : ℝ)linarithhave h3 : (2.7182818283:ℝ) < Real.exp 1 := Real.exp_one_gt_d9nlinarith [Real.exp_pos (2/10 : ℝ)]have := Real.log_le_log (by norm_num : (0:ℝ) < 3) herwa [Real.log_exp] at thishave hlogh : Real.log k - Real.log 3 ≤ Real.log h := byhave h1 : Real.log ((k:ℝ)/3) ≤ Real.log h :=Real.log_le_log (by positivity) hhgerwa [Real.log_div (by positivity) (by norm_num)] at h1have hlogh0 : 0 ≤ Real.log h := byhave : Real.log 1 ≤ Real.log h := Real.log_le_log (by norm_num) hh0simpa using thishave hfinal : (k:ℝ) / 3 * (Real.log k - 12/10) ≤ (h:ℝ) * Real.log h := byhave h1 : (k:ℝ)/3 * (Real.log k - 12/10) ≤ (k:ℝ)/3 * Real.log h := byhave : Real.log k - 12/10 ≤ Real.log h := by linarithexact mul_le_mul_of_nonneg_left this (by positivity)have h2 : (k:ℝ)/3 * Real.log h ≤ (h:ℝ) * Real.log h :=mul_le_mul_of_nonneg_right hhge hlogh0linarithhave hlast : (k:ℝ) * Real.log k / 12 ≤ (k:ℝ)/3 * (Real.log k - 12/10) := byhave hpos : (0:ℝ) ≤ (k:ℝ) := by linarithhave h1 : (0:ℝ) ≤ 3 * Real.log k - 48/10 := by linarithnlinarith [mul_nonneg hpos h1]linarith/-- The invariant maintained along the recursion. -/def Inv (k₀ pstar V : ℕ) : Prop :=0 < V ∧ Odd V ∧ Squarefree V ∧ ¬ pstar ∣ V ∧ k₀ ≤ V.primeFactors.card ∧∀ p ∈ V.primeFactors, p ≤ 4 * Qp V.primeFactors.card/-- One step of the recursion preserves the invariant and increases the cost by `4`. -/theorem inv_step {k₀ E H : ℕ} (hE4 : 4 ≤ E) (hk₀3 : 3 ≤ k₀)(hstep : ∀ k : ℕ, k₀ ≤ k → ∀ pstar : ℕ, pstar.Prime → Odd pstar →∀ V : ℕ, 0 < V → Odd V → Squarefree V → V.primeFactors.card = k →(∀ p ∈ V.primeFactors, p ≤ 4 * Qp k) → ∃ A, SelSpec pstar V A){pstar V : ℕ} (hp : pstar.Prime) (hpodd : Odd pstar) (hInv : Inv k₀ pstar V)(hRep : Rep (2 ^ E * V) H) :Inv k₀ pstar (V * stepA pstar V) ∧ Rep (2 ^ E * (V * stepA pstar V)) (H + 4) ∧(V * stepA pstar V).primeFactors.card= V.primeFactors.card + tp V.primeFactors.card ∧V * stepA pstar V ≤ V * (4 * Qp V.primeFactors.card) ^ tp V.primeFactors.card ∧V < V * stepA pstar V := byobtain ⟨hV0, hVodd, hVsq, hVnd, hVcard, hVpf⟩ := hInvset k := V.primeFactors.card with hkhave hkk₀ : k₀ ≤ k := hVcardhave hk3 : 3 ≤ k := le_trans hk₀3 hkk₀have hex := hstep k hkk₀ pstar hp hpodd V hV0 hVodd hVsq rfl hVpfobtain ⟨hA1, hAodd, hAsq, hAcop, hAnd, hAcard, hAprimes, hAres⟩ := stepA_spec hexset A := stepA pstar V with hAhave hA0 : 0 < A := by omegahave hVA0 : 0 < V * A := by positivityhave hdisj : Disjoint V.primeFactors A.primeFactors :=(Nat.Coprime.disjoint_primeFactors (Nat.Coprime.symm hAcop))have hpfmul : (V * A).primeFactors = V.primeFactors ∪ A.primeFactors :=Nat.primeFactors_mul (by omega) (by omega)have hcardmul : (V * A).primeFactors.card = k + tp k := byrw [hpfmul, Finset.card_union_of_disjoint hdisj, hAcard, ← hk]have hQmono : Qp k ≤ Qp (k + tp k) := Qp_mono hk3 (by omega)refine ⟨⟨hVA0, hVodd.mul hAodd, ?_, ?_, ?_, ?_⟩, ?_, hcardmul, ?_, ?_⟩· exact (Nat.squarefree_mul_iff).mpr ⟨Nat.Coprime.symm hAcop, hVsq, hAsq⟩· intro hdvdrcases (Nat.Prime.dvd_mul hp).mp hdvd with h | h· exact hVnd h· exact hAnd h· rw [hcardmul]; omega· intro q hqrw [hcardmul]rw [hpfmul, Finset.mem_union] at hqrcases hq with hq | hq· exact le_trans (hVpf q hq) (Nat.mul_le_mul le_rfl hQmono)· exact le_trans (hAprimes q hq).2 (Nat.mul_le_mul le_rfl hQmono)· have h := Rep.four_lift hV0 hVodd hE4 hRep hA1 hAodd hAcop hAreshave heq : 2 ^ E * V * A = 2 ^ E * (V * A) := by ringrwa [heq] at h· have hAle : A ≤ (4 * Qp k) ^ tp k := byhave h := self_le_pow_of_primeFactors_le (V := A) (M := 4 * Qp k) hAsq(fun q hq => (hAprimes q hq).2)rwa [hAcard] at hexact Nat.mul_le_mul_left V hAle· exact (Nat.lt_mul_iff_one_lt_right hV0).mpr hA1theorem Vseq_succ (pstar V₀ j : ℕ) :Vseq pstar V₀ (j + 1) = Vseq pstar V₀ j * stepA pstar (Vseq pstar V₀ j) := rfl/-- The properties of the recursive sequence `Vseq`. -/theorem Vseq_props {k₀ E H₀ : ℕ} (hE4 : 4 ≤ E) (hk₀3 : 3 ≤ k₀)(hstep : ∀ k : ℕ, k₀ ≤ k → ∀ pstar : ℕ, pstar.Prime → Odd pstar →∀ V : ℕ, 0 < V → Odd V → Squarefree V → V.primeFactors.card = k →(∀ p ∈ V.primeFactors, p ≤ 4 * Qp k) → ∃ A, SelSpec pstar V A){pstar V₀ : ℕ} (hp : pstar.Prime) (hpodd : Odd pstar) (hInv₀ : Inv k₀ pstar V₀)(hrep₀ : Rep (2 ^ E * V₀) H₀) :∀ j : ℕ, Inv k₀ pstar (Vseq pstar V₀ j) ∧ Rep (2 ^ E * Vseq pstar V₀ j) (H₀ + 4 * j) ∧(Vseq pstar V₀ (j + 1)).primeFactors.card= (Vseq pstar V₀ j).primeFactors.card + tp (Vseq pstar V₀ j).primeFactors.card ∧Vseq pstar V₀ (j + 1) ≤ Vseq pstar V₀ j *(4 * Qp (Vseq pstar V₀ j).primeFactors.card) ^ tp (Vseq pstar V₀ j).primeFactors.card ∧Vseq pstar V₀ j < Vseq pstar V₀ (j + 1) := byintro jinduction j with| zero =>obtain ⟨h1, h2, h3, h4, h5⟩ := inv_step hE4 hk₀3 hstep hp hpodd hInv₀ (H := H₀) hrep₀refine ⟨hInv₀, by simpa [Vseq] using hrep₀, ?_, ?_, ?_⟩· rw [Vseq_succ]; simpa [Vseq] using h3· rw [Vseq_succ]; simpa [Vseq] using h4· rw [Vseq_succ]; simpa [Vseq] using h5| succ j ih =>obtain ⟨hInv, hRep, -, -, -⟩ := ihobtain ⟨h1, h2, -, -, -⟩ := inv_step hE4 hk₀3 hstep hp hpodd hInv hRephave hs : Vseq pstar V₀ (j + 1) = Vseq pstar V₀ j * stepA pstar (Vseq pstar V₀ j) :=Vseq_succ _ _ _have hInv' : Inv k₀ pstar (Vseq pstar V₀ (j + 1)) := by rw [hs]; exact h1have hRep' : Rep (2 ^ E * Vseq pstar V₀ (j + 1)) (H₀ + 4 * (j + 1)) := byhave he : H₀ + 4 * (j + 1) = H₀ + 4 * j + 4 := by ringrw [he, hs]exact h2obtain ⟨-, -, g3, g4, g5⟩ := inv_step hE4 hk₀3 hstep hp hpodd hInv' hRep'refine ⟨hInv', hRep', ?_, ?_, ?_⟩· rw [Vseq_succ pstar V₀ (j + 1)]; exact g3· rw [Vseq_succ pstar V₀ (j + 1)]; exact g4· rw [Vseq_succ pstar V₀ (j + 1)]; exact g5/-- The modulus adjoined at one step is at most `2 ^ k`. -/theorem pow_step_le_two_pow {k : ℕ} (hk3 : 3 ≤ k) (hk : K1P k) :(4 * Qp k) ^ tp k ≤ 2 ^ k := byobtain ⟨-, -, -, -, -, -, -, hvii, -, -, -, -, -, -⟩ := hkhave hlogk : 1 ≤ Real.log k := one_le_log_of_three_le hk3have hloglog : 0 ≤ Real.log (Real.log k) := Real.log_nonneg hlogkhave hQ1 : 1 ≤ Qp k := Qp_pos khave hQ1R : (1:ℝ) ≤ (Qp k : ℝ) := by exact_mod_cast hQ1have hbase : (1:ℝ) ≤ 4 * (Qp k : ℝ) := by linarithhave hlogbase0 : 0 ≤ Real.log (4 * (Qp k : ℝ)) := Real.log_nonneg hbasehave hk0 : (0:ℝ) ≤ (k:ℝ) := by positivityhave h1 : (tp k : ℝ) * Real.log (4 * (Qp k : ℝ))≤ (ap k * k) * (7 * Real.log k + 3 * Real.log (Real.log k)) :=mul_le_mul (tp_le_ap_mul hk3) hvii hlogbase0 (mul_nonneg (ap_pos hk3).le hk0)have h2 : (ap k * k) * (7 * Real.log k + 3 * Real.log (Real.log k)) = (k:ℝ) * lam := byrw [mul_comm (ap k) ((k:ℝ)), mul_assoc, ap_mul_den hk3]rw [h2] at h1have h3 : (tp k : ℝ) * Real.log (4 * (Qp k : ℝ)) ≤ (k:ℝ) * Real.log 2 := bysimpa [lam] using h1have hL : Real.log ((4 * (Qp k : ℝ)) ^ tp k) ≤ Real.log ((2:ℝ) ^ k) := byrw [Real.log_pow, Real.log_pow]exact_mod_cast h3have hp1 : (0:ℝ) < (4 * (Qp k : ℝ)) ^ tp k := by positivityhave hp2 : (0:ℝ) < (2:ℝ) ^ k := by positivityhave hfin : (4 * (Qp k : ℝ)) ^ tp k ≤ (2:ℝ) ^ k := (Real.log_le_log_iff hp1 hp2).mp hLexact_mod_cast hfin/-- **A uniform representation bound**, the key analytic input for the three maintheorems: for every large real `x` and every odd prime `p` there is an integer`B ∈ [x, x²)` not divisible by `p`, exactly divisible by `2 ^ E`, such that every`m < B` is a sum of at most `H` distinct divisors of `B`, with `H + 1 ≤ c₀ (log log x)²`. -/theorem uniform_rep :∃ (E : ℕ) (x₀ : ℝ), 4 ≤ E ∧ Real.exp (Real.exp 1) < x₀ ∧∀ x : ℝ, x₀ ≤ x → ∀ p : ℕ, p.Prime → Odd p →∃ B H : ℕ, x ≤ (B : ℝ) ∧ (B : ℝ) < x ^ 2 ∧ 2 ^ E ∣ B ∧ ¬ (2 ^ (E + 1) ∣ B) ∧¬ (p ∣ B) ∧ Rep B H ∧ 1 ≤ H ∧(H : ℝ) + 1 ≤ c0 * (Real.log (Real.log x)) ^ 2 := byclassicalobtain ⟨k₀, E, H₀, hE4, hH₀1, hE2, hseed, hstep, hK1⟩ := exists_seedhave h2Epos : 0 < 2 ^ E := Nat.two_pow_pos Ehave hk₀3 : 3 ≤ k₀ := byby_contra hconpush_neg at hconhave h := (hK1 k₀ le_rfl).1have hkle : (k₀:ℝ) ≤ 2 := byhave : k₀ ≤ 2 := by omegaexact_mod_cast thishave hlogle : Real.log k₀ ≤ Real.log 2 := byrcases Nat.eq_zero_or_pos k₀ with rfl | hpos· simp [Real.log_nonneg]· exact Real.log_le_log (by positivity) hklehave h2 : Real.log 2 < 1 := byhave := lam_lt_oneunfold lam at thisexact thislinarithobtain ⟨U, hU1, hUabs⟩ := absorb c0 ((H₀:ℝ) + 8) (Real.log 24) c0_pos(Real.log_nonneg (by norm_num))set k₁ : ℕ := max k₀ ⌈Real.exp U⌉₊ with hk₁defset X1 : ℕ := 2 ^ E * (4 * Qp k₁) ^ k₁ with hX1defhave hX1nn : (0:ℝ) ≤ (X1:ℝ) := by positivityrefine ⟨E, Real.exp (Real.exp 1) + (X1:ℝ) + 1, hE4, by linarith, ?_⟩intro x hx p hp hpoddhave hexppos : (0:ℝ) < Real.exp (Real.exp 1) := Real.exp_pos _have hexp1 : (1:ℝ) ≤ Real.exp (Real.exp 1) := Real.one_le_exp (Real.exp_pos 1).lehave hxX1 : (X1:ℝ) < x := by linarithhave hxe : Real.exp (Real.exp 1) < x := by linarithhave hx1 : (1:ℝ) < x := by linarithobtain ⟨V₀, hV₀0, hV₀odd, hV₀sq, hV₀card, hV₀nd, hV₀primes, hV₀rep, hV₀le⟩ := hseed p hp hpoddhave hInv₀ : Inv k₀ p V₀ := byrefine ⟨hV₀0, hV₀odd, hV₀sq, hV₀nd, by rw [hV₀card], ?_⟩intro q hqrw [hV₀card]exact (hV₀primes q hq).2have hprops := Vseq_props hE4 hk₀3 hstep hp hpodd hInv₀ hV₀rephave hV0j : ∀ j, 0 < Vseq p V₀ j := fun j => (hprops j).1.1have hoddj : ∀ j, Odd (Vseq p V₀ j) := fun j => (hprops j).1.2.1have hsqj : ∀ j, Squarefree (Vseq p V₀ j) := fun j => (hprops j).1.2.2.1have hndj : ∀ j, ¬ p ∣ Vseq p V₀ j := fun j => (hprops j).1.2.2.2.1have hkkge : ∀ j, k₀ ≤ (Vseq p V₀ j).primeFactors.card := fun j => (hprops j).1.2.2.2.2.1have hpfj : ∀ j, ∀ q ∈ (Vseq p V₀ j).primeFactors,q ≤ 4 * Qp (Vseq p V₀ j).primeFactors.card := fun j => (hprops j).1.2.2.2.2.2have hcardstep : ∀ j, (Vseq p V₀ (j + 1)).primeFactors.card= (Vseq p V₀ j).primeFactors.card + tp (Vseq p V₀ j).primeFactors.card :=fun j => (hprops j).2.2.1have hsizestep : ∀ j, Vseq p V₀ (j + 1) ≤ Vseq p V₀ j *(4 * Qp (Vseq p V₀ j).primeFactors.card) ^ tp (Vseq p V₀ j).primeFactors.card :=fun j => (hprops j).2.2.2.1have hltj : ∀ j, Vseq p V₀ j < Vseq p V₀ (j + 1) := fun j => (hprops j).2.2.2.2have hk3j : ∀ j, 3 ≤ (Vseq p V₀ j).primeFactors.card := fun j => le_trans hk₀3 (hkkge j)-- the sequence grows at least linearlyhave hgrow : ∀ j, j + 1 ≤ Vseq p V₀ j := byintro jinduction j with| zero => have := hV0j 0; omega| succ j ih => have := hltj j; omega-- and at most quadraticallyhave hquad : ∀ j, 2 ^ E * Vseq p V₀ (j + 1) < (2 ^ E * Vseq p V₀ j) ^ 2 := byintro jhave h1 := hsizestep jhave h2 := pow_step_le_two_pow (hk3j j) (hK1 _ (hkkge j))have h3 : (2:ℕ) ^ (Vseq p V₀ j).primeFactors.card< 3 ^ (Vseq p V₀ j).primeFactors.card :=Nat.pow_lt_pow_left (by norm_num) (by have := hk3j j; omega)have h4 : (3:ℕ) ^ (Vseq p V₀ j).primeFactors.card ≤ Vseq p V₀ j :=three_pow_le_self (hoddj j) (hsqj j)have hVpos := hV0j jhave haV : (4 * Qp (Vseq p V₀ j).primeFactors.card)^ tp (Vseq p V₀ j).primeFactors.card < Vseq p V₀ j := by omegacalc 2 ^ E * Vseq p V₀ (j + 1)≤ 2 ^ E * (Vseq p V₀ j *(4 * Qp (Vseq p V₀ j).primeFactors.card) ^ tp (Vseq p V₀ j).primeFactors.card) :=Nat.mul_le_mul_left _ h1_ < 2 ^ E * (Vseq p V₀ j * Vseq p V₀ j) :=mul_lt_mul_of_pos_left (mul_lt_mul_of_pos_left haV hVpos) h2Epos_ ≤ (2 ^ E * Vseq p V₀ j) ^ 2 := byhave hrw : (2 ^ E * Vseq p V₀ j) ^ 2 = 2 ^ E * (2 ^ E * (Vseq p V₀ j * Vseq p V₀ j)) := byringrw [hrw]exact Nat.mul_le_mul_left _ (Nat.le_mul_of_pos_left _ h2Epos)-- the first index beyond `x`have hexists : ∃ j, x ≤ ((2 ^ E * Vseq p V₀ j : ℕ) : ℝ) := byrefine ⟨⌈x⌉₊, ?_⟩have h1 : x ≤ (⌈x⌉₊ : ℝ) := Nat.le_ceil xhave h2 : ⌈x⌉₊ + 1 ≤ Vseq p V₀ ⌈x⌉₊ := hgrow ⌈x⌉₊have h3 : Vseq p V₀ ⌈x⌉₊ ≤ 2 ^ E * Vseq p V₀ ⌈x⌉₊ := Nat.le_mul_of_pos_left _ h2Eposhave h4 : (⌈x⌉₊ : ℝ) ≤ ((2 ^ E * Vseq p V₀ ⌈x⌉₊ : ℕ) : ℝ) := byhave : ⌈x⌉₊ ≤ 2 ^ E * Vseq p V₀ ⌈x⌉₊ := by omegaexact_mod_cast thislinarithset j := Nat.find hexists with hjdefhave hxB : x ≤ ((2 ^ E * Vseq p V₀ j : ℕ) : ℝ) := Nat.find_spec hexists-- the seed is smallhave hQ4mono : ∀ m : ℕ, 3 ≤ m → m ≤ k₁ → (4 * Qp m) ^ m ≤ (4 * Qp k₁) ^ k₁ := byintro m hm3 hmk₁have hQ : Qp m ≤ Qp k₁ := Qp_mono hm3 hmk₁have hQ1 : 1 ≤ Qp k₁ := Qp_pos k₁calc (4 * Qp m) ^ m ≤ (4 * Qp k₁) ^ m := Nat.pow_le_pow_left (by omega) __ ≤ (4 * Qp k₁) ^ k₁ := Nat.pow_le_pow_right (by omega) hmk₁-- at the chosen index the number of prime factors is largehave hkkbig : k₁ < (Vseq p V₀ j).primeFactors.card := byby_contra hconpush_neg at hconhave h1 : Vseq p V₀ j ≤ (4 * Qp (Vseq p V₀ j).primeFactors.card)^ (Vseq p V₀ j).primeFactors.card :=self_le_pow_of_primeFactors_le (hsqj j) (hpfj j)have h2 := hQ4mono _ (hk3j j) hconhave h3 : 2 ^ E * Vseq p V₀ j ≤ X1 := byrw [hX1def]exact Nat.mul_le_mul_left _ (le_trans h1 h2)have h4 : ((2 ^ E * Vseq p V₀ j : ℕ) : ℝ) ≤ (X1:ℝ) := by exact_mod_cast h3linarith-- the chosen `B` is below `x ^ 2`have hBx2 : ((2 ^ E * Vseq p V₀ j : ℕ) : ℝ) < x ^ 2 := byrcases Nat.eq_zero_or_pos j with hj0 | hjpos· have hle : 2 ^ E * Vseq p V₀ 0 ≤ X1 := byhave h1 : (4 * Qp k₀) ^ k₀ ≤ (4 * Qp k₁) ^ k₁ := hQ4mono _ hk₀3 (le_max_left _ _)have h2 : 2 ^ E * V₀ ≤ X1 := byrw [hX1def]exact le_trans hV₀le (Nat.mul_le_mul_left _ h1)simpa [Vseq] using h2have h5 : ((2 ^ E * Vseq p V₀ 0 : ℕ) : ℝ) ≤ (X1:ℝ) := by exact_mod_cast hlerw [hj0]nlinarith· obtain ⟨m, hm⟩ : ∃ m, j = m + 1 := ⟨j - 1, by omega⟩have hmin : ¬ (x ≤ ((2 ^ E * Vseq p V₀ m : ℕ) : ℝ)) := Nat.find_min hexists (by omega)push_neg at hminhave hq := hquad mhave hqR : ((2 ^ E * Vseq p V₀ (m + 1) : ℕ) : ℝ)< ((2 ^ E * Vseq p V₀ m : ℕ) : ℝ) ^ 2 := by exact_mod_cast hqhave hnn : (0:ℝ) ≤ ((2 ^ E * Vseq p V₀ m : ℕ) : ℝ) := by positivityhave hlt2 : ((2 ^ E * Vseq p V₀ m : ℕ) : ℝ) ^ 2 < x ^ 2 := by nlinarithrw [hm]linarith-- the analytic estimateshave hU_le_u : U ≤ Real.log ((Vseq p V₀ j).primeFactors.card) := byhave h1 : Real.exp U ≤ (⌈Real.exp U⌉₊ : ℝ) := Nat.le_ceil _have h2 : ⌈Real.exp U⌉₊ ≤ k₁ := le_max_right _ _have h2R : ((⌈Real.exp U⌉₊ : ℕ) : ℝ) ≤ (k₁:ℝ) := by exact_mod_cast h2have h3 : (k₁:ℝ) < (((Vseq p V₀ j).primeFactors.card : ℕ) : ℝ) := by exact_mod_cast hkkbighave h4 : Real.exp U < (((Vseq p V₀ j).primeFactors.card : ℕ) : ℝ) := by linarithhave h5 := Real.log_lt_log (Real.exp_pos U) h4rw [Real.log_exp] at h5linarithhave hu1 : 1 ≤ Real.log ((Vseq p V₀ j).primeFactors.card) := le_trans hU1 hU_le_uhave hu3 : 3 ≤ Real.log ((Vseq p V₀ j).primeFactors.card) := (hK1 _ (hkkge j)).1have hjle : (j:ℝ) ≤ Fpot (Real.log ((Vseq p V₀ j).primeFactors.card))+ 2 * Real.log ((Vseq p V₀ j).primeFactors.card) :=step_count hK1 (fun i => (Vseq p V₀ i).primeFactors.card) (hkkge 0) hcardstep jhave hkjpos : (0:ℝ) < (((Vseq p V₀ j).primeFactors.card : ℕ) : ℝ) := byhave := hk3j jhave : (0:ℕ) < (Vseq p V₀ j).primeFactors.card := by omegaexact_mod_cast thishave hlogVlow : (((Vseq p V₀ j).primeFactors.card : ℕ) : ℝ)* Real.log ((Vseq p V₀ j).primeFactors.card) / 12 ≤ Real.log (Vseq p V₀ j) :=log_self_lower (hsqj j) rfl hu3have hlogB : Real.log (Vseq p V₀ j) ≤ Real.log ((2 ^ E * Vseq p V₀ j : ℕ) : ℝ) := byhave hle : (Vseq p V₀ j : ℝ) ≤ ((2 ^ E * Vseq p V₀ j : ℕ) : ℝ) := byhave : Vseq p V₀ j ≤ 2 ^ E * Vseq p V₀ j := Nat.le_mul_of_pos_left _ h2Eposexact_mod_cast thisexact Real.log_le_log (by exact_mod_cast hV0j j) hlehave hBlog : Real.log ((2 ^ E * Vseq p V₀ j : ℕ) : ℝ) < 2 * Real.log x := byhave hBpos : (0:ℝ) < ((2 ^ E * Vseq p V₀ j : ℕ) : ℝ) := byhave : 0 < 2 ^ E * Vseq p V₀ j := by have := hV0j j; positivityexact_mod_cast thishave h1 := Real.log_lt_log hBpos hBx2rwa [Real.log_pow] at h1have hlogx : (((Vseq p V₀ j).primeFactors.card : ℕ) : ℝ)* Real.log ((Vseq p V₀ j).primeFactors.card) / 24 < Real.log x := by linarithhave hT : Real.log ((Vseq p V₀ j).primeFactors.card)+ Real.log (Real.log ((Vseq p V₀ j).primeFactors.card)) - Real.log 24≤ Real.log (Real.log x) := byhave hpos : (0:ℝ) < (((Vseq p V₀ j).primeFactors.card : ℕ) : ℝ)* Real.log ((Vseq p V₀ j).primeFactors.card) / 24 := by positivityhave h1 := Real.log_le_log hpos hlogx.lehave h2 : Real.log ((((Vseq p V₀ j).primeFactors.card : ℕ) : ℝ)* Real.log ((Vseq p V₀ j).primeFactors.card) / 24)= Real.log ((Vseq p V₀ j).primeFactors.card)+ Real.log (Real.log ((Vseq p V₀ j).primeFactors.card)) - Real.log 24 := byrw [Real.log_div (by positivity) (by norm_num),Real.log_mul (by positivity) (by positivity)]linarith [h2 ▸ h1]refine ⟨2 ^ E * Vseq p V₀ j, H₀ + 4 * j, hxB, hBx2, ⟨Vseq p V₀ j, rfl⟩, ?_, ?_,(hprops j).2.1, by omega, ?_⟩· intro hdvdrw [pow_succ] at hdvdhave h2 : 2 ∣ Vseq p V₀ j := (Nat.mul_dvd_mul_iff_left h2Epos).mp hdvdhave hV := hoddj jrw [Nat.odd_iff] at hVomega· intro hdvdrcases (Nat.Prime.dvd_mul hp).mp hdvd with h | h· have hp2 : p ∣ 2 := Nat.Prime.dvd_of_dvd_pow hp hhave hpe : p = 2 := (Nat.prime_dvd_prime_iff_eq hp Nat.prime_two).mp hp2rw [hpe] at hpoddexact (by decide : ¬ Odd 2) hpodd· exact hndj j h· have habs := hUabs (Real.log ((Vseq p V₀ j).primeFactors.card)) (Real.log (Real.log x))hU_le_u hThave hlam : 0 < lam := lam_poshave hc0 : c0 = 14 / lam := rflhave hFpot : 4 * Fpot (Real.log ((Vseq p V₀ j).primeFactors.card))= c0 * (Real.log ((Vseq p V₀ j).primeFactors.card)) ^ 2+ (6 * c0 / 7) * (Real.log ((Vseq p V₀ j).primeFactors.card)* Real.log (Real.log ((Vseq p V₀ j).primeFactors.card))- Real.log ((Vseq p V₀ j).primeFactors.card)) := byunfold Fpotrw [hc0]field_simpringhave hH₀nn : (0:ℝ) ≤ (H₀:ℝ) := by positivitypush_castnlinarith [mul_nonneg hH₀nn(show (0:ℝ) ≤ Real.log ((Vseq p V₀ j).primeFactors.card) - 1 by linarith),mul_pos c0_pos (show (0:ℝ) < Real.log ((Vseq p V₀ j).primeFactors.card) by linarith)]/-! ## Unit fractions and prescribed denominators -//-- Dividing by a divisor: `1 / (M / d) = d / M`. -/theorem one_div_div_eq {M d : ℕ} (hM : 0 < M) (hd : d ∣ M) :(1 : ℚ) / ((M / d : ℕ) : ℚ) = (d : ℚ) / (M : ℚ) := byobtain ⟨q, rfl⟩ := hdhave hd0 : d ≠ 0 := by rintro rfl; simp at hMhave hq0 : q ≠ 0 := by rintro rfl; simp at hMrw [Nat.mul_div_cancel_left _ (Nat.pos_of_ne_zero hd0)]push_castfield_simp/-- The map `d ↦ M / d` is injective on divisors of `M`. -/theorem div_injOn {M : ℕ} (hM : 0 < M) :Set.InjOn (fun d => M / d) {d : ℕ | d ∣ M} := byintro x hx y hy hsimp only [Set.mem_setOf_eq] at hx hyobtain ⟨a, ha⟩ := hxobtain ⟨b, hb⟩ := hyhave hx0 : 0 < x := Nat.pos_of_dvd_of_pos ⟨a, ha⟩ hMhave hy0 : 0 < y := Nat.pos_of_dvd_of_pos ⟨b, hb⟩ hMsimp only at hhave h1 : M / x * x = M := Nat.div_mul_cancel ⟨a, ha⟩have h2 : M / y * y = M := Nat.div_mul_cancel ⟨b, hb⟩have hM0 : 0 < M / x := Nat.div_pos (Nat.le_of_dvd hM ⟨a, ha⟩) hx0rw [h] at h1nlinarith [h1, h2]/-- Quotient and remainder: a short unit-fraction expansion of `a / b`. -/theorem quotient_remainder {B H a b : ℕ} (hb : 2 ≤ b) (hbB : b ≤ B) (hab : a < b)(hRep : Rep B H) :∃ F₁ F₂ : Finset ℕ, Disjoint F₁ F₂ ∧ (∀ d ∈ F₁, 1 < d ∧ d ∣ B ∧ d ≤ B) ∧(∀ e ∈ F₂, B < e) ∧ F₁.card ≤ H ∧ F₂.card ≤ H ∧(∑ d ∈ F₁, (1:ℚ)/d) + (∑ e ∈ F₂, (1:ℚ)/e) = (a:ℚ)/(b:ℚ) ∧(a = b - 1 → ¬ (b ∣ B) → b ∉ F₁ ∧ b ∉ F₂ ∧ F₂.Nonempty) := byclassicalhave hB : 1 ≤ B := hRep.1have hb0 : 0 < b := by omegaset q := a * B / b with hqdefset r := a * B % b with hrdefhave hqr : b * q + r = a * B := Nat.div_add_mod (a * B) bhave hrb : r < b := Nat.mod_lt _ hb0have hqB : q < B := byby_contra hconpush_neg at hconhave h1 : b * B ≤ b * q := Nat.mul_le_mul_left b hconhave h2 : a * B < b * B := byexact Nat.mul_lt_mul_of_lt_of_le hab (le_refl B) (by omega)omegaobtain ⟨D₁, hD₁, hcard₁, hsum₁⟩ := hRep.2 q hqBobtain ⟨D₂, hD₂, hcard₂, hsum₂⟩ := hRep.2 r (lt_of_lt_of_le hrb hbB)have hd₁dvd : ∀ d ∈ D₁, d ∣ B ∧ 0 < d := fun d hd => byhave := Nat.mem_divisors.mp (hD₁ hd); exact ⟨this.1, Nat.pos_of_mem_divisors (hD₁ hd)⟩have hd₂dvd : ∀ e ∈ D₂, e ∣ B ∧ 0 < e := fun e he => byhave := Nat.mem_divisors.mp (hD₂ he); exact ⟨this.1, Nat.pos_of_mem_divisors (hD₂ he)⟩have hd₁le : ∀ d ∈ D₁, d ≤ q := fun d hd => byrw [← hsum₁]; exact Finset.single_le_sum (f := fun x => x) (by simp) hdhave hd₂le : ∀ e ∈ D₂, e ≤ r := fun e he => byrw [← hsum₂]; exact Finset.single_le_sum (f := fun x => x) (by simp) hehave hinj₁ : Set.InjOn (fun d => B / d) ↑D₁ := byintro x hx y hy hexact div_injOn (show 0 < B by omega) (hd₁dvd x (by simpa using hx)).1(hd₁dvd y (by simpa using hy)).1 hhave hinj₂ : Set.InjOn (fun e => b * B / e) ↑D₂ := byintro x hx y hy hrefine div_injOn (M := b * B) (by positivity) ?_ ?_ h· exact Dvd.dvd.mul_left (hd₂dvd x (by simpa using hx)).1 b· exact Dvd.dvd.mul_left (hd₂dvd y (by simpa using hy)).1 brefine ⟨D₁.image (fun d => B / d), D₂.image (fun e => b * B / e), ?_, ?_, ?_, ?_, ?_, ?_, ?_⟩· -- disjointnessrw [Finset.disjoint_left]intro x hx hx'obtain ⟨d, hd, rfl⟩ := Finset.mem_image.mp hxobtain ⟨e, he, heq⟩ := Finset.mem_image.mp hx'have h1 : B / d ≤ B := Nat.div_le_self B dhave h2 : B < b * B / e := byobtain ⟨hedvd, hepos⟩ := hd₂dvd e hehave hBe : e * (B / e) = B := Nat.mul_div_cancel' hedvdhave hBe' : 0 < B / e := Nat.div_pos (Nat.le_of_dvd (by omega) hedvd) heposhave : b * B / e = b * (B / e) := byrw [Nat.mul_div_assoc b hedvd]rw [this]calc B = e * (B / e) := hBe.symm_ < b * (B / e) := byapply Nat.mul_lt_mul_of_lt_of_le _ (le_refl _) hBe'exact lt_of_le_of_lt (hd₂le e he) hrbomega· -- members of F₁intro x hxobtain ⟨d, hd, rfl⟩ := Finset.mem_image.mp hxobtain ⟨hdvd, hpos⟩ := hd₁dvd d hdrefine ⟨?_, Nat.div_dvd_of_dvd hdvd, Nat.div_le_self B d⟩have hdq : d ≤ q := hd₁le d hdhave hdB : d < B := by omegahave hcancel : d * (B / d) = B := Nat.mul_div_cancel' hdvdhave hstep : d * 1 < d * (B / d) := by omegaexact lt_of_mul_lt_mul_left hstep (Nat.zero_le d)· -- members of F₂intro y hyobtain ⟨e, he, rfl⟩ := Finset.mem_image.mp hyobtain ⟨hedvd, hepos⟩ := hd₂dvd e hehave hBe : e * (B / e) = B := Nat.mul_div_cancel' hedvdhave hBe' : 0 < B / e := Nat.div_pos (Nat.le_of_dvd (by omega) hedvd) heposhave heq : b * B / e = b * (B / e) := Nat.mul_div_assoc b hedvdrw [heq]calc B = e * (B / e) := hBe.symm_ < b * (B / e) := byapply Nat.mul_lt_mul_of_lt_of_le _ (le_refl _) hBe'exact lt_of_le_of_lt (hd₂le e he) hrb· exact le_trans (Finset.card_image_le) hcard₁· exact le_trans (Finset.card_image_le) hcard₂· -- the sum identityrw [Finset.sum_image hinj₁, Finset.sum_image hinj₂]have e₁ : ∀ d ∈ D₁, (1:ℚ)/((B / d : ℕ) : ℚ) = (d : ℚ)/(B : ℚ) := byintro d hdexact one_div_div_eq (by omega) (hd₁dvd d hd).1have e₂ : ∀ e ∈ D₂, (1:ℚ)/((b * B / e : ℕ) : ℚ) = (e : ℚ)/((b * B : ℕ) : ℚ) := byintro e heexact one_div_div_eq (by positivity) (Dvd.dvd.mul_left (hd₂dvd e he).1 b)rw [Finset.sum_congr rfl e₁, Finset.sum_congr rfl e₂, ← Finset.sum_div, ← Finset.sum_div]have c₁ : ((∑ d ∈ D₁, d : ℕ) : ℚ) = (q : ℚ) := by exact_mod_cast hsum₁have c₂ : ((∑ e ∈ D₂, e : ℕ) : ℚ) = (r : ℚ) := by exact_mod_cast hsum₂push_cast at c₁ c₂ ⊢rw [c₁, c₂]have hB0 : (B:ℚ) ≠ 0 := by positivityhave hb0' : (b:ℚ) ≠ 0 := by positivityhave hcast : (b:ℚ) * (q:ℚ) + (r:ℚ) = (a:ℚ) * (B:ℚ) := by exact_mod_cast hqrfield_simpnlinarith [hcast]· -- the final assertionsintro hab' hbB'refine ⟨?_, ?_, ?_⟩· intro hmemobtain ⟨d, hd, heq⟩ := Finset.mem_image.mp hmemexact hbB' (heq ▸ Nat.div_dvd_of_dvd (hd₁dvd d hd).1)· intro hmemobtain ⟨e, he, heq⟩ := Finset.mem_image.mp hmemobtain ⟨hedvd, hepos⟩ := hd₂dvd e hehave hBe : e * (B / e) = B := Nat.mul_div_cancel' hedvdhave hBe' : 0 < B / e := Nat.div_pos (Nat.le_of_dvd (by omega) hedvd) heposhave heq2 : b * B / e = b * (B / e) := Nat.mul_div_assoc b hedvdhave : B < b * B / e := byrw [heq2]calc B = e * (B / e) := hBe.symm_ < b * (B / e) := byapply Nat.mul_lt_mul_of_lt_of_le _ (le_refl _) hBe'exact lt_of_le_of_lt (hd₂le e he) hrbomega· have hrpos : 0 < r := byrcases Nat.eq_zero_or_pos r with h0 | h; swap· exact hexfalsohave hdvd : b ∣ a * B := byhave : a * B = b * q := by omegaexact ⟨q, this⟩have : b ∣ B := byhave h1 : b ∣ b * B := Dvd.intro B rflhave h2 : a * B + B = b * B := byhave : a + 1 = b := by omegacalc a * B + B = (a + 1) * B := by ring_ = b * B := by rw [this]have : b ∣ a * B + B := h2 ▸ h1exact (Nat.dvd_add_right hdvd).mp thisexact hbB' thishave hne : D₂.Nonempty := byrcases Finset.eq_empty_or_nonempty D₂ with rfl | h· simp at hsum₂; omega· exact hexact hne.image _/-- If `b ≥ 2` has no odd prime divisor then it is a power of two. -/theorem eq_two_pow_of_no_odd_prime {b : ℕ} (hb : 2 ≤ b)(h : ∀ p : ℕ, p.Prime → Odd p → ¬ p ∣ b) : ∃ r : ℕ, 1 ≤ r ∧ b = 2 ^ r := byhave hb0 : b ≠ 0 := by omegahave key : ∀ {d : ℕ}, Nat.Prime d → d ∣ b → d = 2 := byintro d hd hdvdby_contra hneexact h d hd (hd.odd_of_ne_two hne) hdvdhave hpow := Nat.eq_prime_pow_of_unique_prime_dvd hb0 keyrefine ⟨b.primeFactorsList.length, ?_, hpow⟩by_contra hconhave : b.primeFactorsList.length = 0 := by omegarw [this] at hpowsimp at hpowomega/-- Avoiding a prescribed integer `b`. -/theorem avoid_b : ∃ b₁ : ℕ, Real.exp (Real.exp 1) < b₁ ∧ 2 ≤ b₁ ∧∀ b : ℕ, b₁ ≤ b → ∃ B H : ℕ, b ≤ B ∧ 1 ≤ H ∧ ¬ (b ∣ B) ∧ Rep B H ∧(H : ℝ) + 1 ≤ c0 * (Real.log (Real.log b)) ^ 2 := byobtain ⟨E, x₀, hE, hx₀, hmain⟩ := uniform_repobtain ⟨b₁, hb₁x, hb₁E⟩ : ∃ n : ℕ, x₀ < (n : ℝ) ∧ (2:ℕ) ^ E < n := byobtain ⟨n, hn⟩ := exists_nat_gt (max x₀ ((2 : ℝ) ^ E))refine ⟨n, lt_of_le_of_lt (le_max_left _ _) hn, ?_⟩have := lt_of_le_of_lt (le_max_right x₀ ((2:ℝ)^E)) hnexact_mod_cast thishave h2E : (16:ℕ) ≤ 2 ^ E := bycalc (16:ℕ) = 2 ^ 4 := by norm_num_ ≤ 2 ^ E := Nat.pow_le_pow_right (by norm_num) hErefine ⟨b₁, lt_trans hx₀ hb₁x, by omega, ?_⟩intro b hbhave hbx : x₀ ≤ (b : ℝ) := le_of_lt (lt_of_lt_of_le hb₁x (by exact_mod_cast hb))by_cases hodd : ∃ p : ℕ, p.Prime ∧ Odd p ∧ p ∣ b· obtain ⟨p, hp, hpodd, hpb⟩ := hoddobtain ⟨B, H, hxB, hBx2, hdvdE, hndvdE, hnp, hRep, hH1, hcost⟩ :=hmain (b : ℝ) hbx p hp hpoddrefine ⟨B, H, by exact_mod_cast hxB, hH1, ?_, hRep, hcost⟩intro hconexact hnp (dvd_trans hpb hcon)· push_neg at hoddhave hb2 : 2 ≤ b := by omegaobtain ⟨r, hr1, hrb⟩ := eq_two_pow_of_no_odd_prime hb2 (fun p hp hpo => hodd p hp hpo)have hrE : E + 1 ≤ r := byby_contra hconpush_neg at hconhave : b ≤ 2 ^ E := byrw [hrb]exact Nat.pow_le_pow_right (by norm_num) (by omega)omegaobtain ⟨B, H, hxB, hBx2, hdvdE, hndvdE, hnp, hRep, hH1, hcost⟩ :=hmain (b : ℝ) hbx 3 (by norm_num) (by decide)refine ⟨B, H, by exact_mod_cast hxB, hH1, ?_, hRep, hcost⟩intro hconapply hndvdErefine dvd_trans ?_ hconrw [hrb]exact pow_dvd_pow 2 hrE/-- A short representation of `1` containing a prescribed large denominator. -/theorem large_denominator {b₁ : ℕ} (hb₁2 : 2 ≤ b₁)(H₁ : ∀ b : ℕ, b₁ ≤ b → ∃ B H : ℕ, b ≤ B ∧ 1 ≤ H ∧ ¬ (b ∣ B) ∧ Rep B H ∧(H : ℝ) + 1 ≤ c0 * (Real.log (Real.log b)) ^ 2){b : ℕ} (hb : b₁ ≤ b) :∃ F : Finset ℕ, (∀ d ∈ F, 0 < d) ∧ (∑ d ∈ F, (1:ℚ)/d) = 1 ∧ b ∈ F ∧(∃ d ∈ F, b < d) ∧ (F.card : ℝ) ≤ 2 * c0 * (Real.log (Real.log b)) ^ 2 := byclassicalobtain ⟨B, H, hbB, hH1, hnd, hRep, hcost⟩ := H₁ b hbhave hb2 : 2 ≤ b := le_trans hb₁2 hbobtain ⟨F₁, F₂, hdisj, hF₁, hF₂, hc₁, hc₂, hsum, hextra⟩ :=quotient_remainder hb2 hbB (show b - 1 < b by omega) hRepobtain ⟨hnb₁, hnb₂, hne₂⟩ := hextra rfl hndhave hbnot : b ∉ F₁ ∪ F₂ := bysimp only [Finset.mem_union, not_or]exact ⟨hnb₁, hnb₂⟩refine ⟨insert b (F₁ ∪ F₂), ?_, ?_, Finset.mem_insert_self _ _, ?_, ?_⟩· intro x hxrcases Finset.mem_insert.mp hx with rfl | hx· omega· rcases Finset.mem_union.mp hx with hx | hx· exact lt_trans Nat.zero_lt_one (hF₁ x hx).1· have := hF₂ x hxomega· rw [Finset.sum_insert hbnot, Finset.sum_union hdisj, hsum]have hb0 : (b:ℚ) ≠ 0 := byhave : 0 < b := by omegapositivityhave hcast : ((b - 1 : ℕ) : ℚ) = (b : ℚ) - 1 := byhave h1 : (1:ℕ) ≤ b := by omegapush_cast [Nat.cast_sub h1]ringrw [hcast]field_simpring· obtain ⟨e, he⟩ := hne₂exact ⟨e, Finset.mem_insert_of_mem (Finset.mem_union_right _ he),lt_of_le_of_lt hbB (hF₂ e he)⟩· have hcard : (insert b (F₁ ∪ F₂)).card ≤ 2 * H + 1 := byrw [Finset.card_insert_of_notMem hbnot]have := Finset.card_union_le F₁ F₂omegahave h1 : ((insert b (F₁ ∪ F₂)).card : ℝ) ≤ 2 * (H : ℝ) + 1 := byexact_mod_cast hcardhave h2 : 2 * (H : ℝ) + 1 ≤ 2 * ((H : ℝ) + 1) := by linarithcalc ((insert b (F₁ ∪ F₂)).card : ℝ) ≤ 2 * ((H : ℝ) + 1) := le_trans h1 h2_ ≤ 2 * (c0 * (Real.log (Real.log b)) ^ 2) := by linarith_ = 2 * c0 * (Real.log (Real.log b)) ^ 2 := by ring/-- Increasing the number of terms by one. -/theorem split_one {F : Finset ℕ} {b : ℕ} (hb : 2 ≤ b) (hpos : ∀ d ∈ F, 0 < d)(hsum : (∑ d ∈ F, (1:ℚ)/d) = 1) (hbF : b ∈ F) (hbig : ∃ d ∈ F, b < d) :∃ F' : Finset ℕ, (∀ d ∈ F', 0 < d) ∧ (∑ d ∈ F', (1:ℚ)/d) = 1 ∧F'.card = F.card + 1 ∧ b ∈ F' ∧ (∃ d ∈ F', b < d) := byclassicalobtain ⟨e, heF, hbe⟩ := hbighave hne : F.Nonempty := ⟨b, hbF⟩set d := F.max' hne with hdhave hdF : d ∈ F := F.max'_mem hnehave hbd : b < d := lt_of_lt_of_le hbe (F.le_max' e heF)have hd2 : 2 ≤ d := by omegahave hdmax : ∀ x ∈ F, x ≤ d := fun x hx => F.le_max' x hxhave hlt1 : d < d + 1 := by omegahave hlt2 : d + 1 < d * (d + 1) := by nlinarithhave hnotmem1 : d + 1 ∉ insert (d * (d + 1)) (F.erase d) := bysimp only [Finset.mem_insert, Finset.mem_erase, not_or]refine ⟨by omega, ?_⟩rintro ⟨-, hmem⟩exact absurd (hdmax _ hmem) (by omega)have hnotmem2 : d * (d + 1) ∉ F.erase d := byintro hmemexact absurd (hdmax _ (Finset.mem_of_mem_erase hmem)) (by nlinarith)refine ⟨insert (d + 1) (insert (d * (d + 1)) (F.erase d)), ?_, ?_, ?_, ?_, ?_⟩· intro x hxsimp only [Finset.mem_insert, Finset.mem_erase] at hxrcases hx with rfl | rfl | ⟨-, hx⟩· omega· positivity· exact hpos x hx· rw [Finset.sum_insert hnotmem1, Finset.sum_insert hnotmem2]have hd0 : (d:ℚ) ≠ 0 := byhave : 0 < d := by omegapositivityhave hFsum : (∑ x ∈ F.erase d, (1:ℚ)/x) = 1 - 1/(d:ℚ) := byhave h := Finset.sum_erase_add F (fun x => (1:ℚ)/(x:ℚ)) hdFrw [hsum] at hlinarithpush_castrw [hFsum]field_simpring· rw [Finset.card_insert_of_notMem hnotmem1, Finset.card_insert_of_notMem hnotmem2,Finset.card_erase_of_mem hdF]have : 1 ≤ F.card := Finset.card_pos.mpr hneomega· simp only [Finset.mem_insert, Finset.mem_erase]exact Or.inr (Or.inr ⟨by omega, hbF⟩)· exact ⟨d + 1, by simp, by omega⟩/-- Every larger length is attainable. -/theorem padding {F : Finset ℕ} {b : ℕ} (hb : 2 ≤ b) (hpos : ∀ d ∈ F, 0 < d)(hsum : (∑ d ∈ F, (1:ℚ)/d) = 1) (hbF : b ∈ F) (hbig : ∃ d ∈ F, b < d){k : ℕ} (hk : F.card ≤ k) :∃ G : Finset ℕ, (∀ d ∈ G, 0 < d) ∧ (∑ d ∈ G, (1:ℚ)/d) = 1 ∧ G.card = k ∧ b ∈ G ∧(∃ d ∈ G, b < d) := byclassicalobtain ⟨j, hj⟩ : ∃ j, k = F.card + j := ⟨k - F.card, by omega⟩subst hjclear hkinduction j generalizing F with| zero => exact ⟨F, hpos, hsum, rfl, hbF, hbig⟩| succ j ih =>obtain ⟨F', hpos', hsum', hcard', hbF', hbig'⟩ := split_one hb hpos hsum hbF hbigobtain ⟨G, hG1, hG2, hG3, hG4, hG5⟩ := ih hpos' hsum' hbF' hbig'exact ⟨G, hG1, hG2, by omega, hG4, hG5⟩/-- The telescoping identity `∑_{r=1}^{n} 1/(r(r+1)) = 1 - 1/(n+1)`. -/theorem sum_telescope (n : ℕ) :∑ r ∈ Finset.Icc 1 n, (1:ℚ)/((r:ℚ)*((r:ℚ)+1)) = 1 - 1/((n:ℚ)+1) := byinduction n with| zero => simp| succ n ih =>rw [Finset.sum_Icc_succ_top (by omega), ih]have h1 : ((n:ℚ) + 1) ≠ 0 := by positivityhave h2 : ((n:ℚ) + 1 + 1) ≠ 0 := by positivitypush_castfield_simpring/-- A representation of `1` containing any fixed denominator `b ≥ 2`, of length at most `b+1`. -/theorem fixed_denominator {b : ℕ} (hb : 2 ≤ b) :∃ F : Finset ℕ, (∀ d ∈ F, 0 < d) ∧ (∑ d ∈ F, (1:ℚ)/d) = 1 ∧ b ∈ F ∧(∃ d ∈ F, b < d) ∧ F.card ≤ b + 1 := byclassicalset G : Finset ℕ := (Finset.Icc 1 (b-1)).image (fun r => r * (r+1)) with hGdefhave hinj : Set.InjOn (fun r => r * (r+1)) ↑(Finset.Icc 1 (b-1)) := byintro x _ y _ hsimp only at hnlinarithhave hcardG : G.card = b - 1 := byrw [hGdef, Finset.card_image_of_injOn hinj, Nat.card_Icc]omegahave hmemG : ∀ d ∈ G, ∃ r, 1 ≤ r ∧ r ≤ b - 1 ∧ d = r * (r+1) := byintro d hdrw [hGdef, Finset.mem_image] at hdobtain ⟨r, hr, rfl⟩ := hdrw [Finset.mem_Icc] at hrexact ⟨r, hr.1, hr.2, rfl⟩have hposG : ∀ d ∈ G, 0 < d := byintro d hdobtain ⟨r, hr1, -, rfl⟩ := hmemG d hdpositivityhave hleG : ∀ d ∈ G, d ≤ (b-1) * b := byintro d hdobtain ⟨r, hr1, hr2, rfl⟩ := hmemG d hdhave : r + 1 ≤ b := by omegaexact Nat.mul_le_mul hr2 thishave hevenG : ∀ d ∈ G, Even d := byintro d hdobtain ⟨r, -, -, rfl⟩ := hmemG d hdexact Nat.even_mul_succ_self rhave hsumG : (∑ d ∈ G, (1:ℚ)/d) = 1 - 1/(b:ℚ) := byrw [hGdef, Finset.sum_image hinj]have := sum_telescope (b-1)have hcast : ∀ r ∈ Finset.Icc 1 (b-1), (1:ℚ)/((r * (r+1) : ℕ) : ℚ)= (1:ℚ)/((r:ℚ)*((r:ℚ)+1)) := byintro r _push_castringrw [Finset.sum_congr rfl hcast, this]have : ((b - 1 : ℕ) : ℚ) + 1 = (b : ℚ) := byhave : ((b - 1 : ℕ) : ℚ) = (b : ℚ) - 1 := byhave : (1:ℕ) ≤ b := by omegapush_cast [Nat.cast_sub this]ringrw [this]; ringrw [this]by_cases hbG : b ∈ G· -- `b` already occurs; adjoin `b+1` and `b(b+1)`have hb1 : b + 1 ∉ G := byintro hmemhave h1 := hevenG _ hmemhave h2 := hevenG _ hbGrw [Nat.even_add_one] at h1exact h1 h2have hb2 : b * (b+1) ∉ insert (b+1) G := bysimp only [Finset.mem_insert, not_or]constructor· nlinarith· intro hmemhave h1 := hleG _ hmemhave h2 : (b-1) * b ≤ b * b := Nat.mul_le_mul_right b (Nat.sub_le b 1)have h3 : b * b < b * (b+1) := by nlinarith [show 0 < b by omega]omegarefine ⟨insert (b * (b+1)) (insert (b+1) G), ?_, ?_, ?_, ?_, ?_⟩· intro x hxsimp only [Finset.mem_insert] at hxrcases hx with rfl | rfl | hx· positivity· omega· exact hposG x hx· rw [Finset.sum_insert hb2, Finset.sum_insert hb1, hsumG]have hb0 : (b:ℚ) ≠ 0 := byhave : 0 < b := by omegapositivityhave hb10 : ((b:ℚ) + 1) ≠ 0 := by positivitypush_castfield_simpring· simp only [Finset.mem_insert]exact Or.inr (Or.inr hbG)· exact ⟨b * (b+1), by simp, by nlinarith⟩· rw [Finset.card_insert_of_notMem hb2, Finset.card_insert_of_notMem hb1, hcardG]omega· -- `b` does not occur; adjoin ithave hb3 : 3 ≤ b := byrcases Nat.lt_or_ge b 3 with h | h· exfalsohave hb2' : b = 2 := by omegaapply hbGrw [hGdef, Finset.mem_image]exact ⟨1, by simp [hb2'], by omega⟩· exact hrefine ⟨insert b G, ?_, ?_, ?_, ?_, ?_⟩· intro x hxsimp only [Finset.mem_insert] at hxrcases hx with rfl | hx· omega· exact hposG x hx· rw [Finset.sum_insert hbG, hsumG]ring· simp· refine ⟨(b-1) * b, ?_, ?_⟩· simp only [Finset.mem_insert]refine Or.inr ?_rw [hGdef, Finset.mem_image]refine ⟨b - 1, by simp [Finset.mem_Icc]; omega, ?_⟩congr 1omega· nlinarith [Nat.sub_add_cancel (show 1 ≤ b by omega)]· rw [Finset.card_insert_of_notMem hbG, hcardG]omega/-! ## The three main theorems -//-- **First main theorem.** There are infinitely many natural numbers `n` such that every integer`1 ≤ m ≤ n` is a sum of at most `c₀ (log log n)^2` distinct positive divisors of `n`. -/theorem infinitely_many_divisor_representable :∀ N : ℕ, ∃ n : ℕ, N < n ∧ ∀ m : ℕ, 1 ≤ m → m ≤ n →∃ F ⊆ n.divisors, (F.card : ℝ) ≤ c0 * (Real.log (Real.log n)) ^ 2 ∧∑ d ∈ F, d = m := byintro Nobtain ⟨E, x₀, hE, hx₀, hmain⟩ := uniform_repobtain ⟨x, hxN, hxx₀⟩ : ∃ x : ℝ, (N : ℝ) < x ∧ x₀ ≤ x := byobtain ⟨n, hn⟩ := exists_nat_gt (max (N : ℝ) x₀)exact ⟨n, lt_of_le_of_lt (le_max_left _ _) hn,le_of_lt (lt_of_le_of_lt (le_max_right _ _) hn)⟩obtain ⟨B, H, hxB, hBx2, hdvdE, hndvdE, hnp, hRep, hH1, hcost⟩ :=hmain x hxx₀ 3 (by norm_num) (by decide)have hxe : Real.exp (Real.exp 1) < x := lt_of_lt_of_le hx₀ hxx₀have hNB : N < B := byhave : (N : ℝ) < (B : ℝ) := lt_of_lt_of_le hxN hxBexact_mod_cast thisrefine ⟨B, hNB, ?_⟩intro m hm1 hmBhave hmono : Real.log (Real.log x) ≤ Real.log (Real.log B) := loglog_le_loglog hxe hxBhave hloglog1 : 1 < Real.log (Real.log x) := one_lt_loglog hxehave hsq : c0 * (Real.log (Real.log x)) ^ 2 ≤ c0 * (Real.log (Real.log B)) ^ 2 := byhave h0 : (0:ℝ) ≤ Real.log (Real.log x) := le_of_lt (lt_trans zero_lt_one hloglog1)have := pow_le_pow_left₀ h0 hmono 2nlinarith [c0_pos, this]have hT : (H : ℝ) ≤ c0 * (Real.log (Real.log B)) ^ 2 := by linarithhave hT1 : (1:ℝ) ≤ c0 * (Real.log (Real.log B)) ^ 2 := byhave : (1:ℝ) ≤ (H : ℝ) := by exact_mod_cast hH1linarithexact hRep.all_le hT hT1 hm1 hmB/-- **Second main theorem.** There is a `b₀` such that every rational `a / b ∈ (0,1)` with `b ≥ b₀`is a sum of at most `2 c₀ (log log b)^2` distinct unit fractions. -/theorem exists_short_egyptian_fraction :∃ b₀ : ℕ, ∀ b a : ℕ, b₀ ≤ b → 0 < a → a < b →∃ A : Finset ℕ, (∀ d ∈ A, 0 < d) ∧(A.card : ℝ) ≤ 2 * c0 * (Real.log (Real.log b)) ^ 2 ∧∑ d ∈ A, (1 : ℚ) / d = (a : ℚ) / (b : ℚ) := byclassicalobtain ⟨E, x₀, hE, hx₀, hmain⟩ := uniform_repobtain ⟨b₀, hb₀⟩ := exists_nat_gt (max x₀ 2)refine ⟨b₀, ?_⟩intro b a hb ha habhave hb₀x : x₀ < (b₀ : ℝ) := lt_of_le_of_lt (le_max_left _ _) hb₀have hb₀2 : (2:ℝ) < (b₀ : ℝ) := lt_of_le_of_lt (le_max_right _ _) hb₀have hb2 : 2 ≤ b := byhave : (2:ℝ) < (b:ℝ) := lt_of_lt_of_le hb₀2 (by exact_mod_cast hb)exact_mod_cast le_of_lt thishave hbx : x₀ ≤ (b : ℝ) := le_of_lt (lt_of_lt_of_le hb₀x (by exact_mod_cast hb))obtain ⟨B, H, hxB, hBx2, hdvdE, hndvdE, hnp, hRep, hH1, hcost⟩ :=hmain (b : ℝ) hbx 3 (by norm_num) (by decide)have hbB : b ≤ B := by exact_mod_cast hxBobtain ⟨F₁, F₂, hdisj, hF₁, hF₂, hc₁, hc₂, hsum, -⟩ :=quotient_remainder hb2 hbB hab hReprefine ⟨F₁ ∪ F₂, ?_, ?_, ?_⟩· intro x hxrcases Finset.mem_union.mp hx with hx | hx· exact lt_trans Nat.zero_lt_one (hF₁ x hx).1· have := hF₂ x hxhave : 1 ≤ B := hRep.1omega· have hcard : (F₁ ∪ F₂).card ≤ 2 * H := byhave := Finset.card_union_le F₁ F₂omegahave h1 : ((F₁ ∪ F₂).card : ℝ) ≤ 2 * (H : ℝ) := by exact_mod_cast hcardcalc ((F₁ ∪ F₂).card : ℝ) ≤ 2 * (H : ℝ) := h1_ ≤ 2 * (c0 * (Real.log (Real.log b)) ^ 2) := by linarith_ = 2 * c0 * (Real.log (Real.log b)) ^ 2 := by ring· rw [Finset.sum_union hdisj]exact hsum/-- **Third main theorem.** There is a `k₀` such that for every `k ≥ k₀` and every integer`2 ≤ b ≤ exp (exp (sqrt (k / (2 c₀))))` there is a set `A` of exactly `k` positive integerswith `b ∈ A` and `∑_{n ∈ A} 1/n = 1`. -/theorem exists_egyptian_with_prescribed_denominator :∃ k₀ : ℕ, ∀ k : ℕ, k₀ ≤ k → ∀ b : ℕ, 2 ≤ b →(b : ℝ) ≤ Real.exp (Real.exp (Real.sqrt ((k : ℝ) / (2 * c0)))) →∃ A : Finset ℕ, A.card = k ∧ (∀ d ∈ A, 0 < d) ∧ b ∈ A ∧∑ d ∈ A, (1 : ℚ) / d = 1 := byclassicalobtain ⟨b₁, hb₁e, hb₁2, hb₁main⟩ := avoid_brefine ⟨b₁, ?_⟩intro k hk b hb2 hboundhave hc0 : 0 < c0 := c0_pos-- produce a representation of `1` containing `b` and some larger integerhave key : ∃ F : Finset ℕ, (∀ d ∈ F, 0 < d) ∧ (∑ d ∈ F, (1:ℚ)/d) = 1 ∧ b ∈ F ∧(∃ d ∈ F, b < d) ∧ F.card ≤ k := byby_cases hbig : b₁ ≤ b· obtain ⟨F, hpos, hsum, hbF, hbig', hcard⟩ := large_denominator hb₁2 hb₁main hbigrefine ⟨F, hpos, hsum, hbF, hbig', ?_⟩-- the size boundhave hbe : Real.exp (Real.exp 1) < (b : ℝ) :=lt_of_lt_of_le hb₁e (by exact_mod_cast hbig)have hll1 : 1 < Real.log (Real.log b) := one_lt_loglog hbehave hsqrt : Real.log (Real.log b) ≤ Real.sqrt ((k : ℝ) / (2 * c0)) := byhave hb0 : (0:ℝ) < b := by positivityhave h1 : Real.log b ≤ Real.exp (Real.sqrt ((k : ℝ) / (2 * c0))) := byhave := Real.log_le_log hb0 hboundrwa [Real.log_exp] at thishave hlogb0 : 0 < Real.log b := byhave : (1:ℝ) < (b:ℝ) := by exact_mod_cast hb2.trans_lt' one_lt_twoexact Real.log_pos thishave := Real.log_le_log hlogb0 h1rwa [Real.log_exp] at thishave hnn : (0:ℝ) ≤ (k : ℝ) / (2 * c0) := by positivityhave hsq : (Real.log (Real.log b)) ^ 2 ≤ (k : ℝ) / (2 * c0) := byhave h0 : (0:ℝ) ≤ Real.log (Real.log b) := le_of_lt (lt_trans zero_lt_one hll1)have := pow_le_pow_left₀ h0 hsqrt 2rwa [Real.sq_sqrt hnn] at thishave hfinal : (F.card : ℝ) ≤ (k : ℝ) := bycalc (F.card : ℝ) ≤ 2 * c0 * (Real.log (Real.log b)) ^ 2 := hcard_ ≤ 2 * c0 * ((k : ℝ) / (2 * c0)) := byhave : (0:ℝ) < 2 * c0 := by linarithexact mul_le_mul_of_nonneg_left hsq (le_of_lt this)_ = (k : ℝ) := by field_simpexact_mod_cast hfinal· push_neg at hbigobtain ⟨F, hpos, hsum, hbF, hbig', hcard⟩ := fixed_denominator hb2exact ⟨F, hpos, hsum, hbF, hbig', by omega⟩obtain ⟨F, hpos, hsum, hbF, hbig', hcard⟩ := keyobtain ⟨G, hG1, hG2, hG3, hG4, -⟩ := padding hb2 hpos hsum hbF hbig' hcardexact ⟨G, hG3, hG1, hG4, hG2⟩#print axioms infinitely_many_divisor_representable#print axioms exists_short_egyptian_fraction#print axioms exists_egyptian_with_prescribed_denominatorend SDS