import Mathlib /-! # The Alternation Lemma for adjacent-kill runs (`research/kappa-not-L.md`) Job #22 (formalize). Everything is stated abstractly: no tile, no twin slots, no sieve. * A *kill residue* mod `p` is `0` or `-2` (the prime divides the slot, or the slot + 2). * Along a run of kills, the residue after the entering prime's shift walks on these two states, and the gap between consecutive killed slots is congruent mod `p` to the difference of their residues. Proved here: 1. `step_cases`, `step_from_zero`, `step_from_neg_two`: a step between kill residues is `0`, `2` or `-2`; from residue `0` only `0` or `-2` is legal, from `-2` only `0` or `+2`. 2. `alternation`, `alternation_classes`: two nonzero steps separated only by zero steps are negatives of each other, so the nonzero steps strictly alternate `+2, -2, +2, ...`; `two_ne_neg_two` shows the two classes differ once `5 ≤ p`. 3. The corollary on gaps that are multiples of 6 (twin-slot gaps for `x ≥ 5`), with the note's exact constants: the least positive such gap of class `-2` is `2p-2` (`p ≡ 1 mod 6`) or `4p-2` (`p ≡ 5`), of class `+2` it is `4p+2` or `2p+2`, of class `0` it is `6p`, each attained, and the two nonzero minima sum to `6p` in both cases. 4. `run_cost` (the note's Theorem A): two adjacent gaps of a run of kills sum to at least `6p`. No hypothesis that `p` is prime is used; `5 ≤ p` and `p % 6 ∈ {1, 5}` are all that the statements need. -/ namespace KappaNotL variable {p : ℕ} /-- A residue mod `p` is a kill residue when it is `0` or `-2`. -/ def IsKill (r : ZMod p) : Prop := r = 0 ∨ r = -2 /-! ## 1. Steps between kill residues -/ theorem step_cases {x y : ZMod p} (hx : IsKill x) (hy : IsKill y) : y - x = 0 ∨ y - x = 2 ∨ y - x = -2 := by rcases hx with rfl | rfl <;> rcases hy with rfl | rfl · left; ring · right; right; ring · right; left; ring · left; ring theorem step_from_zero {y : ZMod p} (hy : IsKill y) : y - 0 = 0 ∨ y - 0 = -2 := by rcases hy with rfl | rfl · left; ring · right; ring theorem step_from_neg_two {y : ZMod p} (hy : IsKill y) : y - (-2) = 0 ∨ y - (-2) = 2 := by rcases hy with rfl | rfl · right; ring · left; ring /-- In a two-element set, leaving `x` for `y` and then leaving `y` for `z` returns to `x`. -/ theorem kill_other {x y z : ZMod p} (hx : IsKill x) (hy : IsKill y) (hz : IsKill z) (hxy : y ≠ x) (hzy : z ≠ y) : z = x := by rcases hx with rfl | rfl <;> rcases hy with rfl | rfl <;> rcases hz with rfl | rfl <;> first | rfl | exact absurd rfl hxy | exact absurd rfl hzy theorem adjacent_opposite {x y z : ZMod p} (hx : IsKill x) (hy : IsKill y) (hz : IsKill z) (h1 : y - x ≠ 0) (h2 : z - y ≠ 0) : z - y = -(y - x) := by have hxy : y ≠ x := fun h => h1 (by rw [h, sub_self]) have hzy : z ≠ y := fun h => h2 (by rw [h, sub_self]) rw [kill_other hx hy hz hxy hzy] abel /-! ## 2. The Alternation Lemma -/ /-- Two nonzero steps of a kill sequence, separated only by zero steps, are negatives of each other. -/ theorem alternation (r : ℕ → ZMod p) (hk : ∀ i, IsKill (r i)) {i j : ℕ} (hij : i < j) (hi : r (i + 1) - r i ≠ 0) (hj : r (j + 1) - r j ≠ 0) (hz : ∀ k, i < k → k < j → r (k + 1) - r k = 0) : r (j + 1) - r j = -(r (i + 1) - r i) := by have hconst : ∀ k, i < k → k ≤ j → r k = r (i + 1) := by intro k induction k with | zero => intro h; exact absurd h (Nat.not_lt_zero i) | succ k ih => intro h1 h2 by_cases hki : k = i · subst hki; rfl · have hik : i < k := by omega rw [sub_eq_zero.mp (hz k hik (by omega)), ih hik (by omega)] have hrj : r j = r (i + 1) := hconst j hij le_rfl have hne1 : r (i + 1) ≠ r i := fun h => hi (by rw [h, sub_self]) have hne2 : r (j + 1) ≠ r (i + 1) := fun h => hj (by rw [h, hrj, sub_self]) rw [kill_other (hk i) (hk (i + 1)) (hk (j + 1)) hne1 hne2, hrj] abel /-- The nonzero steps alternate between the classes `+2` and `-2`. -/ theorem alternation_classes (r : ℕ → ZMod p) (hk : ∀ i, IsKill (r i)) {i j : ℕ} (hij : i < j) (hi : r (i + 1) - r i ≠ 0) (hj : r (j + 1) - r j ≠ 0) (hz : ∀ k, i < k → k < j → r (k + 1) - r k = 0) : (r (i + 1) - r i = 2 ∧ r (j + 1) - r j = -2) ∨ (r (i + 1) - r i = -2 ∧ r (j + 1) - r j = 2) := by have halt := alternation r hk hij hi hj hz rcases step_cases (hk i) (hk (i + 1)) with h | h | h · exact absurd h hi · exact Or.inl ⟨h, by rw [halt, h]⟩ · exact Or.inr ⟨h, by rw [halt, h]; ring⟩ theorem two_ne_zero_zmod (hp : 5 ≤ p) : (2 : ZMod p) ≠ 0 := by intro h have h2 : ((2 : ℕ) : ZMod p) = 0 := by exact_mod_cast h rw [CharP.cast_eq_zero_iff (ZMod p) p] at h2 exact absurd (Nat.le_of_dvd (by norm_num) h2) (by omega) theorem neg_two_ne_zero_zmod (hp : 5 ≤ p) : (-2 : ZMod p) ≠ 0 := by intro h exact two_ne_zero_zmod hp (by linear_combination -h) /-- The two nonzero classes are different residues once `5 ≤ p`. -/ theorem two_ne_neg_two (hp : 5 ≤ p) : (2 : ZMod p) ≠ -2 := by intro h have h4 : ((4 : ℕ) : ZMod p) = 0 := by push_cast linear_combination h rw [CharP.cast_eq_zero_iff (ZMod p) p] at h4 exact absurd (Nat.le_of_dvd (by norm_num) h4) (by omega) /-! ## 3. Least gaps that are multiples of 6, with exact constants -/ theorem classNeg2_min_p1 {g : ℕ} (hp : p % 6 = 1) (hg : 0 < g) (h6 : 6 ∣ g) (hc : p ∣ g + 2) : 2 * p - 2 ≤ g := by obtain ⟨k, hk⟩ := hc rcases Nat.lt_or_ge k 2 with hk2 | hk2 · interval_cases k <;> omega · obtain ⟨j, rfl⟩ : ∃ j, k = j + 2 := ⟨k - 2, by omega⟩ have e : p * (j + 2) = p * j + 2 * p := by ring rw [e] at hk generalize p * j = q at hk omega theorem classNeg2_min_p5 {g : ℕ} (hp : p % 6 = 5) (hg : 0 < g) (h6 : 6 ∣ g) (hc : p ∣ g + 2) : 4 * p - 2 ≤ g := by obtain ⟨k, hk⟩ := hc rcases Nat.lt_or_ge k 4 with hk4 | hk4 · interval_cases k <;> omega · obtain ⟨j, rfl⟩ : ∃ j, k = j + 4 := ⟨k - 4, by omega⟩ have e : p * (j + 4) = p * j + 4 * p := by ring rw [e] at hk generalize p * j = q at hk omega theorem classPos2_min_p1 {g : ℕ} (hp : p % 6 = 1) (h6 : 6 ∣ g) (hc : g % p = 2) : 4 * p + 2 ≤ g := by have hdm := Nat.div_add_mod g p rw [hc] at hdm generalize g / p = m at hdm rcases Nat.lt_or_ge m 4 with hm | hm · interval_cases m <;> omega · obtain ⟨j, rfl⟩ : ∃ j, m = j + 4 := ⟨m - 4, by omega⟩ have e : p * (j + 4) = p * j + 4 * p := by ring rw [e] at hdm generalize p * j = q at hdm omega theorem classPos2_min_p5 {g : ℕ} (hp : p % 6 = 5) (h6 : 6 ∣ g) (hc : g % p = 2) : 2 * p + 2 ≤ g := by have hdm := Nat.div_add_mod g p rw [hc] at hdm generalize g / p = m at hdm rcases Nat.lt_or_ge m 2 with hm | hm · interval_cases m <;> omega · obtain ⟨j, rfl⟩ : ∃ j, m = j + 2 := ⟨m - 2, by omega⟩ have e : p * (j + 2) = p * j + 2 * p := by ring rw [e] at hdm generalize p * j = q at hdm omega theorem class0_min {g : ℕ} (hp6 : p % 6 = 1 ∨ p % 6 = 5) (hg : 0 < g) (h6 : 6 ∣ g) (hc : p ∣ g) : 6 * p ≤ g := by obtain ⟨k, hk⟩ := hc rcases Nat.lt_or_ge k 6 with hk6 | hk6 · interval_cases k <;> omega · obtain ⟨j, rfl⟩ : ∃ j, k = j + 6 := ⟨k - 6, by omega⟩ have e : p * (j + 6) = p * j + 6 * p := by ring rw [e] at hk generalize p * j = q at hk omega /-- Attainment for `p ≡ 1 (mod 6)`: `2p-2` (class `-2`) and `4p+2` (class `+2`) are multiples of 6 of those classes, and they sum to `6p`. -/ theorem attain_p1 (hp : p % 6 = 1) (hp5 : 5 ≤ p) : (6 ∣ 2 * p - 2 ∧ p ∣ (2 * p - 2) + 2) ∧ (6 ∣ 4 * p + 2 ∧ (4 * p + 2) % p = 2) ∧ (2 * p - 2) + (4 * p + 2) = 6 * p := by refine ⟨⟨by omega, ⟨2, by omega⟩⟩, ⟨by omega, ?_⟩, by omega⟩ rw [show 4 * p + 2 = 2 + p * 4 by ring, Nat.add_mul_mod_self_left] exact Nat.mod_eq_of_lt (by omega) /-- Attainment for `p ≡ 5 (mod 6)`: `2p+2` (class `+2`) and `4p-2` (class `-2`), summing to `6p`. -/ theorem attain_p5 (hp : p % 6 = 5) : (6 ∣ 2 * p + 2 ∧ (2 * p + 2) % p = 2) ∧ (6 ∣ 4 * p - 2 ∧ p ∣ (4 * p - 2) + 2) ∧ (2 * p + 2) + (4 * p - 2) = 6 * p := by have hp5 : 5 ≤ p := by omega refine ⟨⟨by omega, ?_⟩, ⟨by omega, ⟨4, by omega⟩⟩, by omega⟩ rw [show 2 * p + 2 = 2 + p * 2 by ring, Nat.add_mul_mod_self_left] exact Nat.mod_eq_of_lt (by omega) theorem attain_class0 : 6 ∣ 6 * p ∧ p ∣ 6 * p := ⟨⟨p, by ring⟩, ⟨6, by ring⟩⟩ /-! ## 4. Theorem A: adjacent gaps of a run sum to at least `6p` -/ theorem cast_eq_zero_iff_dvd {g : ℕ} : ((g : ZMod p) = 0) ↔ p ∣ g := CharP.cast_eq_zero_iff (ZMod p) p g theorem cast_eq_neg_two_iff {g : ℕ} : ((g : ZMod p) = -2) ↔ p ∣ g + 2 := by rw [← CharP.cast_eq_zero_iff (ZMod p) p] push_cast constructor <;> intro h <;> linear_combination h theorem cast_eq_two_iff (hp : 5 ≤ p) {g : ℕ} : ((g : ZMod p) = 2) ↔ g % p = 2 := by have h2 : ((2 : ℕ) : ZMod p) = 2 := by norm_num rw [← h2, ZMod.natCast_eq_natCast_iff', Nat.mod_eq_of_lt (by omega : 2 < p)] theorem run_cost {g₁ g₂ : ℕ} (hp5 : 5 ≤ p) (hp6 : p % 6 = 1 ∨ p % 6 = 5) {r₀ r₁ r₂ : ZMod p} (h0 : IsKill r₀) (h1 : IsKill r₁) (h2 : IsKill r₂) (hg₁ : 0 < g₁) (hg₂ : 0 < g₂) (h6₁ : 6 ∣ g₁) (h6₂ : 6 ∣ g₂) (hc₁ : (g₁ : ZMod p) = r₁ - r₀) (hc₂ : (g₂ : ZMod p) = r₂ - r₁) : 6 * p ≤ g₁ + g₂ := by rcases step_cases h0 h1 with s1 | s1 | s1 · have := class0_min hp6 hg₁ h6₁ (cast_eq_zero_iff_dvd.mp (hc₁.trans s1)); omega · rcases step_cases h1 h2 with s2 | s2 | s2 · have := class0_min hp6 hg₂ h6₂ (cast_eq_zero_iff_dvd.mp (hc₂.trans s2)); omega · exfalso have hopp := adjacent_opposite h0 h1 h2 (by rw [s1]; exact two_ne_zero_zmod hp5) (by rw [s2]; exact two_ne_zero_zmod hp5) rw [s1, s2] at hopp exact two_ne_neg_two hp5 hopp · have a1 := (cast_eq_two_iff hp5).mp (hc₁.trans s1) have a2 := cast_eq_neg_two_iff.mp (hc₂.trans s2) rcases hp6 with h | h · have := classPos2_min_p1 h h6₁ a1; have := classNeg2_min_p1 h hg₂ h6₂ a2; omega · have := classPos2_min_p5 h h6₁ a1; have := classNeg2_min_p5 h hg₂ h6₂ a2; omega · rcases step_cases h1 h2 with s2 | s2 | s2 · have := class0_min hp6 hg₂ h6₂ (cast_eq_zero_iff_dvd.mp (hc₂.trans s2)); omega · have a1 := cast_eq_neg_two_iff.mp (hc₁.trans s1) have a2 := (cast_eq_two_iff hp5).mp (hc₂.trans s2) rcases hp6 with h | h · have := classNeg2_min_p1 h hg₁ h6₁ a1; have := classPos2_min_p1 h h6₂ a2; omega · have := classNeg2_min_p5 h hg₁ h6₁ a1; have := classPos2_min_p5 h h6₂ a2; omega · exfalso have hopp := adjacent_opposite h0 h1 h2 (by rw [s1]; exact neg_two_ne_zero_zmod hp5) (by rw [s2]; exact neg_two_ne_zero_zmod hp5) rw [s1, s2, neg_neg] at hopp exact two_ne_neg_two hp5 hopp.symm end KappaNotL