🏛️ Atlas✒️Formalizations.NumberTheory.SymplecticCharacterOrders.Segments view on GitHub ↗26 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-/module public import Mathlib.Data.ZMod.Basicpublic import Mathlib.Data.Nat.ModEqpublic import Mathlib.Order.Interval.Finset.Natpublic import Mathlib.Data.Fintype.Cardpublic import Mathlib.Tactic.IntervalCasespublic import Mathlib.Tactic.Ringpublic import Mathlib.Tactic.Linarith /-!# Segments: which multisets `j ≡ ±c (mod n)` are stable under a unit For `N(c) = #{1 ≤ j ≤ m | j ≡ ±c (mod n)}` (with `h = 2m`) and `n ≥ 3`, `N` is stable undermultiplication by a unit `a ≢ ±1` iff `n ∣ h`, `n ∣ h + 1` or `n ∣ h + 2`; in that case it isstable under every unit. The core is a lemma of Amiot: an interval of `k` elements of `ℤ/n`,`1 < k < n − 1`, is not stable under multiplication by a unit `a ≢ ±1`. The proof reduces `m` to`m mod n` (complete periods), reflects `r ↦ n − 1 − r`, and counts `#{x ∈ I | x + d ∈ I}`. The odd case is the same content as `NotaPuente.Families.prop_4_2_odd` (through`SU(2m+1) = Sp(2m)`), counted differently; no bridge between the two counts is formalized. ## Main results * `Segmentos.amiot_interval`: Amiot's lemma, for every `n` and every interval.* `Segmentos.seg_stable_of_dvd`: if `n ∣ h`, `h + 1` or `h + 2`, `N` is stable under every unit.* `Segmentos.lem_seg`: the equivalence for `n ≥ 3` and `a ≢ ±1`.* `Segmentos.familias`, `Segmentos.familias_m6`: the column `m mod n` of the table of families and  its examples for `m = 6`. ## References * C. Marín, *Conductors of orders generated by character values of Sp(2m)*, preprint (2026);  programs and outputs: Zenodo, doi:10.5281/zenodo.23090540.* C. Marín, *Where the character values of Sp(2m) at a torsion element fail to generate the ring  of integers*, Zenodo (2026), doi:10.5281/zenodo.22813216 (a longer preprint of the same work).-/ @[expose] public section open Finset namespace Segmentos /-! ## Objects -/ /-- `N^{(n)}(c) = #{1 ≤ j ≤ m : j ≡ ±c (mod n)}`. It satisfies `N(-c) = N(c)`, so it is themultiset of the paper on the classes `(ℤ/n)/±1`. -/def N (n m : ℕ) (c : ZMod n) : ℕ :=  ((Icc 1 m).filter (fun j : ℕ => (j : ZMod n) = c ∨ (j : ZMod n) = -c)).card /-- `N^{(n)}` is stable under `c ↦ a c` (as a multiset: `N(a c) = N(c)` for every class). -/def Stable (n m : ℕ) (a : ZMod n) : Prop := ∀ c : ZMod n, N n m (a * c) = N n m c /-- `n ∣ h`, `n ∣ h + 1` or `n ∣ h + 2`, with `h = 2m`. -/def D (n m : ℕ) : Prop := n ∣ 2 * m ∨ n ∣ 2 * m + 1 ∨ n ∣ 2 * m + 2 instance (n m : ℕ) : Decidable (D n m) := by unfold D; infer_instance lemma N_neg (n m : ℕ) (c : ZMod n) : N n m (-c) = N n m c := by  unfold N; congr 1; apply filter_congr; intro j _; rw [neg_neg, or_comm] /-! ## Arithmetic helpers -/ lemma mod_cases {x n : ℕ} (h : x < 2 * n) : (x < n ∧ x % n = x) ∨ (n ≤ x ∧ x % n = x - n) := by  rcases lt_or_ge x n with h1 | h1  · exact Or.inl ⟨h1, Nat.mod_eq_of_lt h1⟩  · refine Or.inr ⟨h1, ?_⟩    rw [Nat.mod_eq_sub_mod h1, Nat.mod_eq_of_lt (by omega)] lemma dvd_small {n x : ℕ} (hn : 0 < n) (hx : x < 3 * n) : n ∣ x ↔ x = 0 ∨ x = n ∨ x = 2 * n := by  constructor  · rintro ⟨q, rfl⟩    have hq : q < 3 := by      by_contra h      have := Nat.mul_le_mul_left n (not_lt.mp h)      omega    interval_cases q <;> omega  · rintro (rfl | rfl | rfl)    · exact dvd_zero n    · exact dvd_rfl    · exact Dvd.intro_left 2 rfl /-- `D n m` only depends on `r = m mod n`, and reads `r ∈ {0, n-1}`, `2r ∈ {n, n-1, n-2}`. -/lemma D_iff {n m : ℕ} (hn : 0 < n) :    D n m ↔ (m % n = 0 ∨ 2 * (m % n) = n ∨ 2 * (m % n) + 1 = n ∨ 2 * (m % n) + 2 = n ∨      m % n + 1 = n) := by  have hmod : m ≡ m % n [MOD n] := (Nat.mod_modEq m n).symm  have hr : m % n < n := Nat.mod_lt m hn  unfold D  rw [(hmod.mul_left 2).dvd_iff dvd_rfl, ((hmod.mul_left 2).add_right 1).dvd_iff dvd_rfl,    ((hmod.mul_left 2).add_right 2).dvd_iff dvd_rfl, dvd_small hn (by omega),    dvd_small hn (by omega), dvd_small hn (by omega)]  omega section Counting variable {n : ℕ} [NeZero n] omit [NeZero n] inlemma natCast_eq_zero_iff_mod (j : ℕ) : (j : ZMod n) = 0 ↔ j % n = 0 := by  rw [← ZMod.val_eq_zero, ZMod.val_natCast] /-- `n` consecutive integers `m+1, …, m+n` represent each class of `ℤ/n` once. -/lemma card_Ioc_period (P : ZMod n → Prop) [DecidablePred P] (m : ℕ) :    ((Ioc m (m + n)).filter (fun j : ℕ => P (j : ZMod n))).card = (univ.filter P).card := by  have hcast : ∀ x : ZMod n,      ((m + 1 + (x - ((m + 1 : ℕ) : ZMod n)).val : ℕ) : ZMod n) = x := by    intro x    rw [Nat.cast_add (m + 1), ZMod.natCast_zmod_val]    ring  apply card_nbij' (fun j : ℕ => (j : ZMod n))    (fun x => m + 1 + (x - ((m + 1 : ℕ) : ZMod n)).val)  · intro j hj    simp only [coe_filter, Set.mem_ofPred_eq, mem_Ioc] at hj    simpa using hj.2  · intro x hx    simp only [coe_filter, Set.mem_ofPred_eq, mem_univ, true_and, mem_Ioc] at hx ⊢    refine ⟨⟨by omega, ?_⟩, by rw [hcast]; exact hx⟩    have := ZMod.val_lt (x - ((m + 1 : ℕ) : ZMod n))    omega  · intro j hj    simp only [coe_filter, Set.mem_ofPred_eq, mem_Ioc] at hj    have h1 : (j : ZMod n) - ((m + 1 : ℕ) : ZMod n) = ((j - (m + 1) : ℕ) : ZMod n) := by      rw [Nat.cast_sub (by omega)]    simp only    rw [h1, ZMod.val_natCast, Nat.mod_eq_of_lt (by omega)]    omega  · intro x _    exact hcast x lemma card_mul_unit (u : (ZMod n)ˣ) (P : ZMod n → Prop) [DecidablePred P] :    (univ.filter (fun x => P ((u : ZMod n) * x))).card = (univ.filter P).card := by  apply card_nbij' (fun x => (u : ZMod n) * x) (fun y => ((u⁻¹ : (ZMod n)ˣ) : ZMod n) * y)  · intro x hx; simpa using hx  · intro y hy; simpa using hy  · intro x _; simp  · intro y _; simp /-- `K(c) = #{x : x = ±c}`: the contribution of one complete period. -/def K (n : ℕ) [NeZero n] (c : ZMod n) : ℕ := (univ.filter (fun x : ZMod n => x = c ∨ x = -c)).card /-- `K0(c) = #{x ≠ 0 : x = ±c}`. -/def K0 (n : ℕ) [NeZero n] (c : ZMod n) : ℕ :=  (univ.filter (fun x : ZMod n => x ≠ 0 ∧ (x = c ∨ x = -c))).card lemma K_unit (u : (ZMod n)ˣ) (c : ZMod n) : K n ((u : ZMod n) * c) = K n c := by  unfold K  rw [← card_mul_unit u]  congr 1  apply filter_congr  intro x _  rw [← mul_neg, Units.mul_right_inj, Units.mul_right_inj] lemma K0_unit (u : (ZMod n)ˣ) (c : ZMod n) : K0 n ((u : ZMod n) * c) = K0 n c := by  unfold K0  rw [← card_mul_unit u]  congr 1  apply filter_congr  intro x _  rw [← mul_neg, Units.mul_right_inj, Units.mul_right_inj, Ne, Units.mul_right_eq_zero] /-- One more complete period adds `K(c)`. -/lemma N_add_period (m : ℕ) (c : ZMod n) : N n (m + n) c = N n m c + K n c := by  unfold N K  rw [← card_Ioc_period (fun x => x = c ∨ x = -c) m, ← card_union_of_disjoint]  · have hU : Icc 1 m ∪ Ioc m (m + n) = Icc 1 (m + n) := by      ext j      simp only [mem_Icc, mem_union, mem_Ioc]      omega    rw [← filter_union, hU]  · apply disjoint_filter_filter    rw [disjoint_left]    intro j h1 h2    simp only [mem_Icc, mem_Ioc] at h1 h2    omega lemma N_add_mul (r q : ℕ) (c : ZMod n) : N n (r + n * q) c = N n r c + q * K n c := by  induction q with  | zero => rw [Nat.mul_zero, Nat.add_zero, Nat.zero_mul, Nat.add_zero]  | succ q ih =>    rw [show r + n * (q + 1) = (r + n * q) + n by ring, N_add_period, ih]    ring /-- Complete periods contribute a unit-invariant multiset: `N_m = N_{m mod n} + ⌊m/n⌋ K`. -/lemma N_eq_mod (m : ℕ) (c : ZMod n) : N n m c = N n (m % n) c + (m / n) * K n c := by  conv_lhs => rw [← Nat.mod_add_div m n]  exact N_add_mul _ _ c lemma stable_mod (m : ℕ) (u : (ZMod n)ˣ) : Stable n m u ↔ Stable n (m % n) u := by  unfold Stable  refine forall_congr' fun c => ?_  rw [N_eq_mod m, N_eq_mod m c, K_unit, add_left_inj] /-- Reflection: `N_r + N_{n-1-r} = K0` for `r < n`. -/lemma N_reflect {r : ℕ} (hr : r < n) (c : ZMod n) : N n r c + N n (n - 1 - r) c = K0 n c := by  have hneg : ∀ j : ℕ, j ≤ n → ((n - j : ℕ) : ZMod n) = -(j : ZMod n) := by    intro j hj    rw [Nat.cast_sub hj, ZMod.natCast_self, zero_sub]  have h1 : N n (n - 1 - r) c =      ((Icc (r + 1) (n - 1)).filter (fun j : ℕ => (j : ZMod n) = c ∨ (j : ZMod n) = -c)).card := by    unfold N    apply card_nbij' (fun j => n - j) (fun j => n - j)    · intro j hj      simp only [coe_filter, Set.mem_ofPred_eq, mem_Icc] at hj ⊢      refine ⟨by omega, ?_⟩      rw [hneg j (by omega), neg_eq_iff_eq_neg, neg_inj, or_comm]      exact hj.2    · intro j hj      simp only [coe_filter, Set.mem_ofPred_eq, mem_Icc] at hj ⊢      refine ⟨by omega, ?_⟩      rw [hneg j (by omega), neg_eq_iff_eq_neg, neg_inj, or_comm]      exact hj.2    · intro j hj      simp only [coe_filter, Set.mem_ofPred_eq, mem_Icc] at hj      simp only      omega    · intro j hj      simp only [coe_filter, Set.mem_ofPred_eq, mem_Icc] at hj      simp only      omega  have h2 := card_Ioc_period (fun x : ZMod n => x ≠ 0 ∧ (x = c ∨ x = -c)) 0  rw [zero_add] at h2  unfold K0  rw [h1, ← h2]  unfold N  rw [← card_union_of_disjoint]  · rw [← filter_union]    congr 1    ext j    simp only [mem_filter, mem_union, mem_Icc, mem_Ioc, Ne, natCast_eq_zero_iff_mod]    constructor    · rintro ⟨hj, hP⟩      refine ⟨⟨by omega, by omega⟩, ?_, hP⟩      rcases mod_cases (x := j) (n := n) (by omega) with h | h <;> omega    · rintro ⟨⟨hj1, hj2⟩, hj3, hP⟩      refine ⟨?_, hP⟩      rcases mod_cases (x := j) (n := n) (by omega) with h | h <;> omega  · apply disjoint_filter_filter    rw [disjoint_left]    intro j h1 h2    simp only [mem_Icc] at h1 h2    omega lemma stable_reflect {r : ℕ} (hr : r < n) (u : (ZMod n)ˣ) :    Stable n r u ↔ Stable n (n - 1 - r) u := by  unfold Stable  refine forall_congr' fun c => ?_  have h1 := N_reflect hr ((u : ZMod n) * c)  have h2 := N_reflect hr c  rw [K0_unit] at h1  omega /-- The predicate "`c ∈ [-s, s]` and `c ≠ 0`". -/def Pm (n s : ℕ) (c : ZMod n) : Prop := (c + (s : ZMod n)).val < 2 * s + 1 ∧ c ≠ 0 instance (n s : ℕ) (c : ZMod n) : Decidable (Pm n s c) := by unfold Pm; infer_instance /-- For `2s < n` the segment `{±1, …, ±s}` has no repetitions: `N_s` is the indicator of`[-s, s] \ {0}`. -/lemma N_small {s : ℕ} (hs : 2 * s < n) (c : ZMod n) : N n s c = if Pm n s c then 1 else 0 := by  obtain ⟨w, hw, rfl⟩ : ∃ w, w < n ∧ c = (w : ZMod n) :=    ⟨c.val, ZMod.val_lt c, (ZMod.natCast_zmod_val c).symm⟩  have e1 : ((w : ZMod n) + (s : ZMod n)).val = (w + s) % n := by    rw [← Nat.cast_add, ZMod.val_natCast]  have e2 : (w : ZMod n) ≠ 0 ↔ w ≠ 0 := by    rw [Ne, natCast_eq_zero_iff_mod, Nat.mod_eq_of_lt hw]  have e3 : ∀ j : ℕ, ((j : ZMod n) = w ∨ (j : ZMod n) = -(w : ZMod n)) ↔      (j % n = w ∨ (j + w) % n = 0) := by    intro j    rw [ZMod.natCast_eq_natCast_iff', Nat.mod_eq_of_lt hw, eq_neg_iff_add_eq_zero, ← Nat.cast_add,      natCast_eq_zero_iff_mod]  have hP : Pm n s (w : ZMod n) ↔ ((w + s) % n < 2 * s + 1 ∧ w ≠ 0) := by    unfold Pm; rw [e1, e2]  unfold N  simp_rw [e3]  have hws := mod_cases (x := w + s) (n := n) (by omega)  split_ifs with h  · rw [hP] at h    rw [card_eq_one]    by_cases hle : w ≤ s    · refine ⟨w, ?_⟩      ext j      simp only [mem_filter, mem_Icc, mem_singleton]      constructor      · rintro ⟨⟨hj1, hj2⟩, hj3⟩        have ha := mod_cases (x := j) (n := n) (by omega)        have hb := mod_cases (x := j + w) (n := n) (by omega)        omega      · intro hj        have ha := mod_cases (x := j) (n := n) (by omega)        have hb := mod_cases (x := j + w) (n := n) (by omega)        omega    · refine ⟨n - w, ?_⟩      ext j      simp only [mem_filter, mem_Icc, mem_singleton]      constructor      · rintro ⟨⟨hj1, hj2⟩, hj3⟩        have ha := mod_cases (x := j) (n := n) (by omega)        have hb := mod_cases (x := j + w) (n := n) (by omega)        omega      · intro hj        have ha := mod_cases (x := j) (n := n) (by omega)        have hb := mod_cases (x := j + w) (n := n) (by omega)        omega  · rw [hP] at h    rw [card_eq_zero, filter_eq_empty_iff]    intro j hj hj3    simp only [mem_Icc] at hj    have ha := mod_cases (x := j) (n := n) (by omega)    have hb := mod_cases (x := j + w) (n := n) (by omega)    omega /-- In the three families, `Pm` is a unit-invariant condition. -/lemma Pm_iff {s : ℕ} (hs : 2 * s < n)    (hcase : s = 0 ∨ n = 2 * s + 1 ∨ n = 2 * s + 2) (c : ZMod n) :    Pm n s c ↔ (s ≠ 0 ∧ c ≠ 0 ∧ c + c ≠ 0) := by  obtain ⟨w, hw, rfl⟩ : ∃ w, w < n ∧ c = (w : ZMod n) :=    ⟨c.val, ZMod.val_lt c, (ZMod.natCast_zmod_val c).symm⟩  unfold Pm  simp only [← Nat.cast_add, ZMod.val_natCast, Ne, natCast_eq_zero_iff_mod, Nat.mod_eq_of_lt hw]  have ha := mod_cases (x := w + s) (n := n) (by omega)  have hb := mod_cases (x := w + w) (n := n) (by omega)  omega lemma stable_small_of {s : ℕ} (hs : 2 * s < n)    (hcase : s = 0 ∨ n = 2 * s + 1 ∨ n = 2 * s + 2) (u : (ZMod n)ˣ) : Stable n s u := by  intro c  rw [N_small hs, N_small hs]  refine if_congr ?_ rfl rfl  rw [Pm_iff hs hcase, Pm_iff hs hcase, Ne, Ne, Ne, Ne, Units.mul_right_eq_zero, ← mul_add,    Units.mul_right_eq_zero] /-! ## Amiot's lemma -/ omit [NeZero n] in/-- Exactly `max(0, k-t) + max(0, k-n+t)` elements `i` of `{0,…,k-1}` have `i + t mod n` in it. -/lemma nat_count {k t : ℕ} (ht1 : 1 ≤ t) (htn : t < n) (hk : k ≤ n) :    ((range n).filter (fun i => i < k ∧ (i + t) % n < k)).card = (k - t) + (k - (n - t)) := by  have hdisj : Disjoint (range (k - t)) (Ico (n - t) k) := by    rw [disjoint_left]    intro i h1 h2    simp only [mem_range, mem_Ico] at h1 h2    omega  have : (range n).filter (fun i => i < k ∧ (i + t) % n < k) = range (k - t) ∪ Ico (n - t) k := by    ext i    simp only [mem_filter, mem_range, mem_union, mem_Ico]    constructor    · intro h      have := mod_cases (x := i + t) (n := n) (by omega)      omega    · intro h      have := mod_cases (x := i + t) (n := n) (by omega)      omega  rw [this, card_union_of_disjoint hdisj, card_range, Nat.card_Ico] /-- `#{x ∈ I : x + d ∈ I}` for the interval `I = {c₀, …, c₀ + k - 1}` of `ℤ/n`. -/lemma interval_count {k : ℕ} (hk : k ≤ n) (c₀ d : ZMod n) (hd : d.val ≠ 0) :    (univ.filter (fun x : ZMod n => (x - c₀).val < k ∧ (x + d - c₀).val < k)).card =      (k - d.val) + (k - (n - d.val)) := by  rw [← nat_count (Nat.one_le_iff_ne_zero.mpr hd) (ZMod.val_lt d) hk]  apply card_nbij' (fun x => (x - c₀).val) (fun i => (i : ZMod n) + c₀)  · intro x hx    simp only [coe_filter, Set.mem_ofPred_eq, mem_univ, true_and, mem_range] at hx ⊢    refine ⟨ZMod.val_lt _, hx.1, ?_⟩    rw [← ZMod.val_add, sub_add_eq_add_sub]    exact hx.2  · intro i hi    simp only [coe_filter, Set.mem_ofPred_eq, mem_univ, true_and, mem_range] at hi ⊢    rw [add_sub_cancel_right, ZMod.val_natCast, Nat.mod_eq_of_lt hi.1,      show (i : ZMod n) + c₀ + d - c₀ = (i : ZMod n) + d by ring, ZMod.val_add, ZMod.val_natCast,      Nat.mod_eq_of_lt hi.1]    exact hi.2  · intro x _    simp only [ZMod.natCast_zmod_val, sub_add_cancel]  · intro i hi    simp only [coe_filter, Set.mem_ofPred_eq, mem_range] at hi    simp only [add_sub_cancel_right, ZMod.val_natCast]    exact Nat.mod_eq_of_lt hi.1 end Counting /-- **Amiot's lemma** (core of `lem:seg`; [Ami09, Thm. 1]). Let `1 < k < n - 1` and let `a` be aunit of `ℤ/n` with `a ≢ ±1`. Then no interval `I = {c₀, c₀+1, …, c₀+k-1}` of `k` elements of`ℤ/n` satisfies `a I ⊆ I` (in particular `a I ≠ I`). -/theorem amiot_interval {n k : ℕ} (hk1 : 1 < k) (hk2 : k < n - 1) (c₀ : ZMod n)    (a : (ZMod n)ˣ) (ha1 : (a : ZMod n) ≠ 1) (ha2 : (a : ZMod n) ≠ -1) :    ¬ ∀ x : ZMod n, (x - c₀).val < k → ((a : ZMod n) * x - c₀).val < k := by  intro H  have : NeZero n := ⟨by omega⟩  have : Fact (1 < n) := ⟨by omega⟩  have hval : ∀ x : ZMod n, ((x.val : ℕ) : ZMod n) = x := ZMod.natCast_zmod_val  have ht0 : (a : ZMod n).val ≠ 0 := by    rw [Ne, ZMod.val_eq_zero]; exact a.ne_zero  have ht1 : (a : ZMod n).val ≠ 1 := by    intro h; apply ha1; rw [← hval (a : ZMod n), h, Nat.cast_one]  have htn : (a : ZMod n).val ≠ n - 1 := by    intro h; apply ha2    rw [← hval (a : ZMod n), h, Nat.cast_sub (by omega), ZMod.natCast_self, Nat.cast_one, zero_sub]  have htlt := ZMod.val_lt (a : ZMod n)  have h1 := interval_count (n := n) (k := k) (by omega) c₀ 1 (by rw [ZMod.val_one]; omega)  have ha := interval_count (n := n) (k := k) (by omega) c₀ (a : ZMod n) ht0  rw [ZMod.val_one] at h1  -- `y ↦ a y` injects `{y ∈ I : y + 1 ∈ I}` into `{x ∈ I : x + a ∈ I}`.  have hle : (univ.filter (fun x : ZMod n => (x - c₀).val < k ∧ (x + 1 - c₀).val < k)).card ≤      (univ.filter (fun x : ZMod n => (x - c₀).val < k ∧ (x + (a : ZMod n) - c₀).val < k)).card := by    apply card_le_card_of_injOn (fun y => (a : ZMod n) * y)    · intro y hy      simp only [coe_filter, Set.mem_ofPred_eq, mem_univ, true_and] at hy ⊢      refine ⟨H y hy.1, ?_⟩      have := H (y + 1) hy.2      rwa [mul_add, mul_one] at this    · intro y _ z _ hyz      exact (Units.mul_right_inj a).mp hyz  rw [h1, ha] at hle  omega /-! ## Lemma `lem:seg` -/ lemma small_only {n s : ℕ} [NeZero n] (hn : 3 ≤ n) (hs : 2 * s < n) (a : (ZMod n)ˣ)    (ha1 : (a : ZMod n) ≠ 1) (ha2 : (a : ZMod n) ≠ -1) (H : Stable n s a) :    s = 0 ∨ n ≤ 2 * s + 2 := by  by_contra hc  apply amiot_interval (n := n) (k := 2 * s + 1) (by omega) (by omega) (-(s : ZMod n)) a ha1 ha2  intro x hx  rw [sub_neg_eq_add] at hx ⊢  by_cases hx0 : x = 0  · rw [hx0, mul_zero]; rw [hx0] at hx; exact hx  · have hP : Pm n s x := ⟨hx, hx0⟩    have h := H x    rw [N_small hs, N_small hs, ite_eq_left hP] at h    by_contra hne    rw [ite_eq_right (fun h' => hne h'.1)] at h    exact absurd h (by norm_num) /-- `lem:seg`, second sentence: if `n ∣ h`, `n ∣ h + 1` or `n ∣ h + 2` (`h = 2m`), then`N^{(n)}` is stable under every unit. -/theorem seg_stable_of_dvd {n m : ℕ} (hn : 3 ≤ n) (hD : D n m) (u : (ZMod n)ˣ) :    Stable n m u := by  have : NeZero n := ⟨by omega⟩  rw [D_iff (by omega)] at hD  rw [stable_mod]  have hr : m % n < n := Nat.mod_lt m (by omega)  generalize m % n = r at hD hr ⊢  rcases lt_or_ge (2 * r) n with h | h  · exact stable_small_of h (by omega) u  · rw [stable_reflect hr]    exact stable_small_of (by omega) (by omega) u /-- **Lemma `lem:seg` (segments).** Let `n ≥ 3` and let `a` be a unit mod `n` with `a ≢ ±1`.Then `N^{(n)}` is stable under `c ↦ a c` iff `n` divides `h`, `h + 1` or `h + 2` (`h = 2m`). -/theorem lem_seg {n m : ℕ} (hn : 3 ≤ n) (a : (ZMod n)ˣ) (ha1 : (a : ZMod n) ≠ 1)    (ha2 : (a : ZMod n) ≠ -1) : Stable n m a ↔ D n m := by  have : NeZero n := ⟨by omega⟩  refine ⟨fun H => ?_, fun hD => seg_stable_of_dvd hn hD a⟩  rw [D_iff (by omega)]  rw [stable_mod] at H  have hr : m % n < n := Nat.mod_lt m (by omega)  generalize m % n = r at H hr ⊢  rcases lt_or_ge (2 * r) n with h | h  · have := small_only hn h a ha1 ha2 H    omega  · rw [stable_reflect hr] at H    have := small_only (s := n - 1 - r) hn (by omega) a ha1 ha2 H    omega /-- The `m mod n` column of Table `tab:familias`. -/theorem familias {n m : ℕ} (hn : 3 ≤ n) :    (n ∣ 2 * m ↔ m % n = 0 ∨ 2 * (m % n) = n) ∧    (n ∣ 2 * m + 1 ↔ 2 * (m % n) + 1 = n) ∧    (n ∣ 2 * m + 2 ↔ m % n = n - 1 ∨ 2 * (m % n) + 2 = n) := by  have hmod : m ≡ m % n [MOD n] := (Nat.mod_modEq m n).symm  have hr : m % n < n := Nat.mod_lt m (by omega)  rw [(hmod.mul_left 2).dvd_iff dvd_rfl, ((hmod.mul_left 2).add_right 1).dvd_iff dvd_rfl,    ((hmod.mul_left 2).add_right 2).dvd_iff dvd_rfl, dvd_small (by omega) (by omega),    dvd_small (by omega) (by omega), dvd_small (by omega) (by omega)]  omega /-- Examples column of Table `tab:familias` (`m = 6`, `3 ≤ n ≤ 30`). -/theorem familias_m6 : (Icc 3 30).filter (fun n => D n 6) = {3, 4, 6, 7, 12, 13, 14} := by  decide end Segmentos