This journal is in preparation. Submissions are open as a pilot and the editorial board is being formed. Articles marked Demonstration show the format only.
Journal of Machine-Verified MathematicsPeer-reviewed mathematics with machine-checked proofs, for the age of AI

E424/Tools/T10Aux.lean

All files · 570 lines · source at the verified commit

1module
2
3public import E424.ToolDefs
4
5@[expose] public section
6
7/-!
8# Erdős 424: helpers for tool T10 (Mertens). Mathlib only.
9
10Chain. `Pn n = ∑_{p ≤ n} log p / p = log n + O(1)` from Legendre's formula and Chebyshev's
11`θ x ≤ log 4 · x`. Abel summation with `f t = (log t)⁻¹` then gives
12`∑_{u<p≤v} 1/p = log (log v / log u) + O(1 / log u)`. The product bounds follow from
13`1 + x ≤ exp x` applied to each factor.
14-/
15
16set_option linter.style.header false
17open Finset MeasureTheory
18
19namespace E424.T10Aux
20
21/-- The weight `log k / k` at primes, `0` elsewhere. -/
22noncomputable def cw (k : ℕ) : ℝ := if k.Prime then Real.log k / k else 0
23
24/-- `Pn n = ∑_{k ≤ n} cw k = ∑_{p ≤ n} log p / p`. -/
25noncomputable def Pn (n : ℕ) : ℝ := ∑ k ∈ Icc 0 n, cw k
26
27/-- Sum of `8 k^{-7/4}`, which bounds `∑_p log p / (p (p-1))`. -/
28noncomputable def K0 : ℝ := ∑' k : ℕ, 8 * ((k : ℝ) ^ ((7 : ℝ) / 4))⁻¹
29
30lemma K0_nonneg : 0 ≤ K0 :=
31 tsum_nonneg fun k => mul_nonneg (by norm_num) (inv_nonneg.mpr (Real.rpow_nonneg (Nat.cast_nonneg k) _))
32
33lemma Pn_eq_filter (n : ℕ) : Pn n = ∑ p ∈ (Icc 0 n).filter Nat.Prime, Real.log p / p := by
34 unfold Pn cw
35 rw [Finset.sum_filter]
36
37/-! ## Factorials -/
38
39lemma log_factorial_ge (n : ℕ) : (n : ℝ) * Real.log n - n ≤ Real.log n.factorial := by
40 rcases Nat.eq_zero_or_pos n with rfl | hn
41 · simp
42 have hn' : (0 : ℝ) < n := by exact_mod_cast hn
43 have hf : (0 : ℝ) < n.factorial := by exact_mod_cast n.factorial_pos
44 have h := Real.pow_div_factorial_le_exp (n : ℝ) (Nat.cast_nonneg n) n
45 have hl := Real.log_le_log (div_pos (pow_pos hn' n) hf) h
46 rw [Real.log_div (pow_pos hn' n).ne' hf.ne', Real.log_pow, Real.log_exp] at hl
47 linarith
48
49lemma log_factorial_le (n : ℕ) : Real.log n.factorial ≤ n * Real.log n := by
50 rcases Nat.eq_zero_or_pos n with rfl | hn
51 · simp
52 have hn' : (0 : ℝ) < n := by exact_mod_cast hn
53 have hf : (0 : ℝ) < n.factorial := by exact_mod_cast n.factorial_pos
54 have h : (n.factorial : ℝ) ≤ (n : ℝ) ^ n := by exact_mod_cast Nat.factorial_le_pow n
55 have := Real.log_le_log hf h
56 rwa [Real.log_pow] at this
57
58lemma log_factorial_eq (n : ℕ) : Real.log n.factorial =
59 ∑ p ∈ n.factorial.primeFactors, (n.factorial.factorization p : ℝ) * Real.log p := by
60 conv_lhs => rw [Real.log_nat_eq_sum_factorization]
61 simp [Finsupp.sum, Nat.support_factorization]
62
63/-- `p ≤ n` gives `⌊n/p⌋ ≤ v_p(n!)`. -/
64lemma nat_div_le_factorization {p n : ℕ} (hp : p.Prime) :
65 n / p ≤ n.factorial.factorization p := by
66 rw [Nat.factorization_factorial hp (n := n) (b := Nat.log p n + 2) (by omega)]
67 have h1 : 1 ∈ Ico 1 (Nat.log p n + 2) := by simp
68 have := Finset.single_le_sum (f := fun i => n / p ^ i) (fun i _ => Nat.zero_le _) h1
69 simpa using this
70
71lemma primes_subset_primeFactors (n : ℕ) :
72 (Icc 0 n).filter Nat.Prime ⊆ n.factorial.primeFactors := by
73 intro p hp
74 rw [Finset.mem_filter, Finset.mem_Icc] at hp
75 rw [Nat.mem_primeFactors]
76 exact ⟨hp.2, (hp.2.dvd_factorial).mpr hp.1.2, n.factorial_ne_zero⟩
77
78lemma primeFactors_subset_primes (n : ℕ) :
79 n.factorial.primeFactors ⊆ (Icc 0 n).filter Nat.Prime := by
80 intro p hp
81 rw [Nat.mem_primeFactors] at hp
82 obtain ⟨hp, hdvd, -⟩ := hp
83 rw [Finset.mem_filter, Finset.mem_Icc]
84 exact ⟨⟨Nat.zero_le _, (hp.dvd_factorial).mp hdvd⟩, hp⟩
85
86/-! ## Discrete Mertens bounds for `Pn` -/
87
88/-- Lower bound on `log n!` from `v_p(n!) ≥ ⌊n/p⌋`. -/
89lemma weighted_le_log_factorial (n : ℕ) :
90 ∑ p ∈ (Icc 0 n).filter Nat.Prime, ((n : ℝ) / p - 1) * Real.log p ≤ Real.log n.factorial := by
91 rw [log_factorial_eq]
92 refine le_trans ?_ (Finset.sum_le_sum_of_subset_of_nonneg (primes_subset_primeFactors n) ?_)
93 · apply Finset.sum_le_sum
94 intro p hp
95 rw [Finset.mem_filter, Finset.mem_Icc] at hp
96 have h1 : ((n / p : ℕ) : ℝ) ≤ (n.factorial.factorization p : ℝ) := by
97 exact_mod_cast nat_div_le_factorization hp.2
98 have h2 : (n : ℝ) / p - 1 ≤ ((n / p : ℕ) : ℝ) := by
99 have hp0 : (0 : ℝ) < p := by exact_mod_cast hp.2.pos
100 have hm := Nat.mod_lt n hp.2.pos
101 have : (n : ℝ) < (p : ℝ) * (n / p : ℕ) + p := by
102 have hd := Nat.div_add_mod n p
103 have : n < p * (n / p) + p := by omega
104 exact_mod_cast this
105 rw [div_sub_one hp0.ne', div_le_iff₀ hp0]
106 nlinarith
107 have h3 : 0 ≤ Real.log p := Real.log_natCast_nonneg p
108 exact mul_le_mul_of_nonneg_right (h2.trans h1) h3
109 · intro p _ _
110 exact mul_nonneg (Nat.cast_nonneg _) (Real.log_natCast_nonneg p)
111
112/-- `Pn n ≤ log n + log 4`. -/
113lemma Pn_le (n : ℕ) : Pn n ≤ Real.log n + Real.log 4 := by
114 rcases Nat.eq_zero_or_pos n with rfl | hn
115 · simp [Pn, cw]
116 exact Real.log_nonneg (by norm_num)
117 have hn' : (0 : ℝ) < n := by exact_mod_cast hn
118 have h1 := weighted_le_log_factorial n
119 have h2 := log_factorial_le n
120 have h3 := Chebyshev.theta_le_log4_mul_x (x := (n : ℝ)) hn'.le
121 have htheta : ∑ p ∈ (Icc 0 n).filter Nat.Prime, Real.log p = Chebyshev.theta n := by
122 rw [Chebyshev.theta_eq_sum_Icc, Nat.floor_natCast]
123 have hsplit : ∑ p ∈ (Icc 0 n).filter Nat.Prime, ((n : ℝ) / p - 1) * Real.log p
124 = n * Pn n - Chebyshev.theta n := by
125 rw [← htheta, Pn_eq_filter, Finset.mul_sum, ← Finset.sum_sub_distrib]
126 apply Finset.sum_congr rfl
127 intro p _
128 ring
129 rw [hsplit] at h1
130 have h4 : (n : ℝ) * Pn n ≤ (n : ℝ) * (Real.log n + Real.log 4) := by
131 rw [mul_add]
132 rw [mul_comm (Real.log 4)] at h3
133 linarith
134 exact le_of_mul_le_mul_left h4 hn'
135
136lemma log_div_le_rpow (k : ℕ) (hk : 2 ≤ k) :
137 Real.log k / ((k : ℝ) * ((k : ℝ) - 1)) ≤ 8 * ((k : ℝ) ^ ((7 : ℝ) / 4))⁻¹ := by
138 have hk' : (2 : ℝ) ≤ k := by exact_mod_cast hk
139 have hpos : (0 : ℝ) < k := by linarith
140 have hden : (0 : ℝ) < k * ((k : ℝ) - 1) := mul_pos hpos (by linarith)
141 have hr : 0 < (k : ℝ) ^ ((7 : ℝ) / 4) := Real.rpow_pos_of_pos hpos _
142 have hlog : Real.log k ≤ 4 * (k : ℝ) ^ ((1 : ℝ) / 4) := by
143 have h := Real.log_le_rpow_div hpos.le (by norm_num : (0 : ℝ) < 1 / 4)
144 rwa [show (k : ℝ) ^ ((1 : ℝ) / 4) / ((1 : ℝ) / 4) = 4 * (k : ℝ) ^ ((1 : ℝ) / 4) by ring]
145 at h
146 have hmul : (k : ℝ) ^ ((7 : ℝ) / 4) * (k : ℝ) ^ ((1 : ℝ) / 4) = (k : ℝ) ^ 2 := by
147 rw [← Real.rpow_add hpos, show (7 : ℝ) / 4 + 1 / 4 = 2 by norm_num, Real.rpow_two]
148 have key : Real.log k * (k : ℝ) ^ ((7 : ℝ) / 4) ≤ 8 * (k * ((k : ℝ) - 1)) := by
149 calc Real.log k * (k : ℝ) ^ ((7 : ℝ) / 4)
150 ≤ 4 * (k : ℝ) ^ ((1 : ℝ) / 4) * (k : ℝ) ^ ((7 : ℝ) / 4) :=
151 mul_le_mul_of_nonneg_right hlog hr.le
152 _ = 4 * ((k : ℝ) ^ ((7 : ℝ) / 4) * (k : ℝ) ^ ((1 : ℝ) / 4)) := by ring
153 _ = 4 * (k : ℝ) ^ 2 := by rw [hmul]
154 _ ≤ 8 * (k * ((k : ℝ) - 1)) := by nlinarith
155 rw [div_le_iff₀ hden]
156 have e : 8 * ((k : ℝ) ^ ((7 : ℝ) / 4))⁻¹ * (k * ((k : ℝ) - 1))
157 = (8 * (k * ((k : ℝ) - 1))) / (k : ℝ) ^ ((7 : ℝ) / 4) := by ring
158 rw [e, le_div_iff₀ hr]
159 exact key
160
161/-- `∑_{p ≤ n} log p / (p (p-1)) ≤ K0`. -/
162lemma sum_log_div_le (n : ℕ) :
163 ∑ p ∈ (Icc 0 n).filter Nat.Prime, Real.log p / ((p : ℝ) * ((p : ℝ) - 1)) ≤ K0 := by
164 have hsum : Summable (fun k : ℕ => 8 * ((k : ℝ) ^ ((7 : ℝ) / 4))⁻¹) :=
165 (Real.summable_nat_rpow_inv.mpr (by norm_num)).mul_left 8
166 have hnn : ∀ k : ℕ, 0 ≤ 8 * ((k : ℝ) ^ ((7 : ℝ) / 4))⁻¹ := fun k => by positivity
167 calc ∑ p ∈ (Icc 0 n).filter Nat.Prime, Real.log p / ((p : ℝ) * ((p : ℝ) - 1))
168 ≤ ∑ p ∈ (Icc 0 n).filter Nat.Prime, 8 * ((p : ℝ) ^ ((7 : ℝ) / 4))⁻¹ := by
169 apply Finset.sum_le_sum
170 intro p hp
171 exact log_div_le_rpow p (Nat.Prime.two_le (Finset.mem_filter.mp hp).2)
172 _ ≤ ∑ p ∈ Icc 0 n, 8 * ((p : ℝ) ^ ((7 : ℝ) / 4))⁻¹ :=
173 Finset.sum_le_sum_of_subset_of_nonneg (Finset.filter_subset _ _) (fun k _ _ => hnn k)
174 _ ≤ K0 := hsum.sum_le_tsum _ (fun k _ => hnn k)
175
176/-- `log n! ≤ n P(n) + n K0` via Legendre's `v_p(n!) ≤ n/(p-1)`. -/
177lemma log_factorial_le_Pn (n : ℕ) : Real.log n.factorial ≤ n * Pn n + n * K0 := by
178 rw [log_factorial_eq]
179 calc ∑ p ∈ n.factorial.primeFactors, (n.factorial.factorization p : ℝ) * Real.log p
180 ≤ ∑ p ∈ (Icc 0 n).filter Nat.Prime, (n.factorial.factorization p : ℝ) * Real.log p :=
181 Finset.sum_le_sum_of_subset_of_nonneg (primeFactors_subset_primes n)
182 (fun p _ _ => mul_nonneg (Nat.cast_nonneg _) (Real.log_natCast_nonneg p))
183 _ ≤ ∑ p ∈ (Icc 0 n).filter Nat.Prime,
184 (n : ℝ) * (Real.log p / p + Real.log p / ((p : ℝ) * ((p : ℝ) - 1))) := by
185 apply Finset.sum_le_sum
186 intro p hp
187 have hpp := (Finset.mem_filter.mp hp).2
188 have hp2 : (2 : ℝ) ≤ p := by exact_mod_cast hpp.two_le
189 have hp0 : (p : ℝ) ≠ 0 := by positivity
190 have hp1 : (p : ℝ) - 1 ≠ 0 := by intro h; linarith
191 have hcast : ((p - 1 : ℕ) : ℝ) = (p : ℝ) - 1 := by
192 rw [Nat.cast_sub hpp.one_le, Nat.cast_one]
193 have hle : (n.factorial.factorization p : ℝ) ≤ (n : ℝ) / ((p : ℝ) - 1) := by
194 have h := Nat.factorization_factorial_le_div_pred hpp n
195 calc (n.factorial.factorization p : ℝ) ≤ ((n / (p - 1) : ℕ) : ℝ) := by exact_mod_cast h
196 _ ≤ (n : ℝ) / ((p - 1 : ℕ) : ℝ) := Nat.cast_div_le
197 _ = (n : ℝ) / ((p : ℝ) - 1) := by rw [hcast]
198 have hid : (n : ℝ) * (Real.log p / p + Real.log p / ((p : ℝ) * ((p : ℝ) - 1)))
199 = (n : ℝ) / ((p : ℝ) - 1) * Real.log p := by
200 field_simp
201 ring
202 rw [hid]
203 exact mul_le_mul_of_nonneg_right hle (Real.log_natCast_nonneg p)
204 _ = n * Pn n + n * ∑ p ∈ (Icc 0 n).filter Nat.Prime,
205 Real.log p / ((p : ℝ) * ((p : ℝ) - 1)) := by
206 rw [Pn_eq_filter]
207 simp only [mul_add, Finset.sum_add_distrib, Finset.mul_sum]
208 _ ≤ n * Pn n + n * K0 := by
209 have := sum_log_div_le n
210 gcongr
211
212/-- Lower bound `log n - 1 - K0 ≤ Pn n` from `n log n - n ≤ log n!` and `log n! ≤ n Pn n + n K0`. -/
213lemma log_sub_le_Pn (n : ℕ) : Real.log n - 1 - K0 ≤ Pn n := by
214 rcases Nat.eq_zero_or_pos n with rfl | hn
215 · simp [Pn, cw]
216 linarith [K0_nonneg]
217 have hn' : (0 : ℝ) < n := by exact_mod_cast hn
218 have h1 := log_factorial_ge n
219 have h2 := log_factorial_le_Pn n
220 have h3 : (n : ℝ) * (Real.log n - 1 - K0) = n * Real.log n - n - n * K0 := by ring
221 have h4 : (n : ℝ) * (Real.log n - 1 - K0) ≤ n * Pn n := by linarith
222 exact le_of_mul_le_mul_left h4 hn'
223
224/-- `|Pn n - log n| ≤ log 4 + 1 + |K0|` for every `n`. -/
225lemma Pn_abs_le (n : ℕ) : |Pn n - Real.log n| ≤ Real.log 4 + 1 + |K0| := by
226 have h1 := Pn_le n
227 have h2 := log_sub_le_Pn n
228 have h4 : 0 ≤ Real.log 4 := Real.log_nonneg (by norm_num)
229 rw [abs_le]
230 constructor <;> linarith [neg_abs_le K0, le_abs_self K0, abs_nonneg K0]
231
232/-! ## Pn bounded on the real line -/
233
234/-- `|Pn ⌊t⌋ - log t| ≤ K` for all `t ≥ 1`. -/
235lemma Pn_floor_bound : ∃ K, 0 < K ∧ ∀ t : ℝ, 1 ≤ t → |Pn ⌊t⌋₊ - Real.log t| ≤ K := by
236 have h4 : 0 ≤ Real.log 4 := Real.log_nonneg (by norm_num)
237 have h2 : 0 ≤ Real.log 2 := Real.log_nonneg (by norm_num)
238 refine ⟨Real.log 4 + 1 + |K0| + Real.log 2, by linarith [abs_nonneg K0], fun t ht => ?_⟩
239 have hf : 1 ≤ ⌊t⌋₊ := Nat.le_floor (by simpa using ht)
240 have hfl : (⌊t⌋₊ : ℝ) ≤ t := Nat.floor_le (by linarith)
241 have hlt : t < ⌊t⌋₊ + 1 := Nat.lt_floor_add_one t
242 have hfpos : (1 : ℝ) ≤ ⌊t⌋₊ := by exact_mod_cast hf
243 have hlog1 : Real.log ⌊t⌋₊ ≤ Real.log t := Real.log_le_log (by linarith) hfl
244 have hlog2 : Real.log t ≤ Real.log ⌊t⌋₊ + Real.log 2 := by
245 have h2f : t ≤ 2 * (⌊t⌋₊ : ℝ) := by linarith
246 have hpos : (0 : ℝ) < ⌊t⌋₊ := by linarith
247 have := Real.log_le_log (by linarith) h2f
248 rw [Real.log_mul (by norm_num) hpos.ne'] at this
249 linarith
250 have h1 := Pn_abs_le ⌊t⌋₊
251 rw [abs_le] at h1 ⊢
252 constructor <;> linarith [h1.1, h1.2]
253
254/-! ## Abel summation -/
255
256lemma hasDerivAt_inv_log {t : ℝ} (ht : 1 < t) :
257 HasDerivAt (fun s => (Real.log s)⁻¹) (-t⁻¹ / (Real.log t) ^ 2) t :=
258 (Real.hasDerivAt_log (by linarith : (0 : ℝ) < t).ne').inv (Real.log_pos ht).ne'
259
260lemma continuousOn_inv_log_Ici {u : ℝ} (hu : 2 ≤ u) : ContinuousOn Real.log (Set.Ici u) := by
261 intro t ht
262 have ht' : u ≤ t := ht
263 have h0 : t ≠ 0 := by intro h; linarith
264 exact (Real.continuousAt_log h0).continuousWithinAt
265
266/-- The derivative `-t⁻¹ / (log t)^2` of `(log t)⁻¹`, continuous on `[u, ∞)`. -/
267lemma continuousOn_g {u : ℝ} (hu : 2 ≤ u) :
268 ContinuousOn (fun t : ℝ => -t⁻¹ / (Real.log t) ^ 2) (Set.Ici u) := by
269 intro t ht
270 have ht' : u ≤ t := ht
271 have h0 : t ≠ 0 := by intro h; linarith
272 have hl : Real.log t ≠ 0 := (Real.log_pos (by linarith)).ne'
273 apply ContinuousAt.continuousWithinAt
274 exact ((continuousAt_inv₀ h0).neg).div ((Real.continuousAt_log h0).pow 2) (pow_ne_zero 2 hl)
275
276lemma integral_g (u v : ℝ) (hu : 2 ≤ u) (huv : u ≤ v) :
277 ∫ t in Set.Ioc u v, -t⁻¹ / (Real.log t) ^ 2 = (Real.log v)⁻¹ - (Real.log u)⁻¹ := by
278 have hint : IntervalIntegrable (fun t : ℝ => -t⁻¹ / (Real.log t) ^ 2) MeasureTheory.volume u v :=
279 ((continuousOn_g hu).mono (by rw [Set.uIcc_of_le huv]; exact Set.Icc_subset_Ici_self)).intervalIntegrable
280 rw [← intervalIntegral.integral_of_le huv,
281 intervalIntegral.integral_eq_sub_of_hasDerivAt (f := fun s => (Real.log s)⁻¹)
282 (f' := fun t => -t⁻¹ / (Real.log t) ^ 2)
283 (fun x hx => hasDerivAt_inv_log (by rw [Set.uIcc_of_le huv] at hx; linarith [hx.1])) hint]
284
285lemma integral_log_part (u v : ℝ) (hu : 2 ≤ u) (huv : u ≤ v) :
286 ∫ t in Set.Ioc u v, (-t⁻¹ / (Real.log t) ^ 2) * Real.log t =
287 -(Real.log (Real.log v) - Real.log (Real.log u)) := by
288 have hderiv : ∀ x ∈ Set.uIcc u v, HasDerivAt (fun s => -Real.log (Real.log s))
289 ((-x⁻¹ / (Real.log x) ^ 2) * Real.log x) x := by
290 intro x hx
291 rw [Set.uIcc_of_le huv] at hx
292 have hx1 : 1 < x := by linarith [hx.1]
293 have hlx : Real.log x ≠ 0 := (Real.log_pos hx1).ne'
294 have hx0 : x ≠ 0 := (by linarith : (0 : ℝ) < x).ne'
295 have h : HasDerivAt (fun y => -Real.log (Real.log y)) (-(x⁻¹ / Real.log x)) x :=
296 ((Real.hasDerivAt_log hx0).log hlx).neg
297 exact h.congr_deriv (by field_simp)
298 have hint : IntervalIntegrable (fun t : ℝ => (-t⁻¹ / (Real.log t) ^ 2) * Real.log t)
299 MeasureTheory.volume u v := by
300 apply ContinuousOn.intervalIntegrable
301 rw [Set.uIcc_of_le huv]
302 exact ((continuousOn_g hu).mono Set.Icc_subset_Ici_self).mul
303 ((continuousOn_inv_log_Ici hu).mono Set.Icc_subset_Ici_self)
304 rw [← intervalIntegral.integral_of_le huv,
305 intervalIntegral.integral_eq_sub_of_hasDerivAt (f := fun s => -Real.log (Real.log s))
306 (f' := fun t => (-t⁻¹ / (Real.log t) ^ 2) * Real.log t) hderiv hint]
307 ring
308
309/-- The error term of Abel summation: `|∫ g (Pn - log)| ≤ K / log u`. -/
310lemma integral_err_le {K : ℝ} (u v : ℝ) (hu : 2 ≤ u) (huv : u ≤ v)
311 (hK : ∀ t : ℝ, 1 ≤ t → |Pn ⌊t⌋₊ - Real.log t| ≤ K) :
312 |∫ t in Set.Ioc u v, (-t⁻¹ / (Real.log t) ^ 2) * (Pn ⌊t⌋₊ - Real.log t)| ≤
313 K * (Real.log u)⁻¹ := by
314 have hK0 : 0 ≤ K := (abs_nonneg _).trans (hK 1 le_rfl)
315 have hlu : 0 < Real.log u := Real.log_pos (by linarith)
316 have hlv : Real.log u ≤ Real.log v := Real.log_le_log (by linarith) huv
317 have hlv0 : 0 < Real.log v := lt_of_lt_of_le hlu hlv
318 have hgI : IntegrableOn (fun t : ℝ => -t⁻¹ / (Real.log t) ^ 2) (Set.Ioc u v) :=
319 ((continuousOn_g hu).mono Set.Icc_subset_Ici_self).integrableOn_Icc.mono_set
320 Set.Ioc_subset_Icc_self
321 have hbound_int : IntegrableOn (fun t : ℝ => K * -(-t⁻¹ / (Real.log t) ^ 2)) (Set.Ioc u v) :=
322 (hgI.neg).const_mul K
323 have hae : ∀ᵐ t ∂(MeasureTheory.volume.restrict (Set.Ioc u v)),
324 ‖(-t⁻¹ / (Real.log t) ^ 2) * (Pn ⌊t⌋₊ - Real.log t)‖ ≤ K * -(-t⁻¹ / (Real.log t) ^ 2) := by
325 rw [MeasureTheory.ae_restrict_iff' measurableSet_Ioc]
326 refine Filter.Eventually.of_forall fun t ht => ?_
327 have ht0 : 0 < t := by linarith [ht.1]
328 have ht1 : 1 < t := by linarith [ht.1]
329 have hgnn : 0 ≤ -(-t⁻¹ / (Real.log t) ^ 2) := by
330 rw [neg_div, neg_neg]
331 exact div_nonneg (inv_nonneg.mpr ht0.le) (sq_nonneg _)
332 have hgabs : |-t⁻¹ / (Real.log t) ^ 2| = -(-t⁻¹ / (Real.log t) ^ 2) :=
333 abs_of_nonpos (by linarith)
334 rw [Real.norm_eq_abs, abs_mul, hgabs, mul_comm K]
335 exact mul_le_mul_of_nonneg_left (hK t ht1.le) hgnn
336 calc |∫ t in Set.Ioc u v, (-t⁻¹ / (Real.log t) ^ 2) * (Pn ⌊t⌋₊ - Real.log t)|
337 = ‖∫ t in Set.Ioc u v, (-t⁻¹ / (Real.log t) ^ 2) * (Pn ⌊t⌋₊ - Real.log t)‖ :=
338 (Real.norm_eq_abs _).symm
339 _ ≤ ∫ t in Set.Ioc u v, K * -(-t⁻¹ / (Real.log t) ^ 2) :=
340 MeasureTheory.norm_integral_le_of_norm_le hbound_int hae
341 _ = K * ((Real.log u)⁻¹ - (Real.log v)⁻¹) := by
342 rw [MeasureTheory.integral_const_mul, MeasureTheory.integral_neg, integral_g u v hu huv]
343 ring
344 _ ≤ K * (Real.log u)⁻¹ :=
345 mul_le_mul_of_nonneg_left (by linarith [inv_nonneg.mpr hlv0.le]) hK0
346
347/-- The Abel weights `(log k)⁻¹ * cw k` over `(⌊u⌋, ⌊v⌋]` are the primes' `1/p`. -/
348lemma sum_Ioc_inv_eq (u v : ℝ) :
349 ∑ k ∈ Ioc ⌊u⌋₊ ⌊v⌋₊, (Real.log k)⁻¹ * cw k = ∑ p ∈ primesIoc u v, 1 / (p : ℝ) := by
350 unfold primesIoc
351 rw [Finset.sum_filter]
352 apply Finset.sum_congr rfl
353 intro k _
354 unfold cw
355 split_ifs with hk
356 · have h2 : (2 : ℝ) ≤ k := by exact_mod_cast hk.two_le
357 have hl : Real.log k ≠ 0 := (Real.log_pos (by linarith)).ne'
358 have hk0 : (k : ℝ) ≠ 0 := by intro h; linarith
359 rw [← mul_div_assoc, inv_mul_cancel₀ hl]
360 · simp
361
362/-- Abel summation: `∑_{u<p≤v} 1/p` as boundary terms minus an integral against `Pn`. -/
363lemma abel_sum_eq (u v : ℝ) (hu : 2 ≤ u) (huv : u ≤ v) :
364 ∑ p ∈ primesIoc u v, 1 / (p : ℝ) =
365 (Real.log v)⁻¹ * Pn ⌊v⌋₊ - (Real.log u)⁻¹ * Pn ⌊u⌋₊ -
366 ∫ t in Set.Ioc u v, (-t⁻¹ / (Real.log t) ^ 2) * Pn ⌊t⌋₊ := by
367 have hu0 : (0 : ℝ) ≤ u := by linarith
368 have hgcont : ContinuousOn (fun t : ℝ => -t⁻¹ / (Real.log t) ^ 2) (Set.Icc u v) :=
369 (continuousOn_g hu).mono Set.Icc_subset_Ici_self
370 have hf_diff : ∀ t ∈ Set.Icc u v, DifferentiableAt ℝ (fun s => (Real.log s)⁻¹) t :=
371 fun t ht => (hasDerivAt_inv_log (by linarith [ht.1])).differentiableAt
372 have hint : IntegrableOn (deriv fun s => (Real.log s)⁻¹) (Set.Icc u v) := by
373 refine IntegrableOn.congr_fun hgcont.integrableOn_Icc (fun t ht => ?_) measurableSet_Icc
374 exact (hasDerivAt_inv_log (by linarith [ht.1])).deriv.symm
375 have h := sum_mul_eq_sub_sub_integral_mul cw hu0 huv hf_diff hint
376 beta_reduce at h
377 have hR : ∫ t in Set.Ioc u v, deriv (fun s : ℝ => (Real.log s)⁻¹) t * ∑ k ∈ Icc 0 ⌊t⌋₊, cw k
378 = ∫ t in Set.Ioc u v, (-t⁻¹ / (Real.log t) ^ 2) * Pn ⌊t⌋₊ := by
379 refine MeasureTheory.setIntegral_congr_fun measurableSet_Ioc (fun t ht => ?_)
380 have ht1 : 1 < t := by linarith [ht.1]
381 rw [(hasDerivAt_inv_log ht1).deriv]
382 rfl
383 rw [sum_Ioc_inv_eq u v, hR] at h
384 exact h
385
386/-! ## Mertens for `∑_{u<p≤v} 1/p` -/
387
388lemma mertens_bound : ∃ C > 0, ∀ u v : ℝ, 2 ≤ u → u ≤ v →
389 |∑ p ∈ primesIoc u v, 1 / (p : ℝ) - Real.log (Real.log v / Real.log u)| ≤ C / Real.log u := by
390 obtain ⟨K, hK0, hK⟩ := Pn_floor_bound
391 refine ⟨3 * K, by linarith, fun u v hu huv => ?_⟩
392 have hlu : 0 < Real.log u := Real.log_pos (by linarith)
393 have hlv : Real.log u ≤ Real.log v := Real.log_le_log (by linarith) huv
394 have hlv0 : 0 < Real.log v := lt_of_lt_of_le hlu hlv
395 have hgcont : ContinuousOn (fun t : ℝ => -t⁻¹ / (Real.log t) ^ 2) (Set.Icc u v) :=
396 (continuousOn_g hu).mono Set.Icc_subset_Ici_self
397 have hglogI : IntegrableOn (fun t : ℝ => (-t⁻¹ / (Real.log t) ^ 2) * Real.log t) (Set.Ioc u v) :=
398 ((hgcont.mul ((continuousOn_inv_log_Ici hu).mono Set.Icc_subset_Ici_self)).integrableOn_Icc).mono_set
399 Set.Ioc_subset_Icc_self
400 have hgPI : IntegrableOn (fun t : ℝ => (-t⁻¹ / (Real.log t) ^ 2) * Pn ⌊t⌋₊) (Set.Ioc u v) :=
401 (integrableOn_mul_sum_Icc cw (m := 0) (by linarith) hgcont.integrableOn_Icc).mono_set
402 Set.Ioc_subset_Icc_self
403 have hgerrI : IntegrableOn (fun t : ℝ => (-t⁻¹ / (Real.log t) ^ 2) * (Pn ⌊t⌋₊ - Real.log t))
404 (Set.Ioc u v) :=
405 (hgPI.sub hglogI).congr_fun (fun t _ => by simp only [Pi.sub_apply]; ring) measurableSet_Ioc
406 have hsplit : ∫ t in Set.Ioc u v, (-t⁻¹ / (Real.log t) ^ 2) * Pn ⌊t⌋₊ =
407 (∫ t in Set.Ioc u v, (-t⁻¹ / (Real.log t) ^ 2) * Real.log t) +
408 ∫ t in Set.Ioc u v, (-t⁻¹ / (Real.log t) ^ 2) * (Pn ⌊t⌋₊ - Real.log t) := by
409 rw [← MeasureTheory.integral_add hglogI hgerrI]
410 congr 1
411 funext t
412 ring
413 have hE := integral_err_le (K := K) u v hu huv hK
414 have hlogdiv : Real.log (Real.log v / Real.log u) = Real.log (Real.log v) - Real.log (Real.log u) :=
415 Real.log_div hlv0.ne' hlu.ne'
416 have hv1 : (Real.log v)⁻¹ * Real.log v = 1 := inv_mul_cancel₀ hlv0.ne'
417 have hu1 : (Real.log u)⁻¹ * Real.log u = 1 := inv_mul_cancel₀ hlu.ne'
418 have hbv : |(Real.log v)⁻¹ * (Pn ⌊v⌋₊ - Real.log v)| ≤ K * (Real.log u)⁻¹ := by
419 rw [abs_mul, abs_of_pos (inv_pos.mpr hlv0)]
420 have h1 := hK v (by linarith)
421 have h2 : (Real.log v)⁻¹ ≤ (Real.log u)⁻¹ := inv_anti₀ hlu hlv
422 calc (Real.log v)⁻¹ * |Pn ⌊v⌋₊ - Real.log v| ≤ (Real.log u)⁻¹ * K :=
423 mul_le_mul h2 h1 (abs_nonneg _) (inv_nonneg.mpr hlu.le)
424 _ = K * (Real.log u)⁻¹ := mul_comm _ _
425 have hbu : |(Real.log u)⁻¹ * (Pn ⌊u⌋₊ - Real.log u)| ≤ K * (Real.log u)⁻¹ := by
426 rw [abs_mul, abs_of_pos (inv_pos.mpr hlu)]
427 have h1 := hK u (by linarith)
428 calc (Real.log u)⁻¹ * |Pn ⌊u⌋₊ - Real.log u| ≤ (Real.log u)⁻¹ * K :=
429 mul_le_mul_of_nonneg_left h1 (inv_nonneg.mpr hlu.le)
430 _ = K * (Real.log u)⁻¹ := mul_comm _ _
431 rw [abel_sum_eq u v hu huv, hsplit, integral_log_part u v hu huv, hlogdiv]
432 set E := ∫ t in Set.Ioc u v, (-t⁻¹ / (Real.log t) ^ 2) * (Pn ⌊t⌋₊ - Real.log t) with hEdef
433 set bv := (Real.log v)⁻¹ * (Pn ⌊v⌋₊ - Real.log v) with hbvdef
434 set bu := (Real.log u)⁻¹ * (Pn ⌊u⌋₊ - Real.log u) with hbudef
435 have key : (Real.log v)⁻¹ * Pn ⌊v⌋₊ - (Real.log u)⁻¹ * Pn ⌊u⌋₊ -
436 (-(Real.log (Real.log v) - Real.log (Real.log u)) + E) -
437 (Real.log (Real.log v) - Real.log (Real.log u)) = bv - bu - E := by
438 rw [hbvdef, hbudef]
439 linear_combination hv1 - hu1
440 rw [key, div_eq_mul_inv]
441 have h3 : |bv - bu - E| ≤ |bv| + |bu| + |E| :=
442 (abs_sub _ _).trans (by linarith [abs_sub bv bu])
443 have hEabs : |E| ≤ K * (Real.log u)⁻¹ := hE
444 linarith
445
446/-! ## Products -/
447
448lemma two_le_of_mem_primesIoc {u v : ℝ} {p : ℕ} (hp : p ∈ primesIoc u v) : (2 : ℝ) ≤ p := by
449 have := (Finset.mem_filter.mp hp).2.two_le
450 exact_mod_cast this
451
452lemma sum_inv_sq_primes_le (u v : ℝ) (hu : 2 ≤ u) (huv : u ≤ v) :
453 ∑ p ∈ primesIoc u v, ((p : ℝ) ^ 2)⁻¹ ≤ 1 / 2 := by
454 have hk : 2 ≤ ⌊u⌋₊ := Nat.le_floor (by exact_mod_cast hu)
455 have hkn : ⌊u⌋₊ ≤ ⌊v⌋₊ := Nat.floor_le_floor huv
456 have hk' : (2 : ℝ) ≤ ⌊u⌋₊ := by exact_mod_cast hk
457 have hsub : primesIoc u v ⊆ Ioc ⌊u⌋₊ ⌊v⌋₊ := Finset.filter_subset _ _
458 calc ∑ p ∈ primesIoc u v, ((p : ℝ) ^ 2)⁻¹
459 ≤ ∑ p ∈ Ioc ⌊u⌋₊ ⌊v⌋₊, ((p : ℝ) ^ 2)⁻¹ :=
460 Finset.sum_le_sum_of_subset_of_nonneg hsub (fun p _ _ => by positivity)
461 _ ≤ (⌊u⌋₊ : ℝ)⁻¹ - (⌊v⌋₊ : ℝ)⁻¹ := sum_Ioc_inv_sq_le_sub (by omega) hkn
462 _ ≤ 1 / 2 := by
463 have h1 : (⌊u⌋₊ : ℝ)⁻¹ ≤ 2⁻¹ := inv_anti₀ (by norm_num) hk'
464 have h2 : (0 : ℝ) ≤ (⌊v⌋₊ : ℝ)⁻¹ := inv_nonneg.mpr (Nat.cast_nonneg _)
465 norm_num at h1 ⊢
466 linarith
467
468lemma one_div_sub_le {p : ℝ} (hp : 2 ≤ p) : 1 / (p - 1) ≤ 1 / p + 2 * (p ^ 2)⁻¹ := by
469 have hp0 : 0 < p := by linarith
470 have hp1 : 0 < p - 1 := by linarith
471 have e : 2 * (p ^ 2)⁻¹ = 2 / p ^ 2 := by ring
472 rw [e, div_add_div _ _ hp0.ne' (pow_pos hp0 2).ne',
473 div_le_div_iff₀ hp1 (mul_pos hp0 (pow_pos hp0 2))]
474 nlinarith [mul_nonneg hp0.le (sub_nonneg.mpr hp)]
475
476lemma sum_inv_sub_le (u v : ℝ) (hu : 2 ≤ u) (huv : u ≤ v) :
477 ∑ p ∈ primesIoc u v, 1 / ((p : ℝ) - 1) ≤ ∑ p ∈ primesIoc u v, 1 / (p : ℝ) + 1 := by
478 calc ∑ p ∈ primesIoc u v, 1 / ((p : ℝ) - 1)
479 ≤ ∑ p ∈ primesIoc u v, (1 / (p : ℝ) + 2 * ((p : ℝ) ^ 2)⁻¹) :=
480 Finset.sum_le_sum fun p hp => one_div_sub_le (two_le_of_mem_primesIoc hp)
481 _ = ∑ p ∈ primesIoc u v, 1 / (p : ℝ) + ∑ p ∈ primesIoc u v, 2 * ((p : ℝ) ^ 2)⁻¹ :=
482 Finset.sum_add_distrib
483 _ ≤ ∑ p ∈ primesIoc u v, 1 / (p : ℝ) + 1 := by
484 have := sum_inv_sq_primes_le u v hu huv
485 rw [← Finset.mul_sum]
486 linarith
487
488lemma one_sub_inv_nonneg {p : ℝ} (hp : 2 ≤ p) : 0 ≤ 1 - 1 / p := by
489 have : 1 / p ≤ 1 := by
490 rw [div_le_one (by linarith)]
491 linarith
492 linarith
493
494lemma prod_upper (u v : ℝ) (hu : 2 ≤ u) (huv : u ≤ v) :
495 ∏ p ∈ primesIoc u v, (1 - 1 / (p : ℝ))⁻¹ ≤
496 Real.exp (∑ p ∈ primesIoc u v, 1 / (p : ℝ) + 1) := by
497 calc ∏ p ∈ primesIoc u v, (1 - 1 / (p : ℝ))⁻¹
498 ≤ ∏ p ∈ primesIoc u v, Real.exp (1 / ((p : ℝ) - 1)) := by
499 apply Finset.prod_le_prod₀
500 · intro p hp
501 exact inv_nonneg.mpr (one_sub_inv_nonneg (two_le_of_mem_primesIoc hp))
502 · intro p hp
503 have hp2 := two_le_of_mem_primesIoc hp
504 have hp0 : (p : ℝ) ≠ 0 := by intro h; linarith
505 have hp1 : (p : ℝ) - 1 ≠ 0 := by intro h; linarith
506 have e : (1 - 1 / (p : ℝ))⁻¹ = 1 + 1 / ((p : ℝ) - 1) := by
507 field_simp
508 ring
509 rw [e]
510 linarith [Real.add_one_le_exp (1 / ((p : ℝ) - 1))]
511 _ = Real.exp (∑ p ∈ primesIoc u v, 1 / ((p : ℝ) - 1)) := (Real.exp_sum _ _).symm
512 _ ≤ Real.exp (∑ p ∈ primesIoc u v, 1 / (p : ℝ) + 1) :=
513 Real.exp_le_exp.mpr (sum_inv_sub_le u v hu huv)
514
515lemma prod_lower (u v : ℝ) :
516 ∏ p ∈ primesIoc u v, (1 - 1 / (p : ℝ)) ≤ Real.exp (-∑ p ∈ primesIoc u v, 1 / (p : ℝ)) := by
517 calc ∏ p ∈ primesIoc u v, (1 - 1 / (p : ℝ))
518 ≤ ∏ p ∈ primesIoc u v, Real.exp (-(1 / (p : ℝ))) := by
519 apply Finset.prod_le_prod₀
520 · intro p hp
521 exact one_sub_inv_nonneg (two_le_of_mem_primesIoc hp)
522 · intro p hp
523 linarith [Real.add_one_le_exp (-(1 / (p : ℝ)))]
524 _ = Real.exp (∑ p ∈ primesIoc u v, -(1 / (p : ℝ))) := (Real.exp_sum _ _).symm
525 _ = Real.exp (-∑ p ∈ primesIoc u v, 1 / (p : ℝ)) := by rw [Finset.sum_neg_distrib]
526
527lemma mertens_prod_bound : ∃ C > 0, ∀ u v : ℝ, 2 ≤ u → u ≤ v →
528 ∏ p ∈ primesIoc u v, (1 - 1 / (p : ℝ))⁻¹ ≤ C * Real.log v / Real.log u ∧
529 ∏ p ∈ primesIoc u v, (1 - 1 / (p : ℝ)) ≤ C * Real.log u / Real.log v := by
530 obtain ⟨C0, hC0, hS⟩ := mertens_bound
531 refine ⟨Real.exp (C0 / Real.log 2 + 1), Real.exp_pos _, fun u v hu huv => ?_⟩
532 have hlu : 0 < Real.log u := Real.log_pos (by linarith)
533 have hlv : Real.log u ≤ Real.log v := Real.log_le_log (by linarith) huv
534 have hlv0 : 0 < Real.log v := lt_of_lt_of_le hlu hlv
535 have hl2 : 0 < Real.log 2 := Real.log_pos (by norm_num)
536 have hl2u : Real.log 2 ≤ Real.log u := Real.log_le_log (by norm_num) hu
537 have hS' := abs_le.mp (hS u v hu huv)
538 have hCu : C0 / Real.log u ≤ C0 / Real.log 2 := div_le_div_of_nonneg_left hC0.le hl2 hl2u
539 have hE1 : Real.exp (C0 / Real.log u) ≤ Real.exp (C0 / Real.log 2 + 1) :=
540 Real.exp_le_exp.mpr (by linarith)
541 have hL : Real.exp (Real.log (Real.log v / Real.log u)) = Real.log v / Real.log u :=
542 Real.exp_log (div_pos hlv0 hlu)
543 constructor
544 · calc ∏ p ∈ primesIoc u v, (1 - 1 / (p : ℝ))⁻¹
545 ≤ Real.exp (∑ p ∈ primesIoc u v, 1 / (p : ℝ) + 1) := prod_upper u v hu huv
546 _ ≤ Real.exp (Real.log (Real.log v / Real.log u) + C0 / Real.log u + 1) :=
547 Real.exp_le_exp.mpr (by linarith [hS'.2])
548 _ = Real.exp (Real.log (Real.log v / Real.log u)) * (Real.exp (C0 / Real.log u) * Real.exp 1) := by
549 rw [Real.exp_add, Real.exp_add, mul_assoc]
550 _ ≤ Real.exp (Real.log (Real.log v / Real.log u)) * (Real.exp (C0 / Real.log 2) * Real.exp 1) := by
551 apply mul_le_mul_of_nonneg_left _ (Real.exp_pos _).le
552 exact mul_le_mul_of_nonneg_right (Real.exp_le_exp.mpr hCu) (Real.exp_pos 1).le
553 _ = Real.exp (C0 / Real.log 2 + 1) * (Real.log v / Real.log u) := by
554 rw [hL, Real.exp_add]
555 ring
556 _ = Real.exp (C0 / Real.log 2 + 1) * Real.log v / Real.log u := (mul_div_assoc _ _ _).symm
557 · calc ∏ p ∈ primesIoc u v, (1 - 1 / (p : ℝ))
558 ≤ Real.exp (-∑ p ∈ primesIoc u v, 1 / (p : ℝ)) := prod_lower u v
559 _ ≤ Real.exp (-Real.log (Real.log v / Real.log u) + C0 / Real.log u) :=
560 Real.exp_le_exp.mpr (by linarith [hS'.1])
561 _ = Real.exp (-Real.log (Real.log v / Real.log u)) * Real.exp (C0 / Real.log u) :=
562 Real.exp_add _ _
563 _ ≤ Real.exp (-Real.log (Real.log v / Real.log u)) * Real.exp (C0 / Real.log 2 + 1) :=
564 mul_le_mul_of_nonneg_left hE1 (Real.exp_pos _).le
565 _ = Real.exp (C0 / Real.log 2 + 1) * Real.log u / Real.log v := by
566 rw [Real.exp_neg, hL, inv_div]
567 ring
568
569end E424.T10Aux
570