import DivisorMoments import Mathlib.Data.ZMod.Basic namespace ManuscriptResidues noncomputable section open scoped BigOperators open KKFiniteCoreDraft IncidenceMoments /-- Exact strict/closed middle band in manuscript equation (15). -/ def middleBand (z0 z1 : ℝ) (p : ℕ) : Prop := z0 < (p : ℝ) ∧ (p : ℝ) ≤ z1 instance (z0 z1 : ℝ) (p : ℕ) : Decidable (middleBand z0 z1 p) := inferInstanceAs (Decidable (_ ∧ _)) def base (p : ℕ) : Finset ℕ := {0, p - 2} def middle (p : ℕ) : Finset ℕ := {0, p - 2, 1, p - 1} def omegaSet (z0 z1 : ℝ) (p : ℕ) : Finset ℕ := if middleBand z0 z1 p then middle p else base p /-- Literal modular sets, including negative classes, from equation (15). -/ def paperSet (z0 z1 : ℝ) (p : ℕ) : Finset (ZMod p) := if middleBand z0 z1 p then {0, -2, 1, -1} else {0, -2} /-- Finite enumeration for a real cutoff; floor is not substituted for the comparison. -/ def primeCutoff (z : ℝ) : Finset ℕ := (Finset.range (⌊z⌋₊ + 1)).filter Nat.Prime def paperPrimes (y : ℝ) : Finset ℕ := primeCutoff (Real.sqrt y) theorem mem_primeCutoff (z : ℝ) (hz : 0 ≤ z) (p : ℕ) : p ∈ primeCutoff z ↔ Nat.Prime p ∧ (p : ℝ) ≤ z := by simp only [primeCutoff, Finset.mem_filter, Finset.mem_range] constructor · rintro ⟨hb, hp⟩ exact ⟨hp, (Nat.le_floor_iff hz).mp (by omega)⟩ · rintro ⟨hp, hb⟩ exact ⟨by have := (Nat.le_floor_iff hz).mpr hb; omega, hp⟩ theorem mem_paperPrimes (y : ℝ) (p : ℕ) : p ∈ paperPrimes y ↔ Nat.Prime p ∧ (p : ℝ) ≤ Real.sqrt y := mem_primeCutoff _ (Real.sqrt_nonneg y) p theorem paperPrimeFamily (y : ℝ) : PrimeFamily (paperPrimes y) := fun p hp => (mem_paperPrimes y p).mp hp |>.1 theorem omega_canonical (z0 z1 : ℝ) (p : ℕ) (hp : Nat.Prime p) (a : ℕ) (ha : a ∈ omegaSet z0 z1 p) : a < p := by have hp2 := hp.two_le unfold omegaSet at ha split_ifs at ha · simp only [middle, Finset.mem_insert, Finset.mem_singleton] at ha rcases ha with ha | ha | ha | ha <;> subst a <;> omega · simp only [base, Finset.mem_insert, Finset.mem_singleton] at ha rcases ha with ha | ha <;> subst a <;> omega /-- The Nat choices are the actual negative modular classes, not a cardinality surrogate. -/ theorem omega_image_eq_paper (z0 z1 : ℝ) (p : ℕ) (hp : Nat.Prime p) : (omegaSet z0 z1 p).image (fun a : ℕ => (a : ZMod p)) = paperSet z0 z1 p := by have h2 : ((p - 2 : ℕ) : ZMod p) = -2 := by rw [Nat.cast_sub hp.two_le] simp have h1 : ((p - 1 : ℕ) : ZMod p) = -1 := by rw [Nat.cast_sub (by have := hp.two_le; omega)] simp unfold omegaSet paperSet split_ifs <;> simp [base, middle, h2, h1] /-- Pointwise membership agrees with the manuscript's literal modular conditions. -/ theorem omega_mem_iff_paper (z0 z1 : ℝ) (p : ℕ) (hp : Nat.Prime p) (n : ℕ) : n % p ∈ omegaSet z0 z1 p ↔ (n : ZMod p) ∈ paperSet z0 z1 p := by rw [← omega_image_eq_paper z0 z1 p hp] constructor · intro hn exact Finset.mem_image.mpr ⟨n % p, hn, (ZMod.natCast_eq_natCast_iff' (n % p) n p).mpr (Nat.mod_mod n p)⟩ · intro hn obtain ⟨a, ha, heq⟩ := Finset.mem_image.mp hn have h := (ZMod.natCast_eq_natCast_iff' a n p).mp heq rw [Nat.mod_eq_of_lt (omega_canonical z0 z1 p hp a ha)] at h exact h ▸ ha theorem paper_card_eq_nat (z0 z1 : ℝ) (p : ℕ) (hp : Nat.Prime p) : (paperSet z0 z1 p).card = (omegaSet z0 z1 p).card := by rw [← omega_image_eq_paper z0 z1 p hp] apply Finset.card_image_iff.mpr intro a ha b hb heq have h := congrArg ZMod.val heq simpa only [ZMod.val_natCast_of_lt (omega_canonical z0 z1 p hp a ha), ZMod.val_natCast_of_lt (omega_canonical z0 z1 p hp b hb)] using h lemma base_card (p : ℕ) (hp : 3 ≤ p) : (base p).card = 2 := by have h0 : 0 ∉ ({p - 2} : Finset ℕ) := by simp only [Finset.mem_singleton]; omega rw [base, Finset.card_insert_of_notMem h0] simp lemma middle_card (p : ℕ) (hp : 5 ≤ p) : (middle p).card = 4 := by have h0 : 0 ∉ ({p - 2, 1, p - 1} : Finset ℕ) := by simp only [Finset.mem_insert, Finset.mem_singleton]; omega have h2 : p - 2 ∉ ({1, p - 1} : Finset ℕ) := by simp only [Finset.mem_insert, Finset.mem_singleton]; omega have h1 : 1 ∉ ({p - 1} : Finset ℕ) := by simp only [Finset.mem_singleton]; omega rw [middle, Finset.card_insert_of_notMem h0, Finset.card_insert_of_notMem h2, Finset.card_insert_of_notMem h1] simp theorem omega_two (z0 z1 : ℝ) (hz0 : 3 < z0) : omegaSet z0 z1 2 = {0} := by have hb : ¬ middleBand z0 z1 2 := by intro h; have := h.1; norm_num at this; linarith simp [omegaSet, hb, base] theorem omega_three (z0 z1 : ℝ) (hz0 : 3 < z0) : omegaSet z0 z1 3 = {0, 1} := by have hb : ¬ middleBand z0 z1 3 := by intro h; have := h.1; norm_num at this; linarith simp [omegaSet, hb, base] /-- Exact prime cardinalities from the actual branch, including both small primes. -/ theorem omega_card (z0 z1 : ℝ) (hz0 : 3 < z0) (p : ℕ) (hp : Nat.Prime p) : (omegaSet z0 z1 p).card = if p = 2 then 1 else if middleBand z0 z1 p then 4 else 2 := by by_cases h2 : p = 2 · subst p simp [omega_two z0 z1 hz0] by_cases h3 : p = 3 · subst p have hb : ¬ middleBand z0 z1 3 := by intro h; have := h.1; norm_num at this; linarith simp [omega_three z0 z1 hz0, hb] have hp5 := hp.five_le_of_ne_two_of_ne_three h2 h3 by_cases hb : middleBand z0 z1 p · simp [omegaSet, hb, h2, middle_card p hp5] · simp [omegaSet, hb, h2, base_card p (by omega)] theorem omega_bounds (z0 z1 : ℝ) (hz0 : 3 < z0) (p : ℕ) (hp : Nat.Prime p) : 1 ≤ (omegaSet z0 z1 p).card ∧ (omegaSet z0 z1 p).card ≤ 4 ∧ (omegaSet z0 z1 p).card < p := by by_cases h2 : p = 2 · subst p simp [omega_two z0 z1 hz0] by_cases h3 : p = 3 · subst p simp [omega_three z0 z1 hz0] have hp5 := hp.five_le_of_ne_two_of_ne_three h2 h3 rw [omega_card z0 z1 hz0 p hp, ite_eq_right h2] split_ifs <;> omega theorem omega_nonempty_proper (z0 z1 : ℝ) (hz0 : 3 < z0) (p : ℕ) (hp : Nat.Prime p) : (omegaSet z0 z1 p).Nonempty ∧ ∃ a : ℕ, a < p ∧ a ∉ omegaSet z0 z1 p := by classical obtain ⟨hpos, _, hlt⟩ := omega_bounds z0 z1 hz0 p hp constructor · exact Finset.card_pos.mp (by omega) · by_contra hn have hcover : Finset.range p ⊆ omegaSet z0 z1 p := by intro a ha by_contra hnot exact hn ⟨a, Finset.mem_range.mp ha, hnot⟩ have hc := Finset.card_le_card hcover rw [Finset.card_range] at hc omega /-- Local density margin claimed in §9, proved with the explicit finite threshold 3 omega_canonical z0 z1 p ((mem_paperPrimes y p).mp hp).1 a ha def realWeight (X : ℕ) (S : Finset ℕ) (Ω : ℕ → Finset ℕ) (t : ℕ) : ℝ := weight X S Ω t theorem real_weight_nonnegative (X : ℕ) (S : Finset ℕ) (Ω : ℕ → Finset ℕ) (t : ℕ) : 0 ≤ realWeight X S Ω t := weight_nonnegative X S Ω t theorem real_weight_zero_outside (X : ℕ) (S : Finset ℕ) (Ω : ℕ → Finset ℕ) (t : ℕ) (ht : t ∉ support X S Ω) : realWeight X S Ω t = 0 := by rw [realWeight, weight_eq_zero_of_not_mem X S Ω t ht] simp theorem real_weight_divisor_sum (X : ℕ) (S : Finset ℕ) (Ω : ℕ → Finset ℕ) (d : ℕ) : (∑ t ∈ support X S Ω, if d ∣ t then realWeight X S Ω t else 0) = ((∑ t ∈ support X S Ω, if d ∣ t then weight X S Ω t else 0 : ℕ) : ℝ) := by rw [Nat.cast_sum] apply Finset.sum_congr rfl intro t _ split_ifs <;> simp [realWeight] /-- Actual real cutoff, actual counting sets, native real finite weights, all divisors. -/ theorem paper_real_moment (X : ℕ) (y z0 z1 : ℝ) (d : ℕ) (hd : d ∣ period (paperPrimes y)) : |(∑ t ∈ support X (paperPrimes y) (omegaSet z0 z1), if d ∣ t then realWeight X (paperPrimes y) (omegaSet z0 z1) t else 0) - ((X : ℝ) / d) * (DivisorMoments.localG (omegaSet z0 z1) d : ℝ)| ≤ (DivisorMoments.localG (omegaSet z0 z1) d : ℝ) := by rw [real_weight_divisor_sum] exact DivisorMoments.real_divisor_moment_remainder X (paperPrimes y) (paperPrimeFamily y) (omegaSet z0 z1) (paper_canonical y z0 z1) d hd #print axioms mem_primeCutoff #print axioms mem_paperPrimes #print axioms paperPrimeFamily #print axioms omega_canonical #print axioms omega_image_eq_paper #print axioms omega_mem_iff_paper #print axioms paper_card_eq_nat #print axioms base_card #print axioms middle_card #print axioms omega_two #print axioms omega_three #print axioms omega_card #print axioms omega_bounds #print axioms omega_nonempty_proper #print axioms density_le_four_fifths #print axioms paper_canonical #print axioms real_weight_nonnegative #print axioms real_weight_zero_outside #print axioms real_weight_divisor_sum #print axioms paper_real_moment end end ManuscriptResidues