import OAI.NumberTheory.PrimeGaps.SingularTails namespace OAI namespace LargePrimeGaps open Filter open Set Filter MeasureTheory open scoped Topology ContDiff noncomputable def distinctBox {s h : ℕ} (a : Fin s → ℤ) : Finset (Fin s → Fin h) := Finset.univ.filter fun n => Function.Injective (boxTuple a n) noncomputable def truncatedBoxMean {s h : ℕ} (a : Fin s → ℤ) (q : ℕ) : ℝ := (∑ n : Fin s → Fin h, siftedTuple (boxTuple a n) q)/(h:ℝ)^s noncomputable def distinctTruncatedBoxMean {s h : ℕ} (a : Fin s → ℤ) (q : ℕ) : ℝ := (∑ n ∈ distinctBox (h:=h) a, siftedTuple (boxTuple a n) q)/(h:ℝ)^s noncomputable def singularBoxMean {s h : ℕ} (a : Fin s → ℤ) : ℝ := (∑ n ∈ distinctBox (h:=h) a, singularSeries (Finset.univ.image (boxTuple a n)))/(h:ℝ)^s theorem siftedTuple_boxTuple {s h q : ℕ} [NeZero q] (a : Fin s → ℤ) (n : Fin s → Fin h) : siftedTuple (boxTuple a n) q = normalizedSiftedCount (residueTuple (q:=q) a n) := by rw [siftedTuple_eq] congr 1 funext i simp only [boxTuple, residueTuple, Int.cast_add, Int.cast_natCast] theorem truncatedBoxMean_bounds {s h q : ℕ} [NeZero q] (hh : 0 Function.Injective (boxTuple a n)) (fun n => siftedTuple (boxTuple a n) q) have hb : (∑ n ∈ Finset.univ.filter (fun n : Fin s → Fin h => ¬Function.Injective (boxTuple a n)), siftedTuple (boxTuple a n) q) ≤ (s:ℝ)^2*(h:ℝ)^(s-1)*(q:ℝ)^s := by calc _ ≤ ((Finset.univ.filter fun n : Fin s → Fin h => ¬Function.Injective (boxTuple a n)).card:ℝ) * (q:ℝ)^s := by simpa only [nsmul_eq_mul] using Finset.sum_le_card_nsmul _ _ ((q:ℝ)^s) (fun n _ => by rw [siftedTuple_eq] exact normalizedSiftedCount_le _) _ ≤ _ := mul_le_mul_of_nonneg_right (by exact_mod_cast boxTuple_nondistinct_count (h:=h) a) (by positivity) unfold truncatedBoxMean distinctTruncatedBoxMean distinctBox rw [← sub_div] calc _ ≤ ((s:ℝ)^2*(h:ℝ)^(s-1)*(q:ℝ)^s)/(h:ℝ)^s := by apply div_le_div_of_nonneg_right _ (by positivity) linarith [hsum] _ = _ := by have hp : (h:ℝ)^s=(h:ℝ)^(s-1)*(h:ℝ) := by rw [← pow_succ]; congr 1; omega rw [hp] field_simp noncomputable def boxTruncationError (s h q : ℕ) : ℝ := |(1-(q:ℝ)/h)^s-1| + |(1+(q:ℝ)/h)^s-1| + (s:ℝ)^2*(q:ℝ)^s/h theorem distinctTruncatedBoxMean_error {s h q : ℕ} [NeZero q] (hh : 0 (singularCutoff s h:ℝ)/Real.log h) atTop (𝓝 (1/singularCutoffScale s)) := by have hh := (tendsto_nat_floor_mul_div_atTop (a:=1/singularCutoffScale s) (one_div_nonneg.mpr (singularCutoffScale_pos s).le)).comp (Real.tendsto_log_atTop.comp (tendsto_natCast_atTop_atTop (R:=ℝ))) simpa only [singularCutoff, one_div_mul_eq_div, Function.comp_def] using hh theorem log_div_singularCutoff_tendsto (s : ℕ) : Tendsto (fun h : ℕ => Real.log h/(singularCutoff s h:ℝ)) atTop (𝓝 (singularCutoffScale s)) := by simpa only [inv_div, one_div, inv_inv] using (singularCutoff_ratio_tendsto s).inv₀ (one_div_ne_zero (singularCutoffScale_pos s).ne') theorem primorial_singularCutoff_power_bound (s : ℕ) {h : ℕ} (hh : 0 (primorial (singularCutoff s h):ℝ)^r/(h:ℝ)) atTop (𝓝 0) := by have ht : Tendsto (fun h : ℕ => (h:ℝ)^(-(1/2:ℝ))) atTop (𝓝 0) := (tendsto_rpow_neg_atTop (by norm_num : (0:ℝ)<1/2)).comp tendsto_natCast_atTop_atTop apply tendsto_of_tendsto_of_tendsto_of_le_of_le' tendsto_const_nhds ht · exact Filter.Eventually.of_forall fun h => by positivity · filter_upwards [eventually_ge_atTop 1] with h hh have hhR : (0:ℝ) (primorial (singularCutoff s h):ℝ)/(h:ℝ)) atTop (𝓝 0) := by simpa only [pow_one] using primorial_singularCutoff_ratio_tendsto s 1 (by omega) theorem log_mul_div_singularCutoff_tendsto (s : ℕ) {K : ℕ} (hK : 0 Real.log (K*h:ℕ)/(singularCutoff s h:ℝ)) atTop (𝓝 (singularCutoffScale s)) := by have hy := tendsto_natCast_atTop_atTop (R:=ℝ) |>.comp (singularCutoff_tendsto s) have hz : Tendsto (fun h : ℕ => Real.log (K:ℝ)/(singularCutoff s h:ℝ)) atTop (𝓝 0) := by simpa only [mul_zero, div_eq_mul_inv, Pi.inv_apply, Function.comp_apply] using hy.inv_tendsto_atTop.const_mul (Real.log (K:ℝ)) have ht := hz.add (log_div_singularCutoff_tendsto s) rw [zero_add] at ht apply ht.congr' filter_upwards [eventually_ge_atTop 1] with h hh rw [Nat.cast_mul, Real.log_mul (by exact_mod_cast hK.ne') (by exact_mod_cast (show h≠0 by omega)), add_div] theorem uniformSingularTailBound_tendsto (s : ℕ) {K : ℕ} (hK : 0 uniformSingularTailBound s (K*h) (singularCutoff s h)) atTop (𝓝 0) := by have hy := tendsto_natCast_atTop_atTop (R:=ℝ) |>.comp (singularCutoff_tendsto s) have hl := Real.tendsto_log_atTop.comp hy have ht := (hy.inv_tendsto_atTop.const_mul (2*((s:ℝ)^2*(2:ℝ)^s))).add (((log_mul_div_singularCutoff_tendsto s hK).mul hl.inv_tendsto_atTop).const_mul (4*(s:ℝ)*((s*(s-1):ℕ):ℝ))) simp only [mul_zero, add_zero] at ht convert ht using 1 ext h unfold uniformSingularTailBound simp only [div_eq_mul_inv, mul_inv_rev, Pi.inv_apply, Function.comp_apply] ring theorem boxTruncationError_tendsto (s : ℕ) : Tendsto (fun h : ℕ => boxTruncationError s h (primorial (singularCutoff s h))) atTop (𝓝 0) := by have hq := primorial_singularCutoff_div_tendsto s have hl := ((tendsto_const_nhds (x:=(1:ℝ)).sub hq).pow s).sub (tendsto_const_nhds (x:=(1:ℝ))) have hu := ((tendsto_const_nhds (x:=(1:ℝ)).add hq).pow s).sub (tendsto_const_nhds (x:=(1:ℝ))) have hd := (primorial_singularCutoff_ratio_tendsto s s (by omega)).const_mul ((s:ℝ)^2) have ht := (hl.abs.add hu.abs).add hd simp only [sub_zero, one_pow, sub_self, add_zero, abs_zero, mul_zero] at ht convert ht using 1 ext h unfold boxTruncationError ring noncomputable def singularBoxError (s K h : ℕ) : ℝ := boxTruncationError s h (primorial (singularCutoff s h)) + 2*uniformSingularTailBound s (K*h) (singularCutoff s h)* (1+(primorial (singularCutoff s h):ℝ)/h)^s theorem singularBoxError_tendsto (s : ℕ) {K : ℕ} (hK : 0.pow s)) simp only [mul_zero, zero_mul, add_zero] at ht exact ht theorem singularBoxMean_error_eventually (s : ℕ) {K : ℕ} (hK : 0 ((hh₀ h hh).1 a hd).trans_lt (hh₀ h hh).2⟩ end LargePrimeGaps end OAI