import Mathlib /-! # Fact A, Fact B and the Localized Merge Lemma of `research/LOCALIZED-GAP.md` Job #29 (formalize). The lemma is PROVEN in the note. The chain built on it is REFUTED (`research/OUTCOMES.md`, "the localized merge chain, telescoped to level x", 2026-08-17). Formalizing the lemma does not reopen that chain. **Reformulation, stated first.** * Twin slots are natural numbers `r` with `r` and `r + 2` coprime to `x#`. `coprime_primorial_iff` shows that coprime to `x#` is the same as `x`-rough, i.e. no prime factor `≤ x`. * `next x s` is the least twin slot of `T_x` above `s` (`Nat.find`; one exists by periodicity). `not_isSlot_of_lt_next` shows that no slot lies strictly between, so `s ↦ next x s` is the sorted successor. * `M x Y` is the maximum of `next x s − s` over slots `s < Y`. `maxsum2 x Y` is the maximum of `next x (next x s) − s` over slots `s < Y`. This is the left-endpoint rule (a) of §8, applied to a window's first gap. * `T_p` is `T_x` folded by `p`, for `p` the next prime after `x` (`isSlot_fold`). `x ≥ 3`; primality of `x` is not used. * **Boundary.** The hypothesis is placed on gaps starting below `Y + p`, not below `Y`. A new gap starting below `Y` fuses old gaps whose later members can start at or above `Y`. The note's §8 settles this empirically, so here it is assumed. * **Constant.** `localized_merge` needs only that those old gaps are shorter than `p − 2`. `localized_merge_quarter` is the note's `M ≤ (p − 2)/4` form, on the same buffered range. * **Route.** This differs from the note's partition count. By Fact B, two consecutive old slots cannot both be killed when their gap is below `p − 2`. So the next `T_p` slot after `s` is at most the second `T_x` successor of `s`. -/ open Finset namespace LocalizedGap /-- `r` is a twin slot of `T_x`: `r` and `r + 2` are both coprime to `x#`. -/ abbrev IsSlot (x r : ℕ) : Prop := Nat.Coprime r (primorial x) ∧ Nat.Coprime (r + 2) (primorial x) /-- `r` is killed by the fold by `p`: `r ≡ 0` or `r ≡ −2 (mod p)`. -/ abbrev Kill (p r : ℕ) : Prop := p ∣ r ∨ p ∣ r + 2 /-- Coprime to `y#` is the same as `y`-rough. -/ theorem coprime_primorial_iff (n y : ℕ) : Nat.Coprime n (primorial y) ↔ ∀ q, q.Prime → q ≤ y → ¬ q ∣ n := by constructor · intro h q hq hqy hqn have h1 : Nat.Coprime q (primorial y) := Nat.Coprime.coprime_dvd_left hqn h exact hq.one_lt.ne' (h1.eq_one_of_dvd (hq.dvd_primorial_iff.2 hqy)) · intro h apply Nat.coprime_of_dvd intro k hk hkn hkP exact h k hk (hk.dvd_primorial_iff.1 hkP) hkn /-! ## Fact A -/ /-- **Fact A.** For `x ≥ 3`, no two twin slots of `T_x` are 2 apart. -/ theorem factA {x s : ℕ} (hx : 3 ≤ x) (hs : IsSlot x s) : ¬ IsSlot x (s + 2) := by intro hs2 have h3 : ∀ n, Nat.Coprime n (primorial x) → ¬ 3 ∣ n := fun n hn => (coprime_primorial_iff n x).1 hn 3 Nat.prime_three hx have a := h3 s hs.1 have b := h3 (s + 2) hs.2 have c := h3 (s + 2 + 2) hs2.2 omega /-- The bound `x ≥ 3` is needed: `1` and `3` are both twin slots of `T_2`. -/ theorem factA_fails_at_two : IsSlot 2 1 ∧ IsSlot 2 3 := by simp only [IsSlot, primorial_two] decide /-! ## Fact B -/ /-- Folding by the next prime `p` deletes exactly the slots in the classes `{0, −2} mod p`. -/ theorem isSlot_fold {x p r : ℕ} (hp : p.Prime) (hxp : x < p) (hnext : ∀ q, q.Prime → x < q → p ≤ q) : IsSlot p r ↔ IsSlot x r ∧ ¬ Kill p r := by have key : ∀ n, Nat.Coprime n (primorial p) ↔ Nat.Coprime n (primorial x) ∧ ¬ p ∣ n := by intro n rw [coprime_primorial_iff, coprime_primorial_iff] constructor · intro h exact ⟨fun q hq hqx => h q hq (by omega), h p hp le_rfl⟩ · rintro ⟨h, hpn⟩ q hq hqp rcases Nat.lt_or_ge x q with hxq | hqx · have hqeq : q = p := le_antisymm hqp (hnext q hq hxq) rw [hqeq] exact hpn · exact h q hq hqx simp only [IsSlot, Kill, key, not_or] tauto lemma add_le_of_dvd_of_lt {p A B : ℕ} (hA : p ∣ A) (hB : p ∣ B) (h : A < B) : A + p ≤ B := by have := Nat.le_of_dvd (by omega) (Nat.dvd_sub hB hA) omega /-- **Fact B**, separation form: two killed twin slots `r < r'` of `T_x` satisfy `r' − r ≥ p − 2`. -/ theorem factB_sep {x p r r' : ℕ} (hx : 3 ≤ x) (hr : IsSlot x r) (hr' : IsSlot x r') (hk : Kill p r) (hk' : Kill p r') (hlt : r < r') : r + p ≤ r' + 2 := by rcases hk with h | h <;> rcases hk' with h' | h' · have := add_le_of_dvd_of_lt h h' hlt omega · have := add_le_of_dvd_of_lt h h' (by omega : r < r' + 2) omega · rcases Nat.lt_trichotomy (r + 2) r' with h2 | h2 | h2 · have := add_le_of_dvd_of_lt h h' h2 omega · have hr2 : IsSlot x (r + 2) := by rw [h2]; exact hr' exact absurd hr2 (factA hx hr) · have := add_le_of_dvd_of_lt h' h h2 omega · have := add_le_of_dvd_of_lt h h' (by omega : r + 2 < r' + 2) omega /-- **Fact B**, interval form: an interval `[a, a + L)` with `L ≤ p − 2` contains at most one kill. -/ theorem factB_interval {x p a L : ℕ} (hx : 3 ≤ x) (hL : L + 2 ≤ p) : ((Ico a (a + L)).filter (fun r => IsSlot x r ∧ Kill p r)).card ≤ 1 := by rw [Finset.card_le_one] intro r hr r' hr' simp only [Finset.mem_filter, Finset.mem_Ico] at hr hr' by_contra hne rcases Nat.lt_or_gt_of_ne hne with h | h · have := factB_sep hx hr.2.1 hr'.2.1 hr.2.2 hr'.2.2 h omega · have := factB_sep hx hr'.2.1 hr.2.1 hr'.2.2 hr.2.2 h omega /-! ## The sorted slot sequence, `M` and `maxsum₂` -/ theorem exists_slot_gt (x s : ℕ) : ∃ t, s < t ∧ IsSlot x t := by have hP : 1 ≤ primorial x := primorial_pos x have hK : s + 2 ≤ (s + 2) * primorial x := by nlinarith refine ⟨(s + 2) * primorial x - 1, by omega, ?_, ?_⟩ · apply Nat.coprime_of_dvd intro k hk h1 h2 have h3 : k ∣ (s + 2) * primorial x := Dvd.dvd.mul_left h2 _ have h4 := Nat.dvd_sub h3 h1 rw [show (s + 2) * primorial x - ((s + 2) * primorial x - 1) = 1 by omega] at h4 exact hk.one_lt.ne' (Nat.dvd_one.mp h4) · apply Nat.coprime_of_dvd intro k hk h1 h2 have h3 : k ∣ (s + 2) * primorial x := Dvd.dvd.mul_left h2 _ have h4 := Nat.dvd_sub h1 h3 rw [show (s + 2) * primorial x - 1 + 2 - (s + 2) * primorial x = 1 by omega] at h4 exact hk.one_lt.ne' (Nat.dvd_one.mp h4) /-- The next twin slot of `T_x` above `s`. -/ def next (x s : ℕ) : ℕ := Nat.find (exists_slot_gt x s) theorem lt_next (x s : ℕ) : s < next x s := (Nat.find_spec (exists_slot_gt x s)).1 theorem isSlot_next (x s : ℕ) : IsSlot x (next x s) := (Nat.find_spec (exists_slot_gt x s)).2 theorem next_le {x s t : ℕ} (h : s < t) (ht : IsSlot x t) : next x s ≤ t := Nat.find_min' _ ⟨h, ht⟩ /-- `next x s` is the successor: no twin slot lies strictly between `s` and `next x s`. -/ theorem not_isSlot_of_lt_next {x s t : ℕ} (h1 : s < t) (h2 : t < next x s) : ¬ IsSlot x t := fun ht => absurd (next_le h1 ht) (by omega) /-- `M(T_x, Y)`: the largest gap between consecutive twin slots of `T_x` among gaps starting below `Y`. -/ def M (x Y : ℕ) : ℕ := ((range Y).filter (IsSlot x)).sup fun s => next x s - s /-- `maxsum₂(T_x, Y)`: the largest sum of two consecutive gaps of `T_x`, the first starting below `Y`. -/ def maxsum2 (x Y : ℕ) : ℕ := ((range Y).filter (IsSlot x)).sup fun s => next x (next x s) - s /-! ## The Localized Merge Lemma -/ /-- **Localized Merge Lemma**, gap form. If every gap of `T_x` starting below `Y + p` is shorter than `p − 2`, then `M(T_p, Y) ≤ maxsum₂(T_x, Y)`. -/ theorem localized_merge {x p Y : ℕ} (hx : 3 ≤ x) (hp : p.Prime) (hxp : x < p) (hnext : ∀ q, q.Prime → x < q → p ≤ q) (hgap : ∀ s, IsSlot x s → s < Y + p → next x s - s + 2 < p) : M p Y ≤ maxsum2 x Y := by unfold M refine Finset.sup_le fun s hs => ?_ show next p s - s ≤ maxsum2 x Y rw [Finset.mem_filter, Finset.mem_range] at hs obtain ⟨hsY, hsp⟩ := hs have hsx : IsSlot x s := ((isSlot_fold hp hxp hnext).1 hsp).1 have hwin : next x (next x s) - s ≤ maxsum2 x Y := Finset.le_sup (f := fun s => next x (next x s) - s) (by rw [Finset.mem_filter, Finset.mem_range]; exact ⟨hsY, hsx⟩) have h1 := lt_next x s have h2 := lt_next x (next x s) by_cases hk : Kill p (next x s) · have hk2 : ¬ Kill p (next x (next x s)) := by intro hk2 have hsep := factB_sep hx (isSlot_next x s) (isSlot_next x (next x s)) hk hk2 h2 have hg1 := hgap s hsx (by omega) have hg2 := hgap (next x s) (isSlot_next x s) (by omega) omega have hslot2 : IsSlot p (next x (next x s)) := (isSlot_fold hp hxp hnext).2 ⟨isSlot_next x _, hk2⟩ have := next_le (by omega : s < next x (next x s)) hslot2 omega · have hslot1 : IsSlot p (next x s) := (isSlot_fold hp hxp hnext).2 ⟨isSlot_next x s, hk⟩ have := next_le h1 hslot1 omega /-- **Localized Merge Lemma**, `M` form: `M(T_x, Y + p) < p − 2` implies `M(T_p, Y) ≤ maxsum₂(T_x, Y)`. -/ theorem localized_merge_M {x p Y : ℕ} (hx : 3 ≤ x) (hp : p.Prime) (hxp : x < p) (hnext : ∀ q, q.Prime → x < q → p ≤ q) (hM : M x (Y + p) + 2 < p) : M p Y ≤ maxsum2 x Y := by refine localized_merge hx hp hxp hnext fun s hs hsY => ?_ have hle : next x s - s ≤ M x (Y + p) := Finset.le_sup (f := fun s => next x s - s) (by rw [Finset.mem_filter, Finset.mem_range]; exact ⟨hsY, hs⟩) omega /-- **Localized Merge Lemma**, the note's form on the buffered range: `M(T_x, Y + p) ≤ (p − 2)/4`. -/ theorem localized_merge_quarter {x p Y : ℕ} (hx : 3 ≤ x) (hp : p.Prime) (hxp : x < p) (hnext : ∀ q, q.Prime → x < q → p ≤ q) (hM : M x (Y + p) ≤ (p - 2) / 4) : M p Y ≤ maxsum2 x Y := by refine localized_merge_M hx hp hxp hnext ?_ omega end LocalizedGap