🏛️ Atlas✒️Formalizations.NumberTheory.UnitsArithmeticProgression view on GitHub ↗23 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 Mathlib.FieldTheory.Finite.Basicpublic import Mathlib.GroupTheory.OrderOfElementpublic import Mathlib.Algebra.Group.Subgroup.ZPowers.Basicpublic import Mathlib.Data.List.Perm.Basicpublic import Mathlib.Tactic.Ringpublic import Mathlib.Tactic.LinearCombinationpublic import Mathlib.Tactic.FieldSimppublic import Mathlib.Data.Nat.Prime.Basicpublic import Mathlib.Data.ZMod.QuotientGrouppublic import Mathlib.Algebra.Group.Subgroup.Finitepublic import Mathlib.SetTheory.Cardinal.Finite /-!# Units of ℤ/p as a direct product in arithmetic progression (Anderson–Cameron–Preece) For a prime `p = 6m + 1`, `m > 1`, the unit group of `ℤ/p` is an internal direct product of cyclicsubgroups generated by `x, x + k, x + 2k` with orders `2, 3, m` in some order if and only if`m ≡ 1, 5 (mod 6)` and some root `α` of `X² + 3X + 3` has order `m`, or `α/2` has order `m`, or `α`has order `p − 1` and the other root order `(p − 1)/2` (Theorem of the Day; Cameron and Preece). Theroot must be quantified existentially: with a fixed labelling the statement fails at `p = 31`.Proof: `α + 1` has order 3, and with `t = 2α + 3`, `α = −(α+1)² t`, `t² = −3`; thearithmetic-progression condition fixes the order-`m` generator in each of the six placements. ## Main results * `ACP.internal_of_coprime`: pairwise coprime orders with product p − 1 give an internal direct product* `ACP.classification`: Anderson–Cameron–Preece classification (∃ root) ## References * R. Whitty, Theorem of the Day, «A Theorem of Anderson, Cameron and Preece on Groups of Units» (NumberTheory/ZnUnits).* P. J. Cameron, D. A. Preece, arXiv:1111.3507, §2; I. Anderson, D. A. Preece, INTEGERS 12 (2012) A52.-/ @[expose] public section namespace ACP /-- `u`, `v`, `w` give an internal direct product decomposition `G = ⟨u⟩ × ⟨v⟩ × ⟨w⟩`. -/def Internal3 {G : Type*} [CommGroup G] (u v w : G) : Prop :=  Function.Bijective (fun t : Subgroup.zpowers u × (Subgroup.zpowers v × Subgroup.zpowers w) =>    ((t.1 : G) * (t.2.1 : G)) * (t.2.2 : G)) /-! ### Part 1: pairwise coprime orders with product `|G|` -/ theorem ord_mul {G : Type*} [CommMonoid G] {x y : G} (h : (orderOf x).Coprime (orderOf y)) :    orderOf (x * y) = orderOf x * orderOf y :=  (Commute.all x y).orderOf_mul_eq_mul_orderOf_of_coprime h /-- Pairwise coprime orders whose product is `|G|` give an internal direct product. -/theorem internal_of_coprime {G : Type*} [CommGroup G] [Finite G] (u v w : G)    (huv : (orderOf u).Coprime (orderOf v)) (huw : (orderOf u).Coprime (orderOf w))    (hvw : (orderOf v).Coprime (orderOf w))    (h : orderOf u * orderOf v * orderOf w = Nat.card G) : Internal3 u v w := by  have h1 : orderOf (u * v) = orderOf u * orderOf v := ord_mul huv  have h2 : orderOf (u * v * w) = Nat.card G := by    rw [ord_mul (x := u * v) (y := w) (by rw [h1]; exact Nat.Coprime.mul_left huw hvw), h1, h]  have htop : Subgroup.zpowers (u * v * w) = ⊤ := by    apply Subgroup.eq_top_of_card_eq    rw [Nat.card_zpowers, h2]  have hsurj : Function.Surjective      (fun t : Subgroup.zpowers u × (Subgroup.zpowers v × Subgroup.zpowers w) =>        ((t.1 : G) * (t.2.1 : G)) * (t.2.2 : G)) := by    intro g    have hg : g ∈ Subgroup.zpowers (u * v * w) := by rw [htop]; trivial    obtain ⟨k, hk⟩ := Subgroup.mem_zpowers_iff.mp hg    refine ⟨(⟨u ^ k, Subgroup.zpow_mem _ (Subgroup.mem_zpowers u) k⟩,      (⟨v ^ k, Subgroup.zpow_mem _ (Subgroup.mem_zpowers v) k⟩,        ⟨w ^ k, Subgroup.zpow_mem _ (Subgroup.mem_zpowers w) k⟩)), ?_⟩    show u ^ k * v ^ k * w ^ k = g    rw [← hk, mul_zpow, mul_zpow]  refine hsurj.bijective_of_nat_card_le ?_  rw [Nat.card_prod, Nat.card_prod, Nat.card_zpowers, Nat.card_zpowers, Nat.card_zpowers, ← h, mul_assoc] /-- Injectivity: `⟨u⟩ ∩ ⟨v⟩` is trivial. -/theorem Internal3.disj_uv {G : Type*} [CommGroup G] {u v w : G} (h : Internal3 u v w) :    Disjoint (Subgroup.zpowers u) (Subgroup.zpowers v) := by  rw [Subgroup.disjoint_def]  intro x hx hx'  have key := h.1 (a₁ := (⟨x, hx⟩, (1, 1))) (a₂ := (1, (⟨x, hx'⟩, 1))) (by simp)  simpa using congrArg (fun t => (t.1 : G)) key /-- Injectivity: `⟨u⟩ ∩ ⟨w⟩` is trivial. -/theorem Internal3.disj_uw {G : Type*} [CommGroup G] {u v w : G} (h : Internal3 u v w) :    Disjoint (Subgroup.zpowers u) (Subgroup.zpowers w) := by  rw [Subgroup.disjoint_def]  intro x hx hx'  have key := h.1 (a₁ := (⟨x, hx⟩, (1, 1))) (a₂ := (1, (1, ⟨x, hx'⟩))) (by simp)  simpa using congrArg (fun t => (t.1 : G)) key /-- Injectivity: `⟨v⟩ ∩ ⟨w⟩` is trivial. -/theorem Internal3.disj_vw {G : Type*} [CommGroup G] {u v w : G} (h : Internal3 u v w) :    Disjoint (Subgroup.zpowers v) (Subgroup.zpowers w) := by  rw [Subgroup.disjoint_def]  intro x hx hx'  have key := h.1 (a₁ := (1, (⟨x, hx⟩, 1))) (a₂ := (1, (1, ⟨x, hx'⟩))) (by simp)  simpa using congrArg (fun t => (t.2.1 : G)) key /-! ### Part 2: small orders in a field -/ theorem coprime6_iff {m : ℕ} : m.Coprime 6 ↔ m % 6 = 1 ∨ m % 6 = 5 := by  rw [Nat.coprime_comm, show (6 : ℕ) = 2 * 3 from rfl, Nat.coprime_mul_iff_left,    Nat.Prime.coprime_iff_not_dvd Nat.prime_two, Nat.Prime.coprime_iff_not_dvd Nat.prime_three]  omega theorem perm_cases {a b c m : ℕ} (h : [a, b, c].Perm [2, 3, m]) :    (a = 2 ∧ b = 3 ∧ c = m) ∨ (a = 2 ∧ b = m ∧ c = 3) ∨ (a = 3 ∧ b = 2 ∧ c = m) ∨    (a = 3 ∧ b = m ∧ c = 2) ∨ (a = m ∧ b = 2 ∧ c = 3) ∨ (a = m ∧ b = 3 ∧ c = 2) := by  have hmem : ∀ x, x ∈ [a, b, c] → x = 2 ∨ x = 3 ∨ x = m := by    intro x hx    have := h.subset hx    simpa using this  have ha := hmem a (by simp)  have hb := hmem b (by simp)  have hc := hmem c (by simp)  have hcount := List.perm_iff_count.mp h  have e2 := hcount 2  have e3 := hcount 3  have em := hcount m  by_cases h2 : m = 2  · subst h2    rcases ha with rfl | rfl | rfl <;> rcases hb with rfl | rfl | rfl <;>      rcases hc with rfl | rfl | rfl <;> simp at e2 e3 ⊢  by_cases h3 : m = 3  · subst h3    rcases ha with rfl | rfl | rfl <;> rcases hb with rfl | rfl | rfl <;>      rcases hc with rfl | rfl | rfl <;> simp at e2 e3 ⊢  rcases ha with rfl | rfl | rfl <;> rcases hb with rfl | rfl | rfl <;>    rcases hc with rfl | rfl | rfl <;> simp [h2, h3, Ne.symm h2, Ne.symm h3] at e2 e3 em ⊢ section Field variable {K : Type*} [Field K] theorem eq_neg_one_of_orderOf_two {y : K} (h : orderOf y = 2) : y = -1 := by  have h1 : y ^ 2 = 1 := by    have := pow_orderOf_eq_one y    rwa [h] at this  have hne : y ≠ 1 := by    rintro rfl    simp at h  have : (y - 1) * (y + 1) = 0 := by linear_combination h1  rcases mul_eq_zero.mp this with h | h  · exact absurd (sub_eq_zero.mp h) hne  · exact eq_neg_of_add_eq_zero_left h theorem orderOf_neg_one_two (h2 : (2 : K) ≠ 0) : orderOf (-1 : K) = 2 := by  apply orderOf_eq_prime (p := 2)  · simp  · intro h    apply h2    linear_combination -h theorem cube_of_orderOf_three {y : K} (h : orderOf y = 3) : y ^ 2 + y + 1 = 0 := by  have h1 : y ^ 3 = 1 := by    have := pow_orderOf_eq_one y    rwa [h] at this  have hne : y ≠ 1 := by    rintro rfl    simp at h  have : (y - 1) * (y ^ 2 + y + 1) = 0 := by linear_combination h1  rcases mul_eq_zero.mp this with h | h  · exact absurd (sub_eq_zero.mp h) hne  · exact h theorem orderOf_three_of_cube (h3 : (3 : K) ≠ 0) {y : K} (h : y ^ 2 + y + 1 = 0) :    orderOf y = 3 := by  have h1 : y ^ 3 = 1 := by linear_combination (y - 1) * h  refine orderOf_eq_prime h1 ?_  rintro rfl  apply h3  linear_combination h theorem units_eq_of_orderOf_two {x y : Kˣ} (hx : orderOf x = 2) (hy : orderOf y = 2) : x = y := by  apply Units.ext  rw [eq_neg_one_of_orderOf_two (y := (x : K)) (by rw [orderOf_units]; exact hx),    eq_neg_one_of_orderOf_two (y := (y : K)) (by rw [orderOf_units]; exact hy)] theorem mem_zpowers_of_orderOf_three {x y : Kˣ} (hx : orderOf x = 3) (hy : orderOf y = 3) :    y ∈ Subgroup.zpowers x := by  have hω := cube_of_orderOf_three (y := (x : K)) (by rw [orderOf_units]; exact hx)  have hη := cube_of_orderOf_three (y := (y : K)) (by rw [orderOf_units]; exact hy)  have hprod : ((y : K) - x) * ((y : K) - (x : K) ^ 2) = 0 := by    linear_combination hη + ((x : K) - 1 - (y : K)) * hω  rcases mul_eq_zero.mp hprod with h | h  · have : y = x := Units.ext (sub_eq_zero.mp h)    rw [this]    exact Subgroup.mem_zpowers x  · have : y = x ^ 2 := Units.ext (by rw [Units.val_pow_eq_pow_val]; exact sub_eq_zero.mp h)    rw [this]    exact Subgroup.pow_mem _ (Subgroup.mem_zpowers x) 2 /-- Disjoint cyclic factors of orders `2`, `3` and `m` force `gcd (m, 6) = 1`. -/theorem coprime6_of_disjoint {m : ℕ} (hm : 0 < m)    {a b c : Kˣ} (ha : orderOf a = 2) (hb : orderOf b = 3) (hc : orderOf c = m)    (dac : Disjoint (Subgroup.zpowers a) (Subgroup.zpowers c))    (dbc : Disjoint (Subgroup.zpowers b) (Subgroup.zpowers c)) : m.Coprime 6 := by  rw [coprime6_iff]  have h2m : ¬ 2 ∣ m := by    intro hd    have hpow : orderOf (c ^ (m / 2)) = 2 := by      rw [orderOf_pow_of_dvd (by omega) (by rw [hc]; exact Nat.div_dvd_of_dvd hd), hc]      exact Nat.div_div_self hd (by omega)    have heq : c ^ (m / 2) = a := units_eq_of_orderOf_two hpow ha    have hmem : a ∈ Subgroup.zpowers c := by      rw [← heq]      exact Subgroup.pow_mem _ (Subgroup.mem_zpowers c) _    have h1 : a = 1 := Subgroup.disjoint_def.mp dac (Subgroup.mem_zpowers a) hmem    rw [h1, orderOf_one] at ha    omega  have h3m : ¬ 3 ∣ m := by    intro hd    have hpow : orderOf (c ^ (m / 3)) = 3 := by      rw [orderOf_pow_of_dvd (by omega) (by rw [hc]; exact Nat.div_dvd_of_dvd hd), hc]      exact Nat.div_div_self hd (by omega)    have hmem : c ^ (m / 3) ∈ Subgroup.zpowers b := mem_zpowers_of_orderOf_three hb hpow    have hmem' : c ^ (m / 3) ∈ Subgroup.zpowers c := Subgroup.pow_mem _ (Subgroup.mem_zpowers c) _    have h1 : c ^ (m / 3) = 1 := Subgroup.disjoint_def.mp dbc hmem hmem'    rw [h1, orderOf_one] at hpow    omega  omega /-! ### Part 3: the lemma `ord t = m ↔ ord α = 6m ∧ ord β = 3m` -/ theorem lemma3_aux (h2 : (2 : K) ≠ 0) (h3 : (3 : K) ≠ 0) {m : ℕ} (hm : 0 < m)    (hcop : m.Coprime 6) {ω t : K} (hω : ω ^ 2 + ω + 1 = 0) :    orderOf t = m ↔ orderOf (-(ω ^ 2) * t) = 6 * m ∧ orderOf (ω * t) = 3 * m := by  have hω3 : ω ^ 3 = 1 := by linear_combination (ω - 1) * hω  have oω : orderOf ω = 3 := orderOf_three_of_cube h3 hω  have oω2 : orderOf (ω ^ 2) = 3 :=    orderOf_three_of_cube h3 (by linear_combination (ω ^ 2 - ω + 1) * hω)  have oneg : orderOf (-(ω ^ 2)) = 6 := by    have : orderOf ((-1) * ω ^ 2) = 2 * 3 := by      rw [ord_mul (x := (-1 : K)) (y := ω ^ 2) (by rw [orderOf_neg_one_two h2, oω2]; norm_num),        orderOf_neg_one_two h2, oω2]    simpa using this  have c3 : Nat.Coprime 3 m := (Nat.Coprime.coprime_dvd_right (by norm_num) hcop).symm  have c6 : Nat.Coprime 6 m := hcop.symm  have hm6 := coprime6_iff.mp hcop  constructor  · intro ht    constructor    · rw [ord_mul (x := -(ω ^ 2)) (y := t) (by rw [oneg, ht]; exact c6), oneg, ht]    · rw [ord_mul (x := ω) (y := t) (by rw [oω, ht]; exact c3), oω, ht]  · rintro ⟨hα, hβ⟩    have hmodd : Odd m := Nat.odd_iff.mpr (by omega)    have hm3 : ¬ 3 ∣ m := by omega    -- s = t ^ m satisfies s ^ 3 = 1    have hs3 : (t ^ m) ^ 3 = 1 := by      have h1 : (ω * t) ^ (3 * m) = 1 := by        rw [← hβ]        exact pow_orderOf_eq_one _      have e : (ω * t) ^ (3 * m) = (ω ^ 3) ^ m * (t ^ m) ^ 3 := by        rw [mul_pow]; ring      rw [e, hω3, one_pow, one_mul] at h1      exact h1    -- r = ω ^ m is a primitive cube root of unity    have hr3 : (ω ^ m) ^ 3 = 1 := by      rw [← pow_mul, mul_comm, pow_mul, hω3, one_pow]    have hr1 : ω ^ m ≠ 1 := by      intro h      have : orderOf ω ∣ m := orderOf_dvd_iff_pow_eq_one.mpr h      rw [oω] at this      exact hm3 this    have hr : (ω ^ m) ^ 2 + ω ^ m + 1 = 0 := by      have : (ω ^ m - 1) * ((ω ^ m) ^ 2 + ω ^ m + 1) = 0 := by linear_combination hr3      rcases mul_eq_zero.mp this with h | h      · exact absurd (sub_eq_zero.mp h) hr1      · exact h    have hαm : (-(ω ^ 2) * t) ^ m = -((ω ^ m) ^ 2) * t ^ m := by      rw [mul_pow, neg_pow, hmodd.neg_one_pow]      ring    have hβm : (ω * t) ^ m = ω ^ m * t ^ m := mul_pow _ _ _    have hcases : (t ^ m - 1) * (t ^ m - ω ^ m) * (t ^ m - (ω ^ m) ^ 2) = 0 := by      linear_combination hs3 + (t ^ m - 1) * (-(t ^ m) + ω ^ m - 1) * hr    rcases mul_eq_zero.mp hcases with h | h    · rcases mul_eq_zero.mp h with h | h      · -- s = 1        have hs1 : t ^ m = 1 := sub_eq_zero.mp h        have hdvd : orderOf t ∣ m := orderOf_dvd_iff_pow_eq_one.mpr hs1        have hdvd2 : 6 * m ∣ 6 * orderOf t := by          rw [← hα]          apply orderOf_dvd_of_pow_eq_one          have e : (-(ω ^ 2) * t) ^ (6 * orderOf t) = ((-(ω ^ 2)) ^ 6) ^ orderOf t * (t ^ orderOf t) ^ 6 := by            rw [mul_pow, pow_mul, pow_mul']          have e6 : (-(ω ^ 2)) ^ 6 = 1 := by            calc (-(ω ^ 2)) ^ 6 = (ω ^ 3) ^ 4 := by ring              _ = 1 := by rw [hω3]; simp          rw [e, e6, pow_orderOf_eq_one, one_pow, one_pow, one_mul]        exact Nat.dvd_antisymm hdvd (Nat.dvd_of_mul_dvd_mul_left (by norm_num) hdvd2)      · -- s = r : α ^ m = -1        exfalso        have hsr : t ^ m = ω ^ m := sub_eq_zero.mp h        have h1 : (-(ω ^ 2) * t) ^ m = -1 := by          rw [hαm, hsr]          linear_combination (-1 : K) * hr3        have h2' : (-(ω ^ 2) * t) ^ (m * 2) = 1 := by          rw [pow_mul, h1]; norm_num        have := orderOf_dvd_of_pow_eq_one h2'        rw [hα] at this        have := Nat.le_of_dvd (by omega) this        omega    · -- s = r ^ 2 : β ^ m = 1      exfalso      have hsr : t ^ m = (ω ^ m) ^ 2 := sub_eq_zero.mp h      have h1 : (ω * t) ^ m = 1 := by        rw [hβm, hsr]        linear_combination hr3      have := orderOf_dvd_of_pow_eq_one h1      rw [hβ] at this      have := Nat.le_of_dvd (by omega) this      omega theorem lemma3 (h2 : (2 : K) ≠ 0) (h3 : (3 : K) ≠ 0) {m : ℕ} (hm : 0 < m)    (hcop : m.Coprime 6) {α : K} (hα : α ^ 2 + 3 * α + 3 = 0) :    orderOf (2 * α + 3) = m ↔ orderOf α = 6 * m ∧ orderOf (-α - 3) = 3 * m := by  have hω : (α + 1) ^ 2 + (α + 1) + 1 = 0 := by linear_combination hα  have e1 : -((α + 1) ^ 2) * (2 * α + 3) = α := by linear_combination (-(2 * α + 1)) * hα  have e2 : (α + 1) * (2 * α + 3) = -α - 3 := by linear_combination 2 * hα  have := lemma3_aux h2 h3 hm hcop (t := 2 * α + 3) hω  rw [e1, e2] at this  exact this /-! ### Part 4: necessity -/ theorem nec_core (h2 : (2 : K) ≠ 0) (h3 : (3 : K) ≠ 0) {m : ℕ} (hm : 0 < m)    {a b c : Kˣ} (ha : orderOf a = 2) (hb : orderOf b = 3) (hc : orderOf c = m)    (dac : Disjoint (Subgroup.zpowers a) (Subgroup.zpowers c))    (dbc : Disjoint (Subgroup.zpowers b) (Subgroup.zpowers c))    (hap : ((a : K) + (c : K) = 2 * (b : K)) ∨ ((a : K) + (b : K) = 2 * (c : K)) ∨      ((b : K) + (c : K) = 2 * (a : K))) :    m.Coprime 6 ∧ ∃ α : K, α ^ 2 + 3 * α + 3 = 0 ∧      (orderOf α = m ∨ orderOf (α / 2) = m ∨ (orderOf α = 6 * m ∧ orderOf (-α - 3) = 3 * m)) := by  have hcop := coprime6_of_disjoint hm ha hb hc dac dbc  refine ⟨hcop, ?_⟩  have ha' : (a : K) = -1 := eq_neg_one_of_orderOf_two (by rw [orderOf_units]; exact ha)  have hb' : (b : K) ^ 2 + (b : K) + 1 = 0 :=    cube_of_orderOf_three (by rw [orderOf_units]; exact hb)  have hc' : orderOf (c : K) = m := by rw [orderOf_units]; exact hc  rcases hap with e | e | e  · -- centre of order 3: c = 2 b + 1    rw [ha'] at e    have hroot : ((b : K) - 1) ^ 2 + 3 * ((b : K) - 1) + 3 = 0 := by linear_combination hb'    refine ⟨(b : K) - 1, hroot, Or.inr (Or.inr ?_)⟩    have hz : 2 * ((b : K) - 1) + 3 = (c : K) := by linear_combination (-1 : K) * e    exact (lemma3 h2 h3 hm hcop hroot).mp (by rw [hz]; exact hc')  · -- centre of order m : (b - 1) / 2 = c    rw [ha'] at e    have hroot : ((b : K) - 1) ^ 2 + 3 * ((b : K) - 1) + 3 = 0 := by linear_combination hb'    refine ⟨(b : K) - 1, hroot, Or.inr (Or.inl ?_)⟩    have hz : ((b : K) - 1) / 2 = (c : K) := by      rw [div_eq_iff h2]      linear_combination e    rw [hz]    exact hc'  · -- centre of order 2 : c = -b - 2    rw [ha'] at e    have hc2 : (c : K) = -(b : K) - 2 := by linear_combination e    refine ⟨(c : K), ?_, Or.inl hc'⟩    rw [hc2]    linear_combination hb' theorem two_ne_zero_zmod {p m : ℕ} [Fact p.Prime] (hp : p = 6 * m + 1) (hm : 1 < m) :    (2 : ZMod p) ≠ 0 := by  intro h  have h' : ((2 : ℕ) : ZMod p) = 0 := by exact_mod_cast h  rw [ZMod.natCast_eq_zero_iff] at h'  have := Nat.le_of_dvd (by norm_num) h'  omega theorem three_ne_zero_zmod {p m : ℕ} [Fact p.Prime] (hp : p = 6 * m + 1) (hm : 1 < m) :    (3 : ZMod p) ≠ 0 := by  intro h  have h' : ((3 : ℕ) : ZMod p) = 0 := by exact_mod_cast h  rw [ZMod.natCast_eq_zero_iff] at h'  have := Nat.le_of_dvd (by norm_num) h'  omega end Field theorem necessity (p m : ℕ) [Fact p.Prime] (hp : p = 6 * m + 1) (hm : 1 < m) :    (∃ u v w : (ZMod p)ˣ, (u : ZMod p) + (w : ZMod p) = 2 * (v : ZMod p) ∧        [orderOf u, orderOf v, orderOf w].Perm [2, 3, m] ∧ Internal3 u v w) →      (m % 6 = 1 ∨ m % 6 = 5) ∧ ∃ α : ZMod p, α ^ 2 + 3 * α + 3 = 0 ∧        (orderOf α = m ∨ orderOf (α / 2) = m ∨          (orderOf α = p - 1 ∧ orderOf (-α - 3) = (p - 1) / 2)) := by  rintro ⟨u, v, w, hap, hperm, hI⟩  have h2 := two_ne_zero_zmod hp hm  have h3 := three_ne_zero_zmod hp hm  have hm0 : 0 < m := by omega  have duv := hI.disj_uv  have duw := hI.disj_uw  have dvw := hI.disj_vw  have key : m.Coprime 6 ∧ ∃ α : ZMod p, α ^ 2 + 3 * α + 3 = 0 ∧      (orderOf α = m ∨ orderOf (α / 2) = m ∨ (orderOf α = 6 * m ∧ orderOf (-α - 3) = 3 * m)) := by    rcases perm_cases hperm with ⟨o1, o2, o3⟩ | ⟨o1, o2, o3⟩ | ⟨o1, o2, o3⟩ | ⟨o1, o2, o3⟩ |        ⟨o1, o2, o3⟩ | ⟨o1, o2, o3⟩    · exact nec_core h2 h3 hm0 o1 o2 o3 duw dvw (Or.inl hap)    · exact nec_core h2 h3 hm0 (b := w) (c := v) o1 o3 o2 duv dvw.symm        (Or.inr (Or.inl hap))    · exact nec_core h2 h3 hm0 (a := v) (b := u) (c := w) o2 o1 o3 dvw duw        (Or.inr (Or.inr hap))    · exact nec_core h2 h3 hm0 (a := w) (b := u) (c := v) o3 o1 o2 dvw.symm duv        (Or.inr (Or.inl (by rw [add_comm]; exact hap)))    · exact nec_core h2 h3 hm0 (a := v) (b := w) (c := u) o2 o3 o1 duv.symm duw.symm        (Or.inr (Or.inr (by rw [add_comm]; exact hap)))    · exact nec_core h2 h3 hm0 (a := w) (b := v) (c := u) o3 o2 o1 duw.symm duv.symm        (Or.inl (by rw [add_comm]; exact hap))  obtain ⟨hcop, α, hα, hord⟩ := key  refine ⟨coprime6_iff.mp hcop, α, hα, ?_⟩  rcases hord with h | h | ⟨h1, h2'⟩  · exact Or.inl h  · exact Or.inr (Or.inl h)  · refine Or.inr (Or.inr ⟨?_, ?_⟩)    · rw [h1]; omega    · rw [h2']; omega /-! ### Part 5: sufficiency -/ theorem suff_core {p m : ℕ} [Fact p.Prime] (hp : p = 6 * m + 1)    (a b c : ZMod p) (ha : a ≠ 0) (hb : b ≠ 0) (hc : c ≠ 0) (hap : a + c = 2 * b)    {o1 o2 o3 : ℕ} (h1 : orderOf a = o1) (h2 : orderOf b = o2) (h3 : orderOf c = o3)    (c12 : o1.Coprime o2) (c13 : o1.Coprime o3) (c23 : o2.Coprime o3)    (hprod : o1 * o2 * o3 = 6 * m) (hperm : [o1, o2, o3].Perm [2, 3, m]) :    ∃ u v w : (ZMod p)ˣ, (u : ZMod p) + (w : ZMod p) = 2 * (v : ZMod p) ∧        [orderOf u, orderOf v, orderOf w].Perm [2, 3, m] ∧ Internal3 u v w := by  have e1 : orderOf (Units.mk0 a ha) = o1 := by rw [← orderOf_units, Units.val_mk0, h1]  have e2 : orderOf (Units.mk0 b hb) = o2 := by rw [← orderOf_units, Units.val_mk0, h2]  have e3 : orderOf (Units.mk0 c hc) = o3 := by rw [← orderOf_units, Units.val_mk0, h3]  refine ⟨Units.mk0 a ha, Units.mk0 b hb, Units.mk0 c hc, by simpa using hap, ?_, ?_⟩  · rw [e1, e2, e3]; exact hperm  · apply internal_of_coprime    · rw [e1, e2]; exact c12    · rw [e1, e3]; exact c13    · rw [e2, e3]; exact c23    · rw [e1, e2, e3, hprod, Nat.card_eq_fintype_card, ZMod.card_units p]      omega theorem sufficiency (p m : ℕ) [Fact p.Prime] (hp : p = 6 * m + 1) (hm : 1 < m) :    ((m % 6 = 1 ∨ m % 6 = 5) ∧ ∃ α : ZMod p, α ^ 2 + 3 * α + 3 = 0 ∧        (orderOf α = m ∨ orderOf (α / 2) = m ∨          (orderOf α = p - 1 ∧ orderOf (-α - 3) = (p - 1) / 2))) →      (∃ u v w : (ZMod p)ˣ, (u : ZMod p) + (w : ZMod p) = 2 * (v : ZMod p) ∧        [orderOf u, orderOf v, orderOf w].Perm [2, 3, m] ∧ Internal3 u v w) := by  rintro ⟨hm6, α, hα, hord⟩  have h2 := two_ne_zero_zmod hp hm  have h3 := three_ne_zero_zmod hp hm  have hm0 : 0 < m := by omega  have hcop : m.Coprime 6 := coprime6_iff.mpr hm6  have c3m : Nat.Coprime 3 m := (Nat.Coprime.coprime_dvd_right (by norm_num) hcop).symm  have c2m : Nat.Coprime 2 m := (Nat.Coprime.coprime_dvd_right (by norm_num) hcop).symm  have hω : (α + 1) ^ 2 + (α + 1) + 1 = 0 := by linear_combination hα  have oω : orderOf (α + 1) = 3 := orderOf_three_of_cube h3 hω  have oω2 : orderOf ((α + 1) ^ 2) = 3 :=    orderOf_three_of_cube h3 (by linear_combination ((α + 1) ^ 2 - (α + 1) + 1) * hω)  have hω0 : α + 1 ≠ 0 := by    intro h    rw [h] at hω    simp at hω  have hα0 : α ≠ 0 := by    intro h    apply h3    rw [h] at hα    linear_combination hα  have hn1 : (-1 : ZMod p) ≠ 0 := neg_ne_zero.mpr one_ne_zero  have on1 : orderOf (-1 : ZMod p) = 2 := orderOf_neg_one_two h2  rcases hord with h | h | ⟨h1, h2'⟩  · -- condition 1: (ω², -1, α)    refine suff_core hp ((α + 1) ^ 2) (-1) α (pow_ne_zero 2 hω0) hn1 hα0      (by linear_combination hα) oω2 on1 h (by norm_num) c3m c2m (by ring) ?_    rw [List.perm_iff_count]; intro x; simp [List.count_cons]; omega  · -- condition 2: (ω, α/2, -1)    refine suff_core hp (α + 1) (α / 2) (-1) hω0 (div_ne_zero hα0 h2) hn1      (by field_simp; ring) oω h on1 c3m (by norm_num) c2m.symm (by ring) ?_    rw [List.perm_iff_count]; intro x; simp [List.count_cons]; omega  · -- condition 3: (t, ω, -1) with t = 2α + 3    have h1' : orderOf α = 6 * m := by rw [h1]; omega    have h2'' : orderOf (-α - 3) = 3 * m := by rw [h2']; omega    have ht : orderOf (2 * α + 3) = m := (lemma3 h2 h3 hm0 hcop hα).mpr ⟨h1', h2''⟩    have ht0 : 2 * α + 3 ≠ 0 := by      intro h0      rw [h0, orderOf_zero] at ht      omega    refine suff_core hp (2 * α + 3) (α + 1) (-1) ht0 hω0 hn1      (by ring) ht oω on1 c3m.symm c2m.symm (by norm_num) (by ring) ?_    rw [List.perm_iff_count]; intro x; simp [List.count_cons]; omega /-- **Anderson–Cameron–Preece.** Let `p = 6m + 1` be prime, `m > 1`. The unit group of `ℤ/p` is an internal directproduct `⟨x⟩ × ⟨x + k⟩ × ⟨x + 2k⟩` of cyclic subgroups whose generators are in arithmetic progression and have orders`2, 3, m` in some order, if and only if `m ≡ 1` or `5 (mod 6)` and some root `α` of `X² + 3X + 3` satisfies`ord α = m`, or `ord (α/2) = m`, or `ord α = p − 1` and `ord β = (p − 1)/2` for the other root `β = −α − 3`. -/theorem classification (p m : ℕ) [Fact p.Prime] (hp : p = 6 * m + 1) (hm : 1 < m) :    (∃ u v w : (ZMod p)ˣ, (u : ZMod p) + (w : ZMod p) = 2 * (v : ZMod p) ∧        [orderOf u, orderOf v, orderOf w].Perm [2, 3, m] ∧ Internal3 u v w) ↔      (m % 6 = 1 ∨ m % 6 = 5) ∧ ∃ α : ZMod p, α ^ 2 + 3 * α + 3 = 0 ∧        (orderOf α = m ∨ orderOf (α / 2) = m ∨ (orderOf α = p - 1 ∧ orderOf (-α - 3) = (p - 1) / 2)) :=  ⟨necessity p m hp hm, sufficiency p m hp hm⟩ end ACP open ACP