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/DivisorMoments/MomentsAux.lean

All files · 468 lines · source at the verified commit

1module
2
3public import E424.DivisorMoments.L13
4
5@[expose] public section
6
7/-!
8# Helpers for Section 4 (Moments)
9
10Products of divisibility indicators, the expansion of `rc S n ^ 4` into tuples, and the bound on
11rough divisors of `n ≤ z^k`.
12-/
13
14set_option linter.style.header false
15open Finset
16
17namespace E424
18
19theorem ind_mul4_dvd (a b c d n : ℕ) :
20 ind (a ∣ n) * ind (b ∣ n) * ind (c ∣ n) * ind (d ∣ n) = ind (a.lcm (b.lcm (c.lcm d)) ∣ n) := by
21 unfold ind
22 by_cases ha : a ∣ n <;> by_cases hb : b ∣ n <;> by_cases hc : c ∣ n <;> by_cases hd : d ∣ n <;>
23 simp [ha, hb, hc, hd, Nat.lcm_dvd_iff]
24
25theorem rc_eq_ind (S : Finset ℕ) (n : ℕ) : rc S n = ∑ s ∈ S, ind (s ∣ n) := by
26 rw [rc_eq_sum]
27 refine Finset.sum_congr rfl fun s _ => ?_
28 unfold ind
29 split_ifs <;> rfl
30
31theorem rc_pow4 (S : Finset ℕ) (n : ℕ) :
32 rc S n ^ 4 = ∑ s₁ ∈ S, ∑ s₂ ∈ S, ∑ s₃ ∈ S, ∑ s₄ ∈ S,
33 ind (s₁.lcm (s₂.lcm (s₃.lcm s₄)) ∣ n) := by
34 simp only [pow_succ, pow_zero, one_mul, rc_eq_ind, Finset.sum_mul, Finset.mul_sum]
35 refine Finset.sum_congr rfl fun s₁ _ => Finset.sum_congr rfl fun s₂ _ =>
36 Finset.sum_congr rfl fun s₃ _ => Finset.sum_congr rfl fun s₄ _ => ?_
37 rw [ind_mul4_dvd]
38 congr 2
39 ac_rfl
40
41open scoped Classical in
42/-- Rough divisors of `n` number at most `2^k` when `n ≤ z^k`. Each prime `p > z` dividing `n` at
43most doubles the count, since `s ↦ s / p` injects the divisible rough divisors into those of `n / p`. -/
44theorem rough_divisors_card (z : ℝ) (hz : 2 ≤ z) (k n : ℕ) (hn : 1 ≤ n) (hnk : (n : ℝ) ≤ z ^ k) :
45 (n.divisors.filter (Rough z)).card ≤ 2 ^ k := by
46 induction n using Nat.strong_induction_on generalizing k with
47 | _ n ih =>
48 by_cases hp : ∃ p, p.Prime ∧ p ∣ n ∧ z < p
49 · obtain ⟨p, hpr, hpn, hzp⟩ := hp
50 have hp2 := hpr.two_le
51 have hpos : 0 < p := hpr.pos
52 set m := n / p with hm_def
53 have hnm : n = p * m := (Nat.mul_div_cancel' hpn).symm
54 have hm1 : 1 ≤ m := by
55 rcases Nat.eq_zero_or_pos m with h | h
56 · rw [hnm, h, mul_zero] at hn; omega
57 · exact h
58 have hmn : m < n := by
59 rw [hnm]; nlinarith
60 -- the exponent drops by one
61 obtain ⟨k', rfl⟩ : ∃ k', k = k' + 1 := by
62 cases k with
63 | zero =>
64 exfalso
65 have : (p : ℝ) ≤ n := by exact_mod_cast Nat.le_of_dvd (by omega) hpn
66 simp at hnk
67 linarith
68 | succ k' => exact ⟨k', rfl⟩
69 have hmz : (m : ℝ) ≤ z ^ k' := by
70 have hcast : (n : ℝ) = p * m := by exact_mod_cast hnm
71 have hz0 : (0 : ℝ) < z := by linarith
72 have h1 : (m : ℝ) * z ≤ (m : ℝ) * p := mul_le_mul_of_nonneg_left (by exact_mod_cast hzp.le) (by positivity)
73 have h2 : (m : ℝ) * z ≤ z ^ k' * z := by
74 rw [pow_succ] at hnk; nlinarith
75 exact le_of_mul_le_mul_right h2 hz0
76 have ihm := ih m hmn k' hm1 hmz
77 have hsub : n.divisors.filter (Rough z) ⊆
78 m.divisors.filter (Rough z) ∪ (m.divisors.filter (Rough z)).image (· * p) := by
79 intro s hs
80 rw [mem_filter, Nat.mem_divisors] at hs
81 obtain ⟨⟨hsn, -⟩, hsr⟩ := hs
82 by_cases hps : p ∣ s
83 · obtain ⟨t, rfl⟩ := hps
84 rw [mem_union, mem_image]
85 right
86 refine ⟨t, ?_, mul_comm _ _⟩
87 rw [mem_filter, Nat.mem_divisors]
88 refine ⟨⟨?_, by omega⟩, fun q hq hqt => hsr q hq (dvd_mul_of_dvd_right hqt p)⟩
89 rw [hnm] at hsn
90 exact Nat.dvd_of_mul_dvd_mul_left hpos hsn
91 · rw [mem_union]
92 left
93 rw [mem_filter, Nat.mem_divisors]
94 refine ⟨⟨?_, by omega⟩, hsr⟩
95 have hcop : Nat.Coprime s p := (Nat.Coprime.symm ((Nat.Prime.coprime_iff_not_dvd hpr).mpr hps))
96 rw [hnm] at hsn
97 exact hcop.dvd_of_dvd_mul_left hsn
98 calc (n.divisors.filter (Rough z)).card
99 ≤ (m.divisors.filter (Rough z) ∪ (m.divisors.filter (Rough z)).image (· * p)).card :=
100 card_le_card hsub
101 _ ≤ (m.divisors.filter (Rough z)).card + ((m.divisors.filter (Rough z)).image (· * p)).card :=
102 card_union_le _ _
103 _ ≤ (m.divisors.filter (Rough z)).card + (m.divisors.filter (Rough z)).card :=
104 Nat.add_le_add_left card_image_le _
105 _ ≤ 2 ^ k' + 2 ^ k' := by omega
106 _ = 2 ^ (k' + 1) := by ring
107 · simp only [not_exists, not_and, not_lt] at hp
108 have : n.divisors.filter (Rough z) ⊆ {1} := by
109 intro s hs
110 rw [mem_filter, Nat.mem_divisors] at hs
111 obtain ⟨⟨hsn, -⟩, hsr⟩ := hs
112 rw [mem_singleton]
113 by_contra hne
114 obtain ⟨q, hq, hqs⟩ := Nat.exists_prime_and_dvd hne
115 exact absurd (hp q hq (hqs.trans hsn)) (not_le.mpr (hsr q hq hqs))
116 exact (card_le_card this).trans (by simp [Nat.one_le_two_pow])
117
118
119
120
121/-- Product of indicators is the indicator of the conjunction. -/
122theorem ind_and_eq (p q : Prop) : ind p * ind q = ind (p ∧ q) := by
123 unfold ind
124 by_cases hp : p <;> by_cases hq : q <;> simp [hp, hq]
125
126/-- `(s - a % s) + a` is a multiple of `s`, so `n ≡ s - a % s` is the class where `s ∣ n + a`. -/
127theorem neg_mod_add_dvd (s a : ℕ) (hs : 0 < s) : (s - a % s + a) % s = 0 := by
128 have h1 := Nat.mod_add_div a s
129 have h2 := Nat.mod_lt a hs
130 have h3 : s - a % s + a = s * (1 + a / s) := by
131 rw [mul_add, mul_one]
132 generalize s * (a / s) = t at *
133 omega
134 rw [h3, Nat.mul_mod_right]
135
136/-- Two divisibility indicators with shifts `a`, `b`: the window sum is at most `len · gcd/(s s')` plus
137`1/lo` when `a ≡ b` mod `gcd s s'` (Chinese remainder), and `0` otherwise. -/
138theorem pair_bound (W : Win) (hlo : 0 < W.lo) (hhi : W.lo ≤ W.hi) {s s' : ℕ} (hs : 0 < s) (hs' : 0 < s')
139 (a b : ℕ) :
140 W.sum (fun n => ind (s ∣ n + a) * ind (s' ∣ n + b)) ≤
141 ind (a ≡ b [MOD Nat.gcd s s']) * (W.len * ((Nat.gcd s s' : ℝ) / ((s : ℝ) * s')) + 1 / W.lo) := by
142 by_cases hc : a ≡ b [MOD Nat.gcd s s']
143 · rw [ind_true hc, one_mul]
144 have hs_pos : 0 < Nat.lcm s s' := Nat.lcm_pos hs hs'
145 -- residues `c ≡ -a` mod s and `c' ≡ -b` mod s', compatible mod gcd
146 have hc1 := neg_mod_add_dvd s a hs
147 have hc2 := neg_mod_add_dvd s' b hs'
148 have e1 : (s - a % s + a) ≡ 0 [MOD Nat.gcd s s'] :=
149 Nat.ModEq.of_dvd (Nat.gcd_dvd_left s s') (Nat.modEq_zero_iff_dvd.2 (Nat.dvd_of_mod_eq_zero hc1))
150 have e2 : (s' - b % s' + b) ≡ 0 [MOD Nat.gcd s s'] :=
151 Nat.ModEq.of_dvd (Nat.gcd_dvd_right s s') (Nat.modEq_zero_iff_dvd.2 (Nat.dvd_of_mod_eq_zero hc2))
152 have hc12 : (s - a % s) ≡ (s' - b % s') [MOD Nat.gcd s s'] :=
153 Nat.ModEq.add_right_cancel' a ((e1.trans e2.symm).trans (Nat.ModEq.add_left (s' - b % s') hc.symm))
154 obtain ⟨k, hk1, hk2⟩ : ∃ k, k ≡ s - a % s [MOD s] ∧ k ≡ s' - b % s' [MOD s'] :=
155 ⟨_, (Nat.chineseRemainder' hc12).2⟩
156 have hka : k + a ≡ 0 [MOD s] := (Nat.ModEq.add_right a hk1).trans (Nat.modEq_zero_iff_dvd.2 (Nat.dvd_of_mod_eq_zero hc1))
157 have hkb : k + b ≡ 0 [MOD s'] := (Nat.ModEq.add_right b hk2).trans (Nat.modEq_zero_iff_dvd.2 (Nat.dvd_of_mod_eq_zero hc2))
158 have key : ∀ n, (s ∣ n + a ∧ s' ∣ n + b) ↔ n ≡ k [MOD Nat.lcm s s'] := by
159 intro n
160 have ks : s ∣ n + a ↔ n ≡ k [MOD s] := by
161 rw [← Nat.modEq_zero_iff_dvd]
162 constructor
163 · intro h; exact Nat.ModEq.add_right_cancel' a (h.trans hka.symm)
164 · intro h; exact (Nat.ModEq.add_right a h).trans hka
165 have ks' : s' ∣ n + b ↔ n ≡ k [MOD s'] := by
166 rw [← Nat.modEq_zero_iff_dvd]
167 constructor
168 · intro h; exact Nat.ModEq.add_right_cancel' b (h.trans hkb.symm)
169 · intro h; exact (Nat.ModEq.add_right b h).trans hkb
170 rw [ks, ks']
171 constructor
172 · rintro ⟨h1, h2⟩; exact Nat.mod_lcm h1 h2
173 · intro h; exact ⟨Nat.ModEq.of_dvd (Nat.dvd_lcm_left s s') h, Nat.ModEq.of_dvd (Nat.dvd_lcm_right s s') h⟩
174 have hfun : (fun n => ind (s ∣ n + a) * ind (s' ∣ n + b)) = fun n => ind (n ≡ k [MOD Nat.lcm s s']) :=
175 funext fun n => by rw [ind_and_eq]; exact ind_congr (key n)
176 rw [hfun]
177 have h13 := abs_le.mp (L13 W hlo hhi (Nat.lcm s s') k hs_pos)
178 have hL : (Nat.lcm s s' : ℝ) * (Nat.gcd s s' : ℝ) = (s : ℝ) * s' := by
179 exact_mod_cast (by rw [mul_comm]; exact Nat.gcd_mul_lcm s s')
180 have hlen : W.len / (Nat.lcm s s' : ℝ) = W.len * ((Nat.gcd s s' : ℝ) / ((s : ℝ) * s')) := by
181 have hL0 : (Nat.lcm s s' : ℝ) ≠ 0 := by exact_mod_cast hs_pos.ne'
182 have hs0 : (s : ℝ) * s' ≠ 0 := by positivity
183 rw [← hL]
184 field_simp
185 linarith [h13.2]
186 · rw [ind_false hc, zero_mul]
187 have hz : ∀ n, ind (s ∣ n + a) * ind (s' ∣ n + b) = 0 := by
188 intro n
189 unfold ind
190 split_ifs with h1 h2 <;> try simp
191 exfalso
192 apply hc
193 have g1 : Nat.gcd s s' ∣ n + a := (Nat.gcd_dvd_left s s').trans h1
194 have g2 : Nat.gcd s s' ∣ n + b := (Nat.gcd_dvd_right s s').trans h2
195 have e1 : n + a ≡ 0 [MOD Nat.gcd s s'] := Nat.modEq_zero_iff_dvd.2 g1
196 have e2 : n + b ≡ 0 [MOD Nat.gcd s s'] := Nat.modEq_zero_iff_dvd.2 g2
197 exact Nat.ModEq.add_left_cancel' n (e1.trans e2.symm)
198 have : W.sum (fun n => ind (s ∣ n + a) * ind (s' ∣ n + b)) = 0 := by
199 simp only [hz]
200 simp [Win.sum]
201 rw [this]
202
203
204
205/-- Among `i' ∈ [1, H]`, those with `h1 * i ≡ h1 * i'` mod `g` (with `g` coprime to `h1`) are at most `H / g + 1`. -/
206theorem count_row (H g h1 i : ℕ) (hcop : Nat.Coprime g h1) :
207 ((Icc 1 H).filter (fun i' => h1 * i ≡ h1 * i' [MOD g])).card ≤ H / g + 1 := by
208 have hcop' : Nat.gcd g h1 = 1 := hcop
209 calc ((Icc 1 H).filter (fun i' => h1 * i ≡ h1 * i' [MOD g])).card
210 ≤ (range (H / g + 1)).card := by
211 refine card_le_card_of_injOn (fun x => x / g) ?_ ?_
212 · intro x hx
213 rw [mem_coe, mem_filter, mem_Icc] at hx
214 rw [mem_coe, mem_range, Nat.lt_succ_iff]
215 exact Nat.div_le_div_right hx.1.2
216 · intro x hx y hy hxy
217 rw [mem_coe, mem_filter] at hx hy
218 have hmod : x ≡ y [MOD g] := by
219 apply Nat.ModEq.cancel_left_of_coprime hcop'
220 exact hx.2.symm.trans hy.2
221 simp only at hxy
222 have h1' := Nat.div_add_mod x g
223 have h2' := Nat.div_add_mod y g
224 unfold Nat.ModEq at hmod
225 rw [hxy, hmod] at h1'
226 omega
227 _ = H / g + 1 := card_range _
228
229/-- Per-pair bound used in Lemma 15: the compatible pairs over the shifts `h1 i` contribute at most
230`1/(s s')`, plus `g/(H s s')` when `g > 1`. -/
231theorem pair_count (H : ℕ) (hH : 1 ≤ H) (h1 g : ℕ) (hg : 0 < g) (hcop : Nat.Coprime g h1) (s s' : ℝ)
232 (hs : 0 < s) (hs' : 0 < s') :
233 (1 / (H : ℝ) ^ 2) * (∑ i ∈ Icc 1 H, ∑ i' ∈ Icc 1 H, ind (h1 * i ≡ h1 * i' [MOD g])) *
234 ((g : ℝ) / (s * s')) ≤
235 1 / (s * s') + (if 1 < g then (g : ℝ) / ((H : ℝ) * (s * s')) else 0) := by
236 have hH0 : (0 : ℝ) < H := by exact_mod_cast hH
237 have hrow : ∀ i ∈ Icc 1 H, ∑ i' ∈ Icc 1 H, ind (h1 * i ≡ h1 * i' [MOD g]) ≤ ((H / g + 1 : ℕ) : ℝ) := by
238 intro i _
239 have : ∑ i' ∈ Icc 1 H, ind (h1 * i ≡ h1 * i' [MOD g]) =
240 (((Icc 1 H).filter (fun i' => h1 * i ≡ h1 * i' [MOD g])).card : ℝ) := by
241 unfold ind
242 rw [Finset.card_filter]
243 push_cast
244 refine Finset.sum_congr rfl fun i' _ => ?_
245 split_ifs <;> rfl
246 rw [this]
247 exact_mod_cast count_row H g h1 i hcop
248 have hcard : ((Icc 1 H).card : ℝ) = H := by simp
249 have hC : ∑ i ∈ Icc 1 H, ∑ i' ∈ Icc 1 H, ind (h1 * i ≡ h1 * i' [MOD g]) ≤
250 (H : ℝ) * ((H / g + 1 : ℕ) : ℝ) := by
251 calc _ ≤ ∑ i ∈ Icc 1 H, ((H / g + 1 : ℕ) : ℝ) := sum_le_sum hrow
252 _ = (H : ℝ) * ((H / g + 1 : ℕ) : ℝ) := by rw [sum_const, nsmul_eq_mul, hcard]
253 have hss : 0 < s * s' := mul_pos hs hs'
254 have hdiv : (0 : ℝ) ≤ (g : ℝ) / (s * s') := by positivity
255 by_cases hg1 : 1 < g
256 · rw [if_pos hg1]
257 have hq : ((H / g : ℕ) : ℝ) * g ≤ H := by exact_mod_cast Nat.div_mul_le_self H g
258 have hstep : (1 / (H : ℝ) ^ 2) * (∑ i ∈ Icc 1 H, ∑ i' ∈ Icc 1 H, ind (h1 * i ≡ h1 * i' [MOD g])) *
259 ((g : ℝ) / (s * s')) ≤ (1 / (H : ℝ) ^ 2) * ((H : ℝ) * ((H / g + 1 : ℕ) : ℝ)) * ((g : ℝ) / (s * s')) := by
260 gcongr
261 have heq : (1 / (H : ℝ) ^ 2) * ((H : ℝ) * ((H / g + 1 : ℕ) : ℝ)) * ((g : ℝ) / (s * s')) =
262 (((H / g : ℕ) : ℝ) * g + g) / ((H : ℝ) * (s * s')) := by
263 push_cast
264 field_simp
265 have hfin : (((H / g : ℕ) : ℝ) * g + g) / ((H : ℝ) * (s * s')) ≤
266 1 / (s * s') + (g : ℝ) / ((H : ℝ) * (s * s')) := by
267 have hsum : 1 / (s * s') + (g : ℝ) / ((H : ℝ) * (s * s')) = ((H : ℝ) + g) / ((H : ℝ) * (s * s')) := by
268 have hH0 : (H : ℝ) ≠ 0 := by positivity
269 field_simp
270 rw [hsum]
271 gcongr
272 linarith
273 · rw [if_neg hg1]
274 have hg_one : g = 1 := by omega
275 subst hg_one
276 have hC1 : ∑ i ∈ Icc 1 H, ∑ i' ∈ Icc 1 H, ind (h1 * i ≡ h1 * i' [MOD 1]) ≤ (H : ℝ) * H := by
277 calc _ ≤ ∑ i ∈ Icc 1 H, (H : ℝ) := sum_le_sum fun i _ =>
278 (sum_le_sum fun i' _ => ind_le_one _).trans (by simp)
279 _ = (H : ℝ) * H := by rw [sum_const, nsmul_eq_mul, hcard]
280 have hH2 : (0 : ℝ) < (H : ℝ) ^ 2 := by positivity
281 have : (1 / (H : ℝ) ^ 2) * (∑ i ∈ Icc 1 H, ∑ i' ∈ Icc 1 H, ind (h1 * i ≡ h1 * i' [MOD 1])) *
282 (((1 : ℕ) : ℝ) / (s * s')) ≤ (1 / (H : ℝ) ^ 2) * ((H : ℝ) * H) * (((1 : ℕ) : ℝ) / (s * s')) := by
283 gcongr
284 have heq : (1 / (H : ℝ) ^ 2) * ((H : ℝ) * H) * (((1 : ℕ) : ℝ) / (s * s')) = 1 / (s * s') := by
285 have hH0 : (H : ℝ) ≠ 0 := by positivity
286 push_cast
287 field_simp
288 linarith
289
290
291
292/-- Along a chain of blocks, `z j ^ 200 ≤ z j'` for `j < j'`. -/
293theorem chain_lt (J : ℕ) (z : ℕ → ℝ) (hz : ∀ j, j < J → 1 ≤ z j)
294 (hc : ∀ j, j + 1 < J → z j ^ 200 ≤ z (j + 1)) :
295 ∀ j j', j < j' → j' < J → z j ^ 200 ≤ z j' := by
296 intro j j' hjj' hj'J
297 induction j' with
298 | zero => omega
299 | succ k ih =>
300 by_cases hk : j < k
301 · have h1 := ih hk (by omega)
302 have h2 : z k ≤ z k ^ 200 := le_self_pow₀ (hz k (by omega)) (by norm_num)
303 have h3 := hc k hj'J
304 linarith
305 · have : j = k := by omega
306 subst this
307 exact hc j hj'J
308
309/-- Distinct blocks are coprime: a common prime would be `> z j'` and `≤ z j ^ 200`. -/
310theorem cross_gcd (J : ℕ) (z : ℕ → ℝ) (S : ℕ → Finset ℕ) (hblk : ∀ j, j < J → 3 ≤ z j ∧ IsFam (z j) (S j))
311 (hc : ∀ j, j + 1 < J → z j ^ 200 ≤ z (j + 1)) (j j' : ℕ) (hjj : j < j') (hj' : j' < J)
312 {s s' : ℕ} (hs : s ∈ S j) (hs' : s' ∈ S j') : Nat.gcd s s' = 1 := by
313 by_contra hne
314 obtain ⟨p, hp, hpg⟩ := Nat.exists_prime_and_dvd hne
315 have hps : p ∣ s := hpg.trans (Nat.gcd_dvd_left _ _)
316 have hps' : p ∣ s' := hpg.trans (Nat.gcd_dvd_right _ _)
317 have hsz := (hblk j (by omega)).2
318 have hsz' := (hblk j' hj').2
319 have hj3 := (hblk j (by omega)).1
320 have hspos : 0 < s := by
321 have := (hsz s hs).1
322 exact_mod_cast (by linarith : (0 : ℝ) < s)
323 have h1 : (p : ℝ) ≤ z j ^ 200 :=
324 (Nat.cast_le.mpr (Nat.le_of_dvd hspos hps)).trans (hsz s hs).2.1
325 have h2 : z j' < p := (hsz' s' hs').2.2 p hp hps'
326 have h3 := chain_lt J z (fun k hk => by linarith [(hblk k hk).1]) hc j j' hjj hj'
327 linarith
328
329/-- Summed over pairs in `T`, the gcd weights `gcd/(s s')` with `gcd > 1` total at most `J` times the
330single-family bound: only pairs inside one block contribute. -/
331theorem block_gcd_sum (J : ℕ) (T : Finset ℕ) (hT : IsBlocks J T) (CG : ℝ)
332 (hCG : ∀ z : ℝ, 2 ≤ z → ∀ S : Finset ℕ, IsFam z S →
333 ∑ s ∈ S, ∑ s' ∈ S, (Nat.gcd s s' : ℝ) / ((s : ℝ) * s') ≤ CG) :
334 ∑ s ∈ T, ∑ s' ∈ T, (if 1 < Nat.gcd s s' then (Nat.gcd s s' : ℝ) / ((s : ℝ) * s') else 0) ≤ J * CG := by
335 obtain ⟨z, S, hblk, hc, rfl⟩ := hT
336 have hdisj : (↑(range J) : Set ℕ).PairwiseDisjoint S := by
337 intro j hj j' hj' hne
338 simp only [Function.onFun]
339 rw [Finset.disjoint_left]
340 intro s hs hs'
341 have hj1 : j < J := by simpa using hj
342 have hj2 : j' < J := by simpa using hj'
343 rcases lt_or_gt_of_ne hne with h | h
344 · have h1 := cross_gcd J z S hblk hc j j' h hj2 hs hs'
345 rw [Nat.gcd_self] at h1
346 subst h1
347 have := ((hblk j hj1).2 1 hs).1
348 norm_num at this
349 linarith [(hblk j hj1).1]
350 · have h1 := cross_gcd J z S hblk hc j' j h hj1 hs' hs
351 rw [Nat.gcd_self] at h1
352 subst h1
353 have := ((hblk j' hj2).2 1 hs').1
354 norm_num at this
355 linarith [(hblk j' hj2).1]
356 calc ∑ s ∈ (range J).biUnion S, ∑ s' ∈ (range J).biUnion S,
357 (if 1 < Nat.gcd s s' then (Nat.gcd s s' : ℝ) / ((s : ℝ) * s') else 0)
358 = ∑ j ∈ range J, ∑ s ∈ S j, ∑ s' ∈ (range J).biUnion S,
359 (if 1 < Nat.gcd s s' then (Nat.gcd s s' : ℝ) / ((s : ℝ) * s') else 0) :=
360 sum_biUnion hdisj
361 _ = ∑ j ∈ range J, ∑ s ∈ S j, ∑ s' ∈ S j,
362 (if 1 < Nat.gcd s s' then (Nat.gcd s s' : ℝ) / ((s : ℝ) * s') else 0) := by
363 refine sum_congr rfl fun j hj => sum_congr rfl fun s hs => ?_
364 rw [sum_biUnion hdisj, sum_eq_single j]
365 · intro j' hj' hne
366 apply sum_eq_zero
367 intro s' hs'
368 have hj1 : j < J := by simpa using hj
369 have hj2 : j' < J := by simpa using hj'
370 have hg : Nat.gcd s s' = 1 := by
371 rcases lt_or_gt_of_ne hne with h | h
372 · exact (Nat.gcd_comm _ _).trans (cross_gcd J z S hblk hc j' j h hj1 hs' hs)
373 · exact cross_gcd J z S hblk hc j j' h hj2 hs hs'
374 simp [hg]
375 · intro hj_not
376 exact absurd hj hj_not
377 _ ≤ ∑ j ∈ range J, CG := by
378 refine sum_le_sum fun j hj => ?_
379 have hj1 : j < J := by simpa using hj
380 calc _ ≤ ∑ s ∈ S j, ∑ s' ∈ S j, (Nat.gcd s s' : ℝ) / ((s : ℝ) * s') := by
381 refine sum_le_sum fun s _ => sum_le_sum fun s' _ => ?_
382 split_ifs
383 · exact le_rfl
384 · positivity
385 _ ≤ CG := hCG (z j) (by linarith [(hblk j hj1).1]) (S j) (hblk j hj1).2
386 _ = J * CG := by simp
387
388/-- `s ∣ n + a` is the single residue class `n ≡ s - a % s` mod `s`. -/
389theorem single_bound (W : Win) (hlo : 0 < W.lo) (hhi : W.lo ≤ W.hi) {s : ℕ} (hs : 0 < s) (a : ℕ) :
390 W.len / s - 1 / W.lo ≤ W.sum (fun n => ind (s ∣ n + a)) := by
391 have hk := neg_mod_add_dvd s a hs
392 have hka : (s - a % s) + a ≡ 0 [MOD s] := Nat.modEq_zero_iff_dvd.2 (Nat.dvd_of_mod_eq_zero hk)
393 have key : ∀ n, s ∣ n + a ↔ n ≡ s - a % s [MOD s] := by
394 intro n
395 rw [← Nat.modEq_zero_iff_dvd]
396 constructor
397 · intro h; exact Nat.ModEq.add_right_cancel' a (h.trans hka.symm)
398 · intro h; exact (Nat.ModEq.add_right a h).trans hka
399 have hfun : (fun n => ind (s ∣ n + a)) = fun n => ind (n ≡ s - a % s [MOD s]) :=
400 funext fun n => ind_congr (key n)
401 rw [hfun]
402 have h13 := abs_le.mp (L13 W hlo hhi s (s - a % s) hs)
403 linarith [h13.1]
404
405/-- Rough integers at `z ≥ 3` are coprime to `54`. -/
406theorem rough_coprime54 {z : ℝ} (hz : 3 ≤ z) {s : ℕ} (hs : Rough z s) : Nat.Coprime s 54 := by
407 have h2 : ¬ 2 ∣ s := fun h => by have := hs 2 Nat.prime_two h; norm_num at this; linarith
408 have h3 : ¬ 3 ∣ s := fun h => by have := hs 3 Nat.prime_three h; norm_num at this; linarith
409 have c2 : Nat.Coprime s 2 := Nat.Coprime.symm ((Nat.Prime.coprime_iff_not_dvd Nat.prime_two).2 h2)
410 have c3 : Nat.Coprime s 3 := Nat.Coprime.symm ((Nat.Prime.coprime_iff_not_dvd Nat.prime_three).2 h3)
411 have : (54 : ℕ) = 2 * 3 ^ 3 := by norm_num
412 rw [this]
413 exact Nat.Coprime.mul_right c2 (Nat.Coprime.pow_right 3 c3)
414
415/-- Members of a union of blocks are coprime to `54` and positive. -/
416theorem blocks_coprime {J : ℕ} {T : Finset ℕ} (hT : IsBlocks J T) :
417 ∀ s ∈ T, Nat.Coprime s 54 ∧ 0 < s := by
418 obtain ⟨z, S, hblk, -, rfl⟩ := hT
419 intro s hs
420 rw [mem_biUnion] at hs
421 obtain ⟨j, hj, hsj⟩ := hs
422 have hj' : j < J := by simpa using hj
423 obtain ⟨h3, hfam⟩ := hblk j hj'
424 obtain ⟨hz, -, hr⟩ := hfam s hsj
425 refine ⟨rough_coprime54 h3 hr, ?_⟩
426 have h0 : (0 : ℝ) < s := by linarith
427 exact_mod_cast h0
428
429/-- Expansion of the squared block average into products of divisibility indicators. -/
430theorem sq_expand (T I : Finset ℕ) (H : ℕ) (hH : 1 ≤ H) (M : ℝ) (n h1 : ℕ) :
431 ((∑ i ∈ I, rc T (n + h1 * i)) / H - M) ^ 2 =
432 (1 / (H : ℝ) ^ 2) * (∑ i ∈ I, ∑ j ∈ I, ∑ s ∈ T, ∑ s' ∈ T,
433 ind (s ∣ n + h1 * i) * ind (s' ∣ n + h1 * j)) -
434 (2 * M / H) * (∑ i ∈ I, ∑ s ∈ T, ind (s ∣ n + h1 * i)) + M ^ 2 := by
435 have hH0 : (H : ℝ) ≠ 0 := by
436 have : (0 : ℝ) < H := by exact_mod_cast hH
437 exact this.ne'
438 simp only [rc_eq_ind]
439 have e1 : (∑ i ∈ I, ∑ s ∈ T, ind (s ∣ n + h1 * i)) ^ 2 =
440 ∑ i ∈ I, ∑ j ∈ I, ∑ s ∈ T, ∑ s' ∈ T, ind (s ∣ n + h1 * i) * ind (s' ∣ n + h1 * j) := by
441 rw [sq, sum_mul_sum]
442 refine sum_congr rfl fun i _ => sum_congr rfl fun j _ => ?_
443 rw [sum_mul_sum]
444 have e2 : ((∑ i ∈ I, ∑ s ∈ T, ind (s ∣ n + h1 * i)) / H - M) ^ 2 =
445 (∑ i ∈ I, ∑ s ∈ T, ind (s ∣ n + h1 * i)) ^ 2 / (H : ℝ) ^ 2 -
446 (2 * M / H) * (∑ i ∈ I, ∑ s ∈ T, ind (s ∣ n + h1 * i)) + M ^ 2 := by
447 field_simp
448 ring
449 rw [e2, e1]
450 ring
451
452/-- Swap the two index pairs of a four-fold sum, so the divisor pair `(s, s')` is outermost. -/
453theorem sum_swap4 (I T : Finset ℕ) (f : ℕ → ℕ → ℕ → ℕ → ℝ) :
454 ∑ i ∈ I, ∑ j ∈ I, ∑ s ∈ T, ∑ s' ∈ T, f i j s s' =
455 ∑ s ∈ T, ∑ s' ∈ T, ∑ i ∈ I, ∑ j ∈ I, f i j s s' := by
456 calc ∑ i ∈ I, ∑ j ∈ I, ∑ s ∈ T, ∑ s' ∈ T, f i j s s'
457 = ∑ i ∈ I, ∑ s ∈ T, ∑ j ∈ I, ∑ s' ∈ T, f i j s s' :=
458 sum_congr rfl fun i _ => sum_comm
459 _ = ∑ s ∈ T, ∑ i ∈ I, ∑ j ∈ I, ∑ s' ∈ T, f i j s s' := sum_comm
460 _ = ∑ s ∈ T, ∑ s' ∈ T, ∑ i ∈ I, ∑ j ∈ I, f i j s s' :=
461 sum_congr rfl fun s _ => by
462 calc ∑ i ∈ I, ∑ j ∈ I, ∑ s' ∈ T, f i j s s'
463 = ∑ i ∈ I, ∑ s' ∈ T, ∑ j ∈ I, f i j s s' :=
464 sum_congr rfl fun i _ => sum_comm
465 _ = ∑ s' ∈ T, ∑ i ∈ I, ∑ j ∈ I, f i j s s' := sum_comm
466
467end E424
468