🏛️ Atlas✒️Formalizations.Combinatorics.Caballero.AngularSecondPeakOdd view on GitHub ↗31 theorems
/-Copyright (c) 2026 Carles Marín. All rights reserved.Released under Apache 2.0 license as described in the file LICENSE.Authors: Carles Marín, Claude (AI assistant)-/module public import Formalizations.Combinatorics.Caballero.AngularGeneralApublic import Formalizations.Combinatorics.Caballero.AngularSlopeTwopublic import Formalizations.Combinatorics.Caballero.AngularTwoSteppublic import Mathlib.Data.Rat.Floor /-!# The second peak for odd a: a two-step vector and a slope-2/q vector with the same Z (Caballero, Part IV) Theorem 18 of Part IV. For a = 2r+1 >= 3, q = a+2, l >= 1, e in {0,1} and Delta = l(a+1) - 1 + e <=b+1, put x = b - Delta, y = b - r, c = (x, y, b, ..., b), T = (l+1)q - 5 + e, k = qb + 2a - T - 1and g_h = clip floor((k+2h)/q). Then c and g are distinct realisable vectors with Z(c) = Z(g). Theproof follows the paper: the positive part of Z^r(g) is the multiset {{Phi(T-v) : v in Sigma, v <=T}} (Lemma 15, `CaballeroSlopeTwo.rows_slopeTwo`), where Sigma = {2t + qs : 0 <= t <= a, s >= 0} isnot a semigroup for odd a (2(q-1) is missing); its gaps are A = {1, 3, ..., 2r-1} and B = {q-2+nq :n >= 0} (proved as `mem_sigma_iff`, together with a closed form of Phi, `phiN_closed`). Thereflected gaps of B reproduce the extra copies coming from B (a reflection n -> l-1-n of the gapsbelow T), those of A remove Delta+r-1, ..., Delta-r+1, and what remains is the multiset of Lemma 16(`CaballeroTwoStep.lem_twostep`) with rho = r. Equality of Z follows from Lemma 7(`CaballeroGeneralA.rows_iff`). The imported modules are used, not copied; their definitions arelinked to the ones of this module by short bridge lemmas. Conventions: b is a natural number withonly Delta <= b+1 assumed (the paper fixes b >= 1; Delta >= a forces b >= a-1). The step to thelimit of Theorem 3 (count of the pairs (l, e) and the liminf) is not formalised. ## Main results * `CaballeroK6Odd.thm_K6odd`: Theorem 18: for a odd the two-step vector c and the slope-2/q vector g are distinct realisable vectors with Z(c) = Z(g)* `CaballeroK6Odd.mem_sigma_iff`: for odd a, v is in Sigma iff it is not a gap: not an odd number below 2r and not congruent to q-2 mod q* `CaballeroK6Odd.phiN_closed`: closed form of Phi(u) = #(Sigma cap [0,u]): Phi(u) + min(r, (u+1)/2) + (u+2)/q = u + 1* `CaballeroK6Odd.core`: the multiset {{Phi(T-v) : v in Sigma, v <= T}} equals the positive part of Z^r(c) of Lemma 16 with rho = r (the reflection of the gaps of Sigma) ## References * C. Marín, How Many Dyck Words Do the Divisors of an Integer Make? IV. The Primary Phase of the Ambiguity Profile for General a, draft (2 December 2026), unpublished.* C. Marín, How Many Dyck Words Do the Divisors of an Integer Make? Exact Counts and Quasipolynomial Growth (Part I), Zenodo (2026), doi:10.5281/zenodo.24536372 (concept DOI).-/ @[expose] public section namespace CaballeroK6Odd open CaballeroGeneralA CaballeroSlopeTwo Finset /-- The two-step vector `c = (x, y, b, …, b)`: `c_{-a} = x`, `c_{-a+1} = y`, `c_h = b` for`h ≥ 2 - a` (the coordinate `c_h` is stored at index `h + a`). -/def twoStepVec (a b : ℕ) (x y : ℤ) : Vec a :=  fun m => if (m : ℕ) = 0 then x else if (m : ℕ) = 1 then y else (b : ℤ) /-- The vector of slope `2/q`: `g_h = clip ⌊(k + 2h)/q⌋`, `|h| ≤ a`, with `clip(v) = min(b, max(-b-1, v))`(`CaballeroPeaks.clip`). -/noncomputable def slopeVec (a b q : ℕ) (k : ℤ) : Vec a :=  fun m => CaballeroPeaks.clip b ⌊((k + 2 * (((m : ℕ) : ℤ) - a) : ℤ) : ℚ) / (q : ℚ)⌋ /-! ### The gaps of `Σ` for `a = 2r+1`, `q = 2r+3` -/ /-- The gaps of `Σ = {2t + qs : 0 ≤ t ≤ a, s ≥ 0}` for `a = 2r+1`, `q = a+2 = 2r+3`: the odd numbers`1, 3, …, 2r-1` (the set `A` of the paper) and the numbers `≡ q - 2 (mod q)` (the set `B`). -/def IsGap (r v : ℕ) : Prop := (v % 2 = 1 ∧ v + 1 ≤ 2 * r) ∨ v % (2 * r + 3) = 2 * r + 1 instance (r : ℕ) : DecidablePred (IsGap r) := fun v => by unfold IsGap; infer_instance theorem mod_two_mul_ne (r t : ℕ) (ht : t ≤ 2 * r + 1) : (2 * t) % (2 * r + 3) ≠ 2 * r + 1 := by  by_cases h : 2 * t < 2 * r + 3  · rw [Nat.mod_eq_of_lt h]; omega  · rw [Nat.mod_eq_sub_mod (by omega), Nat.mod_eq_of_lt (by omega)]; omega /-- For odd `a = 2r+1` and `q = a+2`, `v ∈ Σ` exactly when `v` is not a gap. -/theorem mem_sigma_iff (r v : ℕ) :    v ∈ slope2_Sigma (2 * r + 1) (2 * r + 3) ↔ ¬ IsGap r v := by  constructor  · rintro ⟨t, s, ht, rfl⟩ hg    have hmod : (2 * t + (2 * r + 3) * s) % (2 * r + 3) = (2 * t) % (2 * r + 3) :=      Nat.add_mul_mod_self_left _ _ _    rcases hg with ⟨h1, h2⟩ | h    · rcases Nat.eq_zero_or_pos s with rfl | hs      · omega      · have : (2 * r + 3) * 1 ≤ (2 * r + 3) * s := Nat.mul_le_mul_left _ hs        omega    · rw [hmod] at h      exact mod_two_mul_ne r t ht h  · intro hg    have hq : 0 < 2 * r + 3 := by omega    have hdm := Nat.div_add_mod v (2 * r + 3)    have hlt := Nat.mod_lt v hq    unfold IsGap at hg    generalize v / (2 * r + 3) = s0 at hdm    generalize v % (2 * r + 3) = m at hdm hlt hg    subst hdm    have hB : m ≠ 2 * r + 1 := fun h => hg (Or.inr h)    by_cases hm2 : m % 2 = 0    · exact ⟨m / 2, s0, by omega, by omega⟩    · rcases s0 with _ | s1      · exact absurd (Or.inl ⟨by omega, by omega⟩) hg      · refine ⟨(m + (2 * r + 3)) / 2, s1, ?_, ?_⟩        · omega        · rw [Nat.mul_add, Nat.mul_one]; omega /-! ### `Φ(u) = #(Σ ∩ [0,u])` -/ /-- `Φ(u) = #(Σ ∩ [0,u])` for `u ≥ 0`, as the number of non-gaps in `[0,u]`. -/def phiN (r u : ℕ) : ℕ := ∑ v ∈ range (u + 1), if IsGap r v then 0 else 1 theorem phiN_zero (r : ℕ) : phiN r 0 = 1 := by  have : ¬ IsGap r 0 := by simp only [IsGap, Nat.zero_mod]; omega  simp [phiN, this] theorem phiN_succ (r u : ℕ) :    phiN r (u + 1) = phiN r u + if IsGap r (u + 1) then 0 else 1 := by  simp only [phiN]  rw [Finset.sum_range_succ _ (u + 1)] theorem dvd_iff (r w : ℕ) : (2 * r + 3) ∣ w + 2 ↔ w % (2 * r + 3) = 2 * r + 1 := by  have hq : 0 < 2 * r + 3 := by omega  have hdm := Nat.div_add_mod w (2 * r + 3)  have hlt := Nat.mod_lt w hq  constructor  · rintro ⟨d, hd⟩    generalize w / (2 * r + 3) = e at hdm    generalize w % (2 * r + 3) = m at hdm hlt ⊢    have h1 : e < d := by      by_contra hc      push Not at hc      have := Nat.mul_le_mul_left (2 * r + 3) hc      omega    have h2 : d < e + 2 := by      by_contra hc      push Not at hc      have := Nat.mul_le_mul_left (2 * r + 3) hc      rw [Nat.mul_add] at this      omega    obtain rfl : d = e + 1 := by omega    rw [Nat.mul_add] at hd    omega  · intro h    refine ⟨w / (2 * r + 3) + 1, ?_⟩    rw [Nat.mul_add]    omega /-- A closed form of `Φ`: `Φ(u) = u + 1 - min(r, ⌊(u+1)/2⌋) - ⌊(u+2)/q⌋` (the two subtracted termscount the gaps of `A` and of `B` up to `u`). -/theorem phiN_closed (r u : ℕ) :    phiN r u + min r ((u + 1) / 2) + (u + 2) / (2 * r + 3) = u + 1 := by  induction u with  | zero =>    rw [phiN_zero, Nat.div_eq_of_lt (by omega : 2 < 2 * r + 3)]    omega  | succ u ih =>    rw [phiN_succ]    have hsd : (u + 1 + 2) / (2 * r + 3)        = (u + 2) / (2 * r + 3) + if (2 * r + 3) ∣ u + 3 then 1 else 0 :=      @Nat.succ_div (u + 2) (2 * r + 3)    rw [hsd]    by_cases hB : (u + 1) % (2 * r + 3) = 2 * r + 1    · have hdv : (2 * r + 3) ∣ u + 3 := (dvd_iff r (u + 1)).mpr hB      have hg : IsGap r (u + 1) := Or.inr hB      have hle := Nat.mod_le (u + 1) (2 * r + 3)      simp only [hg, hdv, ↓reduceIte]      omega    · have hdv : ¬ (2 * r + 3) ∣ u + 3 := fun h => hB ((dvd_iff r (u + 1)).mp h)      by_cases hA : (u + 1) % 2 = 1 ∧ u + 2 ≤ 2 * r      · have hg : IsGap r (u + 1) := Or.inl ⟨hA.1, by omega⟩        simp only [hg, hdv, ↓reduceIte]        omega      · have hg : ¬ IsGap r (u + 1) := by          intro h          simp only [IsGap] at h          omega        simp only [hg, hdv, ↓reduceIte]        omega /-! ### Multiset bookkeeping -/ /-- `{{1, …, n}}` as a sum of singletons. -/def Sm (n : ℕ) : Multiset ℤ := ∑ j ∈ range n, ({(j : ℤ) + 1} : Multiset ℤ) theorem Sm_succ (n : ℕ) : Sm (n + 1) = Sm n + {(n : ℤ) + 1} := Finset.sum_range_succ _ _ theorem Icc_eq_map (n : ℕ) :    Multiset.Icc (1 : ℤ) n = (range n).val.map (fun j : ℕ => (j : ℤ) + 1) := by  rw [Multiset.Nodup.ext Multiset.nodup_Icc    (Multiset.Nodup.map (fun a b h => by simpa using h) (range n).nodup)]  intro v  simp only [Multiset.mem_Icc, Multiset.mem_map, mem_val, mem_range]  constructor  · rintro ⟨h1, h2⟩    exact ⟨(v - 1).toNat, by omega, by omega⟩  · rintro ⟨j, hj, rfl⟩    omega theorem Icc_eq_Sm (n : ℕ) : Multiset.Icc (1 : ℤ) n = Sm n := by  rw [Icc_eq_map, CaballeroTwoStep.map_eq_sum]  rfl theorem Icc_map_eq (n : ℕ) (f : ℤ → ℤ) :    (Multiset.Icc (1 : ℤ) n).map f = ∑ j ∈ range n, ({f ((j : ℤ) + 1)} : Multiset ℤ) := by  rw [Icc_eq_map, Multiset.map_map, CaballeroTwoStep.map_eq_sum]  rfl theorem Sm_add (m r : ℕ) :    Sm (m + 2 * r) = Sm m + (∑ j ∈ range r, ({(m : ℤ) + 2 * j + 1} : Multiset ℤ))      + ∑ j ∈ range r, ({(m : ℤ) + 2 * j + 2} : Multiset ℤ) := by  induction r with  | zero => simp  | succ r ih =>    have h : m + 2 * (r + 1) = (m + 2 * r + 1) + 1 := by ring    rw [h, Sm_succ, Sm_succ, ih, Finset.sum_range_succ, Finset.sum_range_succ]    have e1 : (((m + 2 * r + 1 : ℕ) : ℤ) + 1) = (m : ℤ) + 2 * (r : ℤ) + 2 := by push_cast; ring    have e2 : (((m + 2 * r : ℕ) : ℤ) + 1) = (m : ℤ) + 2 * (r : ℤ) + 1 := by push_cast; ring    rw [e1, e2]    abel /-- Counting `Φ(u)` along `u = 0, …, T`: the values form `{{1, …, Φ(T)}}` plus one extra copy of`Φ(g)` for every gap `g ≤ T` (`Φ` rises by one at each element of `Σ` and stalls at each gap). -/theorem W_eq (r T : ℕ) :    ∑ u ∈ range (T + 1), ({(phiN r u : ℤ)} : Multiset ℤ)      = Sm (phiN r T) + ∑ u ∈ range (T + 1),          (if IsGap r u then ({(phiN r u : ℤ)} : Multiset ℤ) else 0) := by  induction T with  | zero =>    have h0 : ¬ IsGap r 0 := by simp only [IsGap, Nat.zero_mod]; omega    simp [phiN_zero, Sm, h0]  | succ T ih =>    rw [Finset.sum_range_succ, ih,      Finset.sum_range_succ (fun u => if IsGap r u then ({(phiN r u : ℤ)} : Multiset ℤ) else 0)        (T + 1), phiN_succ r T]    by_cases hg : IsGap r (T + 1)    · simp only [hg, ↓reduceIte, Nat.add_zero]      abel    · simp only [hg, ↓reduceIte]      rw [Sm_succ]      push_cast      abel theorem gaps_sum (r ℓ ε Tn : ℕ) (hℓ : 1 ≤ ℓ) (hε : ε ≤ 1)    (hT : Tn + 5 = (2 * r + 3) * ℓ + (2 * r + 3) + ε) (F : ℕ → Multiset ℤ) :    ∑ u ∈ range (Tn + 1), (if IsGap r u then F u else 0)      = ∑ j ∈ range r, F (2 * j + 1) + ∑ n ∈ range ℓ, F (2 * r + 1 + (2 * r + 3) * n) := by  rw [← Finset.sum_filter]  have hqℓ : (2 * r + 3) * 1 ≤ (2 * r + 3) * ℓ := Nat.mul_le_mul_left _ hℓ  have hset : (range (Tn + 1)).filter (IsGap r) =      (range r).image (fun j => 2 * j + 1)        ∪ (range ℓ).image (fun n => 2 * r + 1 + (2 * r + 3) * n) := by    ext u    simp only [mem_filter, mem_range, mem_union, mem_image, IsGap]    constructor    · rintro ⟨hu, hA | hB⟩      · left; exact ⟨(u - 1) / 2, by omega, by omega⟩      · right        have h2 := Nat.div_add_mod u (2 * r + 3)        refine ⟨u / (2 * r + 3), ?_, by omega⟩        by_contra hc        push Not at hc        have h1 := Nat.mul_le_mul_left (2 * r + 3) hc        omega    · rintro (⟨j, hj, rfl⟩ | ⟨n, hn, rfl⟩)      · exact ⟨by omega, Or.inl ⟨by omega, by omega⟩⟩      · have h1 := Nat.mul_le_mul_left (2 * r + 3) (show n + 1 ≤ ℓ by omega)        rw [Nat.mul_add, Nat.mul_one] at h1        refine ⟨by omega, Or.inr ?_⟩        rw [Nat.add_mul_mod_self_left, Nat.mod_eq_of_lt (by omega)]  have hdisj : Disjoint ((range r).image (fun j => 2 * j + 1))      ((range ℓ).image (fun n => 2 * r + 1 + (2 * r + 3) * n)) := by    rw [Finset.disjoint_left]    intro u hu1 hu2    simp only [mem_image, mem_range] at hu1 hu2    obtain ⟨j, hj, rfl⟩ := hu1    obtain ⟨n, hn, h⟩ := hu2    omega  rw [hset, Finset.sum_union hdisj, Finset.sum_image, Finset.sum_image]  · intro x _ y _ h    have h' : (2 * r + 3) * x = (2 * r + 3) * y := by simpa using h    exact Nat.eq_of_mul_eq_mul_left (by omega) h'  · intro x _ y _ h    simpa using h /-! ### Values of `Φ` -/ theorem phi_A (r j : ℕ) (hj : j < r) : phiN r (2 * j + 1) = j + 1 := by  have h := phiN_closed r (2 * j + 1)  rw [Nat.div_eq_of_lt (by omega : 2 * j + 1 + 2 < 2 * r + 3)] at h  omega /-- `Φ(T)`: `T + 1 - r - ℓ`, equal to `Δ + r` in the notation of the paper. -/theorem phi_T (r ℓ ε Tn m : ℕ) (hℓ : 1 ≤ ℓ) (hε : ε ≤ 1)    (hT : Tn + 5 = (2 * r + 3) * ℓ + (2 * r + 3) + ε)    (hm : m + r + 1 = (2 * r + 2) * ℓ + ε) : phiN r Tn = m + 2 * r := by  have h := phiN_closed r Tn  have e1 : ℓ * (2 * r + 3) = (2 * r + 3) * ℓ := Nat.mul_comm _ _  have e2 : (ℓ + 1) * (2 * r + 3) = (2 * r + 3) * ℓ + (2 * r + 3) := by ring  have e3 : (2 * r + 3) * ℓ = (2 * r + 2) * ℓ + ℓ := by ring  have hqℓ : (2 * r + 3) * 1 ≤ (2 * r + 3) * ℓ := Nat.mul_le_mul_left _ hℓ  rw [Nat.div_eq_of_lt_le (by omega) (by omega : Tn + 2 < (ℓ + 1) * (2 * r + 3))] at h  omega /-- The reflected gaps of `A`: `Φ(T - (2(r-1-j)+1)) = Δ - r + 2j + 1`. -/theorem phi_Aprime (r ℓ ε Tn m j : ℕ) (hℓ : 1 ≤ ℓ) (hε : ε ≤ 1)    (hT : Tn + 5 = (2 * r + 3) * ℓ + (2 * r + 3) + ε)    (hm : m + r + 1 = (2 * r + 2) * ℓ + ε) (hj : j < r) :    phiN r (Tn - (2 * (r - 1 - j) + 1)) = m + 2 * j + 1 := by  have e1 : ℓ * (2 * r + 3) = (2 * r + 3) * ℓ := Nat.mul_comm _ _  have e2 : (ℓ + 1) * (2 * r + 3) = (2 * r + 3) * ℓ + (2 * r + 3) := by ring  have e3 : (2 * r + 3) * ℓ = (2 * r + 2) * ℓ + ℓ := by ring  have hqℓ : (2 * r + 3) * 1 ≤ (2 * r + 3) * ℓ := Nat.mul_le_mul_left _ hℓ  have h := phiN_closed r (Tn - (2 * (r - 1 - j) + 1))  rw [Nat.div_eq_of_lt_le (by omega)    (by omega : Tn - (2 * (r - 1 - j) + 1) + 2 < (ℓ + 1) * (2 * r + 3))] at h  omega /-- The reflected gaps of `B`: `Φ(T - g_n) = Φ(g_{ℓ-1-n})` (`g_n = q - 2 + nq`): the two indicesdiffer by `1 - ε`, and `Φ` does not move at the gap `g`. -/theorem phi_B (r ℓ ε Tn n n' : ℕ) (hε : ε ≤ 1)    (hT : Tn + 5 = (2 * r + 3) * ℓ + (2 * r + 3) + ε) (hn : n + n' + 1 = ℓ) :    phiN r (Tn - (2 * r + 1 + (2 * r + 3) * n)) = phiN r (2 * r + 1 + (2 * r + 3) * n') := by  have hq : (2 * r + 3) * ℓ = (2 * r + 3) * n + (2 * r + 3) * n' + (2 * r + 3) := by    rw [← hn]; ring  have hu : Tn - (2 * r + 1 + (2 * r + 3) * n) + 1 = 2 * r + 1 + (2 * r + 3) * n' + ε := by    omega  have hgap : IsGap r (2 * r + 1 + (2 * r + 3) * n') := by    right    rw [Nat.add_mul_mod_self_left, Nat.mod_eq_of_lt (by omega)]  rcases Nat.le_one_iff_eq_zero_or_eq_one.mp hε with h0 | h1  · subst h0    have e : 2 * r + 1 + (2 * r + 3) * n'        = (Tn - (2 * r + 1 + (2 * r + 3) * n)) + 1 := by omega    rw [e] at hgap ⊢    rw [phiN_succ]    simp only [hgap, ↓reduceIte, Nat.add_zero]  · subst h1    congr 1    omega /-! ### The reflection of the gaps: the positive part of `Zʳ(g)` -/ /-- The multiset `{{Φ(T - v) : v ∈ Σ, v ≤ T}}` of Lemma 15 equals the positive part of `Zʳ(c)` ofLemma 16 (with `ρ = r`, `Δ = m + r`): the indices `T - v`, `v` a gap, remove the extra copies. -/theorem core (r ℓ ε Tn m : ℕ) (hℓ : 1 ≤ ℓ) (hε : ε ≤ 1)    (hT : Tn + 5 = (2 * r + 3) * ℓ + (2 * r + 3) + ε)    (hm : m + r + 1 = (2 * r + 2) * ℓ + ε) :    ∑ v ∈ range (Tn + 1), (if IsGap r v then 0 else ({(phiN r (Tn - v) : ℤ)} : Multiset ℤ))      = Sm r + Sm m + ∑ j ∈ range r, ({(m : ℤ) + 2 * ((j : ℤ) + 1)} : Multiset ℤ) := by  have hW := W_eq r Tn  have hrefl : ∑ u ∈ range (Tn + 1), ({(phiN r u : ℤ)} : Multiset ℤ)      = ∑ v ∈ range (Tn + 1), ({(phiN r (Tn - v) : ℤ)} : Multiset ℤ) := by    have := Finset.sum_range_reflect (fun u => ({(phiN r u : ℤ)} : Multiset ℤ)) (Tn + 1)    simpa using this.symm  have hsplit : ∑ v ∈ range (Tn + 1), ({(phiN r (Tn - v) : ℤ)} : Multiset ℤ)      = ∑ v ∈ range (Tn + 1),          (if IsGap r v then 0 else ({(phiN r (Tn - v) : ℤ)} : Multiset ℤ))        + ∑ v ∈ range (Tn + 1),          (if IsGap r v then ({(phiN r (Tn - v) : ℤ)} : Multiset ℤ) else 0) := by    rw [← Finset.sum_add_distrib]    apply Finset.sum_congr rfl    intro v _    split_ifs <;> simp  have hG : ∑ u ∈ range (Tn + 1),      (if IsGap r u then ({(phiN r u : ℤ)} : Multiset ℤ) else 0)      = ∑ j ∈ range r, ({(phiN r (2 * j + 1) : ℤ)} : Multiset ℤ)        + ∑ n ∈ range ℓ, ({(phiN r (2 * r + 1 + (2 * r + 3) * n) : ℤ)} : Multiset ℤ) :=    gaps_sum r ℓ ε Tn hℓ hε hT (fun u => ({(phiN r u : ℤ)} : Multiset ℤ))  have hG' : ∑ v ∈ range (Tn + 1),      (if IsGap r v then ({(phiN r (Tn - v) : ℤ)} : Multiset ℤ) else 0)      = ∑ j ∈ range r, ({(phiN r (Tn - (2 * j + 1)) : ℤ)} : Multiset ℤ)        + ∑ n ∈ range ℓ,            ({(phiN r (Tn - (2 * r + 1 + (2 * r + 3) * n)) : ℤ)} : Multiset ℤ) :=    gaps_sum r ℓ ε Tn hℓ hε hT (fun v => ({(phiN r (Tn - v) : ℤ)} : Multiset ℤ))  -- the gaps of `A`  have hA : ∑ j ∈ range r, ({(phiN r (2 * j + 1) : ℤ)} : Multiset ℤ) = Sm r := by    apply Finset.sum_congr rfl    intro j hj    rw [phi_A r j (mem_range.mp hj)]    push_cast    rfl  have hA' : ∑ j ∈ range r, ({(phiN r (Tn - (2 * j + 1)) : ℤ)} : Multiset ℤ)      = ∑ j ∈ range r, ({(m : ℤ) + 2 * j + 1} : Multiset ℤ) := by    rw [← Finset.sum_range_reflect (fun j => ({(phiN r (Tn - (2 * j + 1)) : ℤ)} : Multiset ℤ)) r]    apply Finset.sum_congr rfl    intro j hj    rw [phi_Aprime r ℓ ε Tn m j hℓ hε hT hm (mem_range.mp hj)]    push_cast    rfl  -- the gaps of `B`  have hB : ∑ n ∈ range ℓ, ({(phiN r (Tn - (2 * r + 1 + (2 * r + 3) * n)) : ℤ)} : Multiset ℤ)      = ∑ n ∈ range ℓ, ({(phiN r (2 * r + 1 + (2 * r + 3) * n) : ℤ)} : Multiset ℤ) := by    rw [← Finset.sum_range_reflect      (fun n => ({(phiN r (2 * r + 1 + (2 * r + 3) * n) : ℤ)} : Multiset ℤ)) ℓ]    apply Finset.sum_congr rfl    intro n hn    have hn' := mem_range.mp hn    rw [phi_B r ℓ ε Tn n (ℓ - 1 - n) hε hT (by omega)]  have hphi := phi_T r ℓ ε Tn m hℓ hε hT hm  have key : (∑ v ∈ range (Tn + 1),        (if IsGap r v then 0 else ({(phiN r (Tn - v) : ℤ)} : Multiset ℤ)))      + (∑ j ∈ range r, ({(m : ℤ) + 2 * j + 1} : Multiset ℤ)        + ∑ n ∈ range ℓ, ({(phiN r (2 * r + 1 + (2 * r + 3) * n) : ℤ)} : Multiset ℤ))      = (Sm r + Sm m + ∑ j ∈ range r, ({(m : ℤ) + 2 * ((j : ℤ) + 1)} : Multiset ℤ))        + (∑ j ∈ range r, ({(m : ℤ) + 2 * j + 1} : Multiset ℤ)        + ∑ n ∈ range ℓ, ({(phiN r (2 * r + 1 + (2 * r + 3) * n) : ℤ)} : Multiset ℤ)) := by    have e2 : ∑ j ∈ range r, ({(m : ℤ) + 2 * ((j : ℤ) + 1)} : Multiset ℤ)        = ∑ j ∈ range r, ({(m : ℤ) + 2 * j + 2} : Multiset ℤ) := by      apply Finset.sum_congr rfl      intro j _      congr 1    calc _ = (∑ v ∈ range (Tn + 1),            (if IsGap r v then 0 else ({(phiN r (Tn - v) : ℤ)} : Multiset ℤ)))          + ∑ v ∈ range (Tn + 1),            (if IsGap r v then ({(phiN r (Tn - v) : ℤ)} : Multiset ℤ) else 0) := by            rw [hG', hA', hB]      _ = ∑ v ∈ range (Tn + 1), ({(phiN r (Tn - v) : ℤ)} : Multiset ℤ) := hsplit.symm      _ = ∑ u ∈ range (Tn + 1), ({(phiN r u : ℤ)} : Multiset ℤ) := hrefl.symm      _ = Sm (m + 2 * r) + (Sm r            + ∑ n ∈ range ℓ, ({(phiN r (2 * r + 1 + (2 * r + 3) * n) : ℤ)} : Multiset ℤ)) := by            rw [hW, hG, hA, hphi]      _ = _ := by            rw [Sm_add, e2]            abel  exact add_right_cancel key /-! ### Bridges to the modules of Lemmas 7, 15 and 16 -/ theorem phi_link (r u : ℕ) : slope2_Phi (2 * r + 1) (2 * r + 3) (u : ℤ) = phiN r u := by  unfold slope2_Phi  simp only [Int.natCast_nonneg, ↓reduceIte]  have hset : {v : ℕ | v ∈ slope2_Sigma (2 * r + 1) (2 * r + 3) ∧ (v : ℤ) ≤ u}      = ↑((range (u + 1)).filter (fun v => ¬ IsGap r v)) := by    ext v    simp only [Finset.coe_filter, mem_range, Set.mem_ofPred_eq, mem_sigma_iff]    constructor    · rintro ⟨h1, h2⟩      exact ⟨by omega, h1⟩    · rintro ⟨h1, h2⟩      exact ⟨h2, by omega⟩  rw [hset, Set.ncard_coe_finset, Finset.card_filter]  apply Finset.sum_congr rfl  intro v _  by_cases h : IsGap r v <;> simp [h] open Classical in/-- Lemma 15 for `a = 2r+1`, `q = 2r+3`, in the form used here. -/theorem rows_sum (r b Tn : ℕ) (k : ℤ) (hk : 2 * ((2 * r + 1 : ℕ) : ℤ) ≤ k)    (hT : slope2_T (2 * r + 1) b (2 * r + 3) k = (Tn : ℤ))    (hT' : (Tn : ℤ) < ((2 * r + 3 : ℕ) : ℤ) * (b + 1)) :    (slope2_Zr (2 * r + 1) b (slope2_g b (2 * r + 3) k)).filter (fun x => 0 < x)      = ∑ v ∈ range (Tn + 1), (if IsGap r v then 0 else ({(phiN r (Tn - v) : ℤ)} : Multiset ℤ)) := by  have hq : Odd (2 * r + 3) := ⟨r + 1, by ring⟩  rw [rows_slopeTwo (2 * r + 1) b (2 * r + 3) k hq (by omega) hk (by rw [hT]; exact hT'), hT,    Int.toNat_natCast, CaballeroTwoStep.map_eq_sum, Finset.sum_filter]  apply Finset.sum_congr rfl  intro v hv  have hv' := mem_range.mp hv  by_cases hg : IsGap r v  · have : ¬ (v ∈ slope2_Sigma (2 * r + 1) (2 * r + 3) ∧ (v : ℤ) ≤ (Tn : ℤ)) := by      rw [mem_sigma_iff]      exact fun h => h.1 hg    simp only [this, hg, ↓reduceIte]  · have : (v ∈ slope2_Sigma (2 * r + 1) (2 * r + 3) ∧ (v : ℤ) ≤ (Tn : ℤ)) :=      ⟨(mem_sigma_iff r v).mpr hg, by omega⟩    simp only [this, hg, and_self, ↓reduceIte]    have e : (Tn : ℤ) - (v : ℤ) = ((Tn - v : ℕ) : ℤ) := by push_cast [Nat.cast_sub (by omega : v ≤ Tn)]; ring    rw [e, phi_link] theorem R_nonneg (a b : ℕ) (c : Vec a) (i j : ℕ) : 0 ≤ R a b c i j :=  Finset.sum_nonneg fun _ _ => le_min (by positivity) (le_max_left _ _) theorem Zr_nonneg (a b : ℕ) (c : Vec a) : ∀ x ∈ Zr a b c, 0 ≤ x := by  intro x hx  unfold Zr at hx  obtain ⟨p, _, rfl⟩ := Multiset.mem_map.mp hx  exact R_nonneg a b c p.1 p.2 theorem Zr_card (a b : ℕ) (c : Vec a) : Multiset.card (Zr a b c) = (a + 1) * (b + 1) := by  rw [Zr, Multiset.card_map, Finset.card_val, Finset.card_product, Finset.card_range,    Finset.card_range] /-- Two multisets of nonnegative integers with the same cardinality and the same positive part areequal. -/theorem eq_of_filter_pos (s t : Multiset ℤ) (hs : ∀ x ∈ s, 0 ≤ x) (ht : ∀ x ∈ t, 0 ≤ x)    (hc : Multiset.card s = Multiset.card t)    (hf : s.filter (fun x => 0 < x) = t.filter (fun x => 0 < x)) : s = t := by  have hz : ∀ u : Multiset ℤ, (∀ x ∈ u, 0 ≤ x) →      u.filter (fun x => ¬ 0 < x) = Multiset.replicate (Multiset.card (u.filter (fun x => ¬ 0 < x))) 0 := by    intro u hu    apply Multiset.eq_replicate.mpr    refine ⟨rfl, ?_⟩    intro x hx    have := Multiset.mem_filter.mp hx    have := hu x this.1    omega  have hs' := Multiset.filter_add_not (fun x => 0 < x) s  have ht' := Multiset.filter_add_not (fun x => 0 < x) t  have hcs := congrArg Multiset.card hs'  have hct := congrArg Multiset.card ht'  rw [Multiset.card_add] at hcs hct  have hn : Multiset.card (s.filter (fun x => ¬ 0 < x)) = Multiset.card (t.filter (fun x => ¬ 0 < x)) := by    rw [hf] at hcs    omega  rw [← hs', ← ht', hf, hz s hs, hz t ht, hn] theorem slopeVec_coord (a b q : ℕ) (k h : ℤ) (h1 : -(a : ℤ) ≤ h) (h2 : h ≤ a) :    coord (slopeVec a b q k) h = slope2_g b q k h := by  have e : ((((h + a).toNat : ℕ)) : ℤ) = h + a := Int.toNat_of_nonneg (by omega)  simp only [coord, slopeVec, slope2_g, slope2_clip, CaballeroPeaks.clip, h1, h2, and_self, ↓reduceDIte,    e, add_sub_cancel_right] theorem Zr_slopeVec (a b q : ℕ) (k : ℤ) :    Zr a b (slopeVec a b q k) = slope2_Zr a b (slope2_g b q k) := by  unfold Zr slope2_Zr  apply Multiset.map_congr rfl  intro p hp  have hp' := Finset.mem_product.mp (mem_val.mp hp)  have hi := mem_range.mp hp'.1  unfold R slope2_row  apply Finset.sum_congr rfl  intro h hh  have hh' := mem_Icc.mp hh  rw [slopeVec_coord a b q k h (by omega) (by omega)] theorem twoStepVec_eq (a b r : ℕ) (x y : ℤ) (hy : y = (b : ℤ) - r) :    twoStepVec a b x y = CaballeroTwoStep.twoStep a b r x := by  subst hy  rfl theorem floor_real (N : ℤ) (q : ℕ) (hq : 0 < q) :    ⌊((N : ℝ) + 1 / 2) / (q : ℝ)⌋ = N / (q : ℤ) := by  have hq' : (0 : ℝ) < q := by exact_mod_cast hq  have hq'' : (0 : ℤ) < q := by exact_mod_cast hq  have hde := Int.mul_ediv_add_emod N q  have h0 := Int.emod_nonneg N hq''.ne'  have h1 := Int.emod_lt_of_pos N hq''  generalize N / (q : ℤ) = d at hde ⊢  generalize N % (q : ℤ) = e at hde h0 h1  rw [Int.floor_eq_iff]  have hN : (N : ℝ) = (q : ℝ) * d + e := by exact_mod_cast hde.symm  have h0' : (0 : ℝ) ≤ e := by exact_mod_cast h0  have h1' : (e : ℝ) + 1 ≤ q := by exact_mod_cast h1  constructor  · rw [le_div_iff₀ hq']    nlinarith  · rw [div_lt_iff₀ hq']    nlinarith theorem slopeVec_realisable (a b q : ℕ) (hq : 0 < q) (k : ℤ) (hk : 0 < k) :    Realisable a b (slopeVec a b q k) := by  have hq' : (0 : ℝ) < q := by exact_mod_cast hq  have hk' : (0 : ℝ) < k := by exact_mod_cast hk  refine ⟨((k : ℝ) + 1 / 2) / q, 2 / q, by positivity, by positivity, ?_, ?_⟩  · intro h _ _ z hz    have e : ((k : ℝ) + 1 / 2) / q + (h : ℝ) * (2 / q) = (((k + 2 * h : ℤ) : ℝ) + 1 / 2) / q := by      push_cast; ring    rw [e, div_eq_iff hq'.ne'] at hz    push_cast at hz    have hz2 : ((2 * (k + 2 * h) + 1 : ℤ) : ℝ) = ((2 * (z * q) : ℤ) : ℝ) := by      push_cast      linarith    have hz3 := Int.cast_injective hz2    omega  · funext m    have e : ((k : ℝ) + 1 / 2) / q + ((((m : ℕ) : ℤ) - a : ℤ) : ℝ) * (2 / q)        = (((k + 2 * (((m : ℕ) : ℤ) - a) : ℤ) : ℝ) + 1 / 2) / q := by      push_cast; ring    simp only [cellVec, slopeVec]    rw [e, floor_real _ q hq, Rat.floor_intCast_div_natCast] /-! ### Theorem 18 -/ theorem thm_K6odd_aux (r b ℓ ε Tn m : ℕ) (hr : 1 ≤ r) (hℓ : 1 ≤ ℓ) (hε : ε ≤ 1)    (hT : Tn + 5 = (2 * r + 3) * ℓ + (2 * r + 3) + ε)    (hm : m + r + 1 = (2 * r + 2) * ℓ + ε) (hb : m + r ≤ b + 1) (k : ℤ)    (hk : k = ((2 * r + 3 : ℕ) : ℤ) * b + 2 * ((2 * r + 1 : ℕ) : ℤ) - Tn - 1) :    Realisable (2 * r + 1) b (twoStepVec (2 * r + 1) b ((b : ℤ) - (m + r)) ((b : ℤ) - r)) ∧      Realisable (2 * r + 1) b (slopeVec (2 * r + 1) b (2 * r + 3) k) ∧      twoStepVec (2 * r + 1) b ((b : ℤ) - (m + r)) ((b : ℤ) - r)        ≠ slopeVec (2 * r + 1) b (2 * r + 3) k ∧      Z (2 * r + 1) b (twoStepVec (2 * r + 1) b ((b : ℤ) - (m + r)) ((b : ℤ) - r))        = Z (2 * r + 1) b (slopeVec (2 * r + 1) b (2 * r + 3) k) := by  have e1 : (2 * r + 3) * 1 ≤ (2 * r + 3) * ℓ := Nat.mul_le_mul_left _ hℓ  have e2 : (2 * r + 2) * 1 ≤ (2 * r + 2) * ℓ := Nat.mul_le_mul_left _ hℓ  have hmr : r + 1 ≤ m := by omega  -- integer versions  have hTz : (Tn : ℤ) + 5 = (2 * r + 3) * ℓ + (2 * r + 3) + ε := by exact_mod_cast hT  have hmz : (m : ℤ) + r + 1 = (2 * r + 2) * ℓ + ε := by exact_mod_cast hm  have hbz : (m : ℤ) + r ≤ b + 1 := by exact_mod_cast hb  have hε0 : (0 : ℤ) ≤ ε := by positivity  have hrz : (1 : ℤ) ≤ r := by exact_mod_cast hr  have hℓz : (1 : ℤ) ≤ ℓ := by exact_mod_cast hℓ  -- `k ≥ 2a` and `T < q(b+1)`  have hA : (3 : ℤ) ≤ (2 * r + 1) * ℓ := by nlinarith  have hB : 0 ≤ ((2 * r + 3 : ℤ)) * (b + 2 - (2 * r + 2) * ℓ - ε) :=    mul_nonneg (by positivity) (by linarith)  have hC : ((2 * r + 3 : ℤ)) * 3 ≤ (2 * r + 3) * ((2 * r + 1) * ℓ) :=    mul_le_mul_of_nonneg_left hA (by positivity)  have hqb : (Tn : ℤ) + 1 ≤ (2 * r + 3) * b := by nlinarith  have hk2 : 2 * ((2 * r + 1 : ℕ) : ℤ) ≤ k := by    rw [hk]; push_cast at hqb ⊢; linarith  have hTT : slope2_T (2 * r + 1) b (2 * r + 3) k = (Tn : ℤ) := by    unfold slope2_T; rw [hk]; push_cast; ring  have hTlt : (Tn : ℤ) < ((2 * r + 3 : ℕ) : ℤ) * (b + 1) := by    push_cast; nlinarith  -- the vectors  have hc : Realisable (2 * r + 1) b (twoStepVec (2 * r + 1) b ((b : ℤ) - (m + r)) ((b : ℤ) - r)) := by    rw [twoStepVec_eq _ _ r _ _ rfl]    exact CaballeroTwoStep.twoStep_realisable (2 * r + 1) b r _ (by omega) hr (by omega) (by omega)      (by omega)  have hg : Realisable (2 * r + 1) b (slopeVec (2 * r + 1) b (2 * r + 3) k) :=    slopeVec_realisable _ _ _ (by omega) k (by push_cast at hk2; omega)  refine ⟨hc, hg, ?_, ?_⟩  · -- distinct: the coordinate `h = -a`    intro hcg    have h0 := congrFun hcg ⟨0, by omega⟩    have hl : twoStepVec (2 * r + 1) b ((b : ℤ) - (m + r)) ((b : ℤ) - r) ⟨0, by omega⟩        = (b : ℤ) - (m + r) := by simp [twoStepVec]    have hdiv : (k + 2 * (((0 : ℕ) : ℤ) - ((2 * r + 1 : ℕ) : ℤ))) / ((2 * r + 3 : ℕ) : ℤ)        = (b : ℤ) - ℓ - 1 := by      have hq0 : (0 : ℤ) < ((2 * r + 3 : ℕ) : ℤ) := by positivity      refine ((Int.ediv_emod_unique (r := 4 - (ε : ℤ)) hq0).2 ⟨?_, ?_, ?_⟩).1      · rw [hk]; push_cast; nlinarith      · omega      · push_cast; omega    have hbℓ : 4 * ℓ ≤ (2 * r + 2) * ℓ := Nat.mul_le_mul_right ℓ (by omega)    have hr' : slopeVec (2 * r + 1) b (2 * r + 3) k ⟨0, by omega⟩ = (b : ℤ) - ℓ - 1 := by      simp only [slopeVec, Rat.floor_intCast_div_natCast]      rw [hdiv]      simp only [CaballeroPeaks.clip]      omega    rw [hl, hr'] at h0    omega  · have hpos : (Zr (2 * r + 1) b (twoStepVec (2 * r + 1) b ((b : ℤ) - (m + r)) ((b : ℤ) - r))).filter          (fun x => 0 < x)        = (Zr (2 * r + 1) b (slopeVec (2 * r + 1) b (2 * r + 3) k)).filter (fun x => 0 < x) := by      have hbx : (b : ℤ) - ((b : ℤ) - ((m : ℤ) + r)) - r = m := by ring      have hL := (CaballeroTwoStep.lem_twostep (2 * r + 1) b r ((b : ℤ) - (m + r)) (by omega) hr        (by omega) (by omega)).1      rw [twoStepVec_eq _ _ r _ _ rfl, Zr_slopeVec, rows_sum r b Tn k hk2 hTT hTlt,        core r ℓ ε Tn m hℓ hε hT hm]      have e : Zr (2 * r + 1) b (CaballeroTwoStep.twoStep (2 * r + 1) b r ((b : ℤ) - (m + r)))          = CaballeroTwoStep.Zr (2 * r + 1) b            (CaballeroTwoStep.twoStep (2 * r + 1) b r ((b : ℤ) - (m + r))) := rfl      rw [e, hL, hbx, Icc_map_eq, Icc_eq_Sm, Icc_eq_Sm]    have hZr := eq_of_filter_pos _ _ (Zr_nonneg _ _ _) (Zr_nonneg _ _ _)      (by rw [Zr_card, Zr_card]) hpos    exact (rows_iff _ _ _ _ hc hg).mp hZr /-- Part IV, Theorem 18 (second peak, `a` odd): let `a = 2r+1 ≥ 3`, `q = a+2`, `ℓ ≥ 1`, `ε ∈ {0,1}`,`Δ = ℓ(a+1) - 1 + ε ≤ b+1`, `x = b - Δ`, `y = b - r`, `c = (x, y, b, …, b)`,`T = (ℓ+1) q - 5 + ε`, `k = q b + 2a - T - 1` and `g_h = clip ⌊(k+2h)/q⌋`. Then `c` and `g` aredistinct realisable vectors with `Z(c) = Z(g)`. -/theorem thm_K6odd (a b r ℓ ε : ℕ) (ha : a = 2 * r + 1) (ha3 : 3 ≤ a) (hℓ : 1 ≤ ℓ) (hε : ε ≤ 1)    (Δ : ℤ) (hΔ : Δ = (ℓ : ℤ) * (a + 1) - 1 + ε) (hΔb : Δ ≤ (b : ℤ) + 1)    (x y : ℤ) (hx : x = (b : ℤ) - Δ) (hy : y = (b : ℤ) - r)    (T k : ℤ) (hT : T = ((ℓ : ℤ) + 1) * ((a : ℤ) + 2) - 5 + ε)    (hk : k = ((a : ℤ) + 2) * b + 2 * a - T - 1) :    Realisable a b (twoStepVec a b x y) ∧ Realisable a b (slopeVec a b (a + 2) k) ∧      twoStepVec a b x y ≠ slopeVec a b (a + 2) k ∧      Z a b (twoStepVec a b x y) = Z a b (slopeVec a b (a + 2) k) := by  subst ha  have hr : 1 ≤ r := by omega  have e1 : (2 * r + 3) * 1 ≤ (2 * r + 3) * ℓ := Nat.mul_le_mul_left _ hℓ  have e2 : (2 * r + 2) * 1 ≤ (2 * r + 2) * ℓ := Nat.mul_le_mul_left _ hℓ  obtain ⟨Tn, hTn⟩ : ∃ Tn, Tn + 5 = (2 * r + 3) * ℓ + (2 * r + 3) + ε :=    ⟨(2 * r + 3) * ℓ + (2 * r + 3) + ε - 5, by omega⟩  obtain ⟨m, hm⟩ : ∃ m, m + r + 1 = (2 * r + 2) * ℓ + ε :=    ⟨(2 * r + 2) * ℓ + ε - r - 1, by omega⟩  have hTz : (Tn : ℤ) + 5 = (2 * r + 3) * ℓ + (2 * r + 3) + ε := by exact_mod_cast hTn  have hmz : (m : ℤ) + r + 1 = (2 * r + 2) * ℓ + ε := by exact_mod_cast hm  have hΔm : Δ = (m : ℤ) + r := by rw [hΔ]; push_cast; linarith  have hx' : x = (b : ℤ) - ((m : ℤ) + r) := by rw [hx, hΔm]  have hT' : T = (Tn : ℤ) := by rw [hT]; push_cast; linarith  have hk' : k = ((2 * r + 3 : ℕ) : ℤ) * b + 2 * ((2 * r + 1 : ℕ) : ℤ) - Tn - 1 := by    rw [hk, hT']; push_cast; ring  have hb : m + r ≤ b + 1 := by    rw [hΔm] at hΔb    exact_mod_cast hΔb  have := thm_K6odd_aux r b ℓ ε Tn m hr hℓ hε hTn hm hb k hk'  rw [hx', hy]  exact this end CaballeroK6Odd