import Mathlib /-! # Theorem 1.2: Universal Offset Absorbability for `1 < a < d^2` For every integer `d ≥ 2`, `1 < a < d^2` with `Nat.Coprime a d`, and every offset `m ≥ 1` (in fact every `μ : ℤ`), there exists a finite digit word `w : List (Fin d)` taking `(0, μ)` to the absorbed state `(0, 0)` under the d-adic pair chain. Proof (strong induction on `|μ|`): * If `μ = 0`, the empty word works. * If `d ∣ μ`, digit `0` divides `μ` by `d`, strictly decreasing `|μ|`. * If `¬ (d ∣ μ)`: 1. Take `p ≡ 0 (mod d)` (or `q ≡ 0 (mod d)` when `μ < 0`) to move from `(0, μ)` to `(1, N)` with `N = (a * |μ| + res d a |μ|) / d ≥ 1` and `N ≤ d * |μ|`. 2. Divide out the maximal power `d^k` dividing `N` using `k` copies of digit `0`, reaching `(1, s)` with `1 ≤ s ≤ N ≤ d * |μ|` and `¬ (d ∣ s)`. 3. Choose the unique nonzero digit `c₀ ∈ {1, ..., d - 1}` with `a * c₀ + s ≡ 0 (mod d)` (`q ≡ 0 (mod d)`), reaching `(0, |(s - r_{c₀}) / d|)` with `|(s - r_{c₀}) / d| < |μ|`. -/ namespace DAdicCollatz.OffsetAbsorb /-! ## Core d-adic pair chain definitions -/ def res (d a : ℕ) (x : ℤ) : ℤ := (-(a : ℤ) * x) % (d : ℤ) structure State where s : ℕ mu : ℤ deriving DecidableEq, Repr def State.norm (st : State) : State := if st.s = 0 then ⟨0, |st.mu|⟩ else st def absorbed : State := ⟨0, 0⟩ def step (d a : ℕ) (st : State) (c : Fin d) : State := let qRem := ((a : ℤ) ^ st.s * (c.val : ℤ) + st.mu) % (d : ℤ) if c.val = 0 then if qRem = 0 then (⟨st.s, st.mu / (d : ℤ)⟩ : State).norm else ⟨st.s + 1, ((a : ℤ) * st.mu + res d a st.mu) / (d : ℤ)⟩ else if qRem = 0 then match st.s with | 0 => ⟨1, (res d a (c.val : ℤ) - (a : ℤ) * st.mu) / (d : ℤ)⟩ | s' + 1 => (⟨s', (st.mu - (a : ℤ) ^ s' * res d a (c.val : ℤ)) / (d : ℤ)⟩ : State).norm else (⟨st.s, ((a : ℤ) * st.mu + res d a ((a : ℤ) ^ st.s * (c.val : ℤ) + st.mu) - (a : ℤ) ^ st.s * res d a (c.val : ℤ)) / (d : ℤ)⟩ : State).norm def run (d a : ℕ) (st : State) (w : List (Fin d)) : State := w.foldl (step d a) st /-! ## Helper lemmas on `res`, `run`, and modular inverses -/ theorem run_nil (d a : ℕ) (st : State) : run d a st [] = st := rfl theorem run_cons (d a : ℕ) (st : State) (c : Fin d) (w : List (Fin d)) : run d a st (c :: w) = run d a (step d a st c) w := rfl theorem run_append (d a : ℕ) (st : State) (w₁ w₂ : List (Fin d)) : run d a st (w₁ ++ w₂) = run d a (run d a st w₁) w₂ := by simp [run, List.foldl_append] theorem res_nonneg {d a : ℕ} (hd : 0 < d) (x : ℤ) : 0 ≤ res d a x := Int.emod_nonneg _ (by omega) theorem res_lt {d a : ℕ} (hd : 0 < d) (x : ℤ) : res d a x < (d : ℤ) := Int.emod_lt_of_pos _ (by omega) theorem dvd_mul_add_res (d a : ℕ) (x : ℤ) : (d : ℤ) ∣ (a : ℤ) * x + res d a x := by apply Int.dvd_of_emod_eq_zero unfold res rw [Int.add_emod, Int.emod_emod, ← Int.add_emod] have h : (a : ℤ) * x + -(a : ℤ) * x = 0 := by ring rw [h, Int.zero_emod] theorem res_congr {d a : ℕ} {x y : ℤ} (h : x % (d : ℤ) = y % (d : ℤ)) : res d a x = res d a y := by unfold res rw [Int.mul_emod, Int.mul_emod (-(a : ℤ)) y, h] theorem res_pos_of_emod_ne_zero {d a : ℕ} (hd : 0 < d) (hcop : Nat.Coprime a d) {x : ℤ} (hx : x % (d : ℤ) ≠ 0) : 1 ≤ res d a x := by have h0 : 0 ≤ res d a x := res_nonneg hd x have hne : res d a x ≠ 0 := by intro h apply hx have hdvd : (d : ℤ) ∣ -(a : ℤ) * x := Int.dvd_of_emod_eq_zero h rw [show -(a : ℤ) * x = -((a : ℤ) * x) by ring, dvd_neg] at hdvd have hcopZ : IsCoprime (d : ℤ) (a : ℤ) := Nat.isCoprime_iff_coprime.mpr hcop.symm exact Int.emod_eq_zero_of_dvd (hcopZ.dvd_of_dvd_mul_left hdvd) omega /-- Every positive natural number factors as `d^k * s` with `s ≥ 1` and `¬ (d ∣ s)`. -/ theorem exists_pow_mul_not_dvd {d : ℕ} (hd : 2 ≤ d) : ∀ n : ℕ, 0 < n → ∃ k s : ℕ, 0 < s ∧ ¬ d ∣ s ∧ n = d ^ k * s := by intro n induction n using Nat.strong_induction_on with | _ n ih => intro hn by_cases hdiv : d ∣ n · obtain ⟨m, rfl⟩ := hdiv have hm_pos : 0 < m := by nlinarith have hm_lt : m < d * m := by nlinarith obtain ⟨k, s, hs_pos, hs_ndvd, rfl⟩ := ih m hm_lt hm_pos refine ⟨k + 1, s, hs_pos, hs_ndvd, ?_⟩ rw [pow_succ'] ring · exact ⟨0, n, hn, hdiv, by simp⟩ /-- Given `Nat.Coprime a d` and `¬ (d ∣ s)`, there exists a nonzero digit `c : Fin d` such that `(a * c + s) % d = 0`. -/ theorem exists_nonzero_digit_dvd {d a : ℕ} (hd : 2 ≤ d) (hcop : Nat.Coprime a d) (s : ℤ) (hs : s % (d : ℤ) ≠ 0) : ∃ c : Fin d, c.val ≠ 0 ∧ ((a : ℤ) * (c.val : ℤ) + s) % (d : ℤ) = 0 := by have hdpos : 0 < d := by omega have hcopZ : IsCoprime (a : ℤ) (d : ℤ) := Nat.isCoprime_iff_coprime.mpr hcop obtain ⟨u, v, huv⟩ := hcopZ set r : ℤ := (-u * s) % (d : ℤ) have hr0 : 0 ≤ r := Int.emod_nonneg _ (by omega) have hrlt : r < (d : ℤ) := Int.emod_lt_of_pos _ (by omega) let c : Fin d := ⟨r.toNat, by omega⟩ have hc_cast : (c.val : ℤ) = r := Int.toNat_of_nonneg hr0 have hmod : ((a : ℤ) * (c.val : ℤ) + s) % (d : ℤ) = 0 := by rw [hc_cast] apply Int.emod_eq_zero_of_dvd have h1 : (a : ℤ) * (-u * s) + s = (v * s) * (d : ℤ) := by linear_combination -(s) * huv have h2 : (d : ℤ) ∣ r - (-u * s) := by apply Int.dvd_of_emod_eq_zero change ((-u * s) % (d : ℤ) - (-u * s)) % (d : ℤ) = 0 rw [Int.sub_emod, Int.emod_emod, sub_self, Int.zero_emod] have h3 : (a : ℤ) * r + s = (a : ℤ) * (r - (-u * s)) + (v * s) * (d : ℤ) := by linear_combination h1 rw [h3] exact dvd_add (dvd_mul_of_dvd_right h2 _) (dvd_mul_left _ _) have hc_ne : c.val ≠ 0 := by intro h0 rw [h0, Nat.cast_zero, mul_zero, zero_add] at hmod exact hs hmod exact ⟨c, hc_ne, hmod⟩ /-- At level 1, `k` steps of digit `0` divide out `d^k` from `d^k * s`. -/ theorem run_replicate_zero_level_one {d a : ℕ} (hd : 2 ≤ d) (k : ℕ) (s : ℤ) : run d a ⟨1, (d : ℤ) ^ k * s⟩ (List.replicate k ⟨0, by omega⟩) = ⟨1, s⟩ := by have hd0 : (d : ℤ) ≠ 0 := by omega induction k with | zero => simp [run] | succ k ih => rw [List.replicate_succ, run_cons] have hstep : step d a ⟨1, (d : ℤ) ^ (k + 1) * s⟩ ⟨0, by omega⟩ = ⟨1, (d : ℤ) ^ k * s⟩ := by unfold step dsimp only have hmul : (d : ℤ) ^ (k + 1) * s = (d : ℤ) * ((d : ℤ) ^ k * s) := by rw [pow_succ'] ring have hq : ((a : ℤ) ^ 1 * (0 : ℤ) + (d : ℤ) ^ (k + 1) * s) % (d : ℤ) = 0 := by rw [mul_zero, zero_add, hmul, Int.mul_emod_right] rw [if_pos rfl, Nat.cast_zero, if_pos hq] simp only [State.norm, show (1 : ℕ) ≠ 0 by omega, if_false] congr 1 rw [hmul, Int.mul_ediv_cancel_left _ hd0] rw [hstep] exact ih /-- From `⟨1, d^k * s⟩` with `s ≥ 1`, `¬ (d ∣ s)`, and `s ≤ d * m` (`m ≥ 1`), one can reach `⟨0, m'⟩` with `0 ≤ m' < m`. -/ theorem level_one_to_level_zero_lt {d a : ℕ} (hd : 2 ≤ d) (hcop : Nat.Coprime a d) (m : ℤ) (hm : 1 ≤ m) (k : ℕ) (s : ℤ) (hs_pos : 1 ≤ s) (hs_ndvd : s % (d : ℤ) ≠ 0) (hs_le : s ≤ (d : ℤ) * m) : ∃ (w : List (Fin d)) (m' : ℤ), 0 ≤ m' ∧ m' < m ∧ run d a ⟨1, (d : ℤ) ^ k * s⟩ w = ⟨0, m'⟩ := by have hdpos : 0 < d := by omega have hdZ : (2 : ℤ) ≤ (d : ℤ) := by exact_mod_cast hd obtain ⟨c₀, hc₀_ne, hc₀_mod⟩ := exists_nonzero_digit_dvd hd hcop s hs_ndvd let r : ℤ := res d a (c₀.val : ℤ) have hr_ge : 1 ≤ r := by apply res_pos_of_emod_ne_zero hdpos hcop rw [Int.emod_eq_of_lt (Int.natCast_nonneg _) (by exact_mod_cast c₀.isLt)] exact_mod_cast hc₀_ne have hr_lt : r < (d : ℤ) := res_lt hdpos _ have hdvd : (d : ℤ) ∣ s - r := by have h1 : (d : ℤ) ∣ (a : ℤ) * (c₀.val : ℤ) + s := Int.dvd_of_emod_eq_zero hc₀_mod have h2 : (d : ℤ) ∣ (a : ℤ) * (c₀.val : ℤ) + r := dvd_mul_add_res d a (c₀.val : ℤ) have h3 : s - r = ((a : ℤ) * (c₀.val : ℤ) + s) - ((a : ℤ) * (c₀.val : ℤ) + r) := by ring rw [h3] exact dvd_sub h1 h2 have hdiv : (d : ℤ) * ((s - r) / (d : ℤ)) = s - r := Int.mul_ediv_cancel' hdvd let m' : ℤ := |(s - r) / (d : ℤ)| have hm'_nonneg : 0 ≤ m' := abs_nonneg _ have hm'_lt : m' < m := by have hlow : -((d : ℤ) * m) < s - r := by nlinarith have hhigh : s - r < (d : ℤ) * m := by linarith have hq_low : -m < (s - r) / (d : ℤ) := by nlinarith have hq_high : (s - r) / (d : ℤ) < m := by nlinarith exact abs_lt.mpr ⟨hq_low, hq_high⟩ refine ⟨List.replicate k ⟨0, by omega⟩ ++ [c₀], m', hm'_nonneg, hm'_lt, ?_⟩ rw [run_append, run_replicate_zero_level_one hd k s, run_cons, run_nil] unfold step dsimp only rw [if_neg hc₀_ne, pow_one, if_pos hc₀_mod] simp only [pow_zero, one_mul, State.norm, if_true] rfl /-! ## Theorem 1.2: Universal Offset Absorbability -/ /-- **Theorem 1.2 (all integer offsets).** For all `d ≥ 2`, `1 < a < d^2` with `Nat.Coprime a d`, and every offset `μ : ℤ`, there exists a finite digit word taking `(0, μ)` to `(0, 0)`. -/ theorem offset_absorbable {d a : ℕ} (hd : 2 ≤ d) (ha_gt : 1 < a) (ha_lt : a < d ^ 2) (hcop : Nat.Coprime a d) (μ : ℤ) : ∃ w : List (Fin d), run d a ⟨0, μ⟩ w = absorbed := by have hdpos : 0 < d := by omega have hdZ : (2 : ℤ) ≤ (d : ℤ) := by exact_mod_cast hd have ha_leZ : (a : ℤ) ≤ (d : ℤ) ^ 2 - 1 := by have h : (a : ℤ) < ((d ^ 2 : ℕ) : ℤ) := by exact_mod_cast ha_lt push_cast at h omega induction hn : μ.natAbs using Nat.strong_induction_on generalizing μ with | _ n ih => subst hn by_cases h0 : μ = 0 · subst h0 exact ⟨[], rfl⟩ by_cases hdiv : μ % (d : ℤ) = 0 · -- Case 1: `d ∣ μ`. Take digit `0`, reaching `⟨0, |μ / d|⟩` with `|μ / d| < |μ|`. have hμd : (d : ℤ) * (μ / (d : ℤ)) = μ := Int.mul_ediv_cancel' (Int.dvd_of_emod_eq_zero hdiv) have hlt : (|μ / (d : ℤ)|).natAbs < μ.natAbs := by rw [Int.natAbs_abs] have h1 : (d : ℤ) * |μ / (d : ℤ)| = |μ| := by calc (d : ℤ) * |μ / (d : ℤ)| = |(d : ℤ)| * |μ / (d : ℤ)| := by rw [abs_of_nonneg (show (0 : ℤ) ≤ d by omega)] _ = |(d : ℤ) * (μ / (d : ℤ))| := (abs_mul _ _).symm _ = |μ| := by rw [hμd] have h2 : 0 < |μ| := abs_pos.mpr h0 have h3 : |μ / (d : ℤ)| < |μ| := by nlinarith have h4 : (((μ / (d : ℤ)).natAbs : ℕ) : ℤ) < ((μ.natAbs : ℕ) : ℤ) := by simpa [Int.natCast_natAbs] using h3 exact_mod_cast h4 obtain ⟨w, hw⟩ := ih (|μ / (d : ℤ)|).natAbs hlt (|μ / (d : ℤ)|) rfl refine ⟨⟨0, hdpos⟩ :: w, ?_⟩ rw [run_cons] have hstep : step d a ⟨0, μ⟩ ⟨0, hdpos⟩ = ⟨0, |μ / (d : ℤ)|⟩ := by unfold step dsimp only rw [if_pos rfl, pow_zero, one_mul, Nat.cast_zero, zero_add, if_pos hdiv] simp [State.norm] rw [hstep] exact hw · -- Case 2: `¬ (d ∣ μ)`. set m : ℤ := |μ| have hm_pos : 1 ≤ m := by have : 0 < |μ| := abs_pos.mpr h0 omega have hm_nat : m.natAbs = μ.natAbs := Int.natAbs_abs μ -- Step 1: reach `⟨1, N⟩` where `N = (a * m + res d a m) / d` have hN_dvd : (d : ℤ) ∣ (a : ℤ) * m + res d a m := dvd_mul_add_res d a m let N : ℤ := ((a : ℤ) * m + res d a m) / (d : ℤ) have hN_mul : (d : ℤ) * N = (a : ℤ) * m + res d a m := Int.mul_ediv_cancel' hN_dvd have hr_nonneg : 0 ≤ res d a m := res_nonneg hdpos m have hr_lt : res d a m < (d : ℤ) := res_lt hdpos m have hN_pos : 1 ≤ N := by have h1 : (2 : ℤ) ≤ (a : ℤ) := by exact_mod_cast ha_gt have h2 : (d : ℤ) * N ≥ 2 := by nlinarith nlinarith have hN_le : N ≤ (d : ℤ) * m := by have h1 : (d : ℤ) * N < (d : ℤ) * ((d : ℤ) * m + 1) := by nlinarith nlinarith -- Reach `⟨1, N⟩` in 1 step from `⟨0, μ⟩` have hfirst : ∃ c₁ : Fin d, step d a ⟨0, μ⟩ c₁ = ⟨1, N⟩ := by rcases lt_or_gt_of_ne h0 with hneg | hpos · -- `μ < 0`: take `c₁` with `(c₁ + μ) % d = 0` (`q ≡ 0 mod d`) have hm_eq : m = -μ := abs_of_neg hneg have h1 : ∃ c₁ : Fin d, c₁.val ≠ 0 ∧ ((c₁.val : ℤ) + μ) % (d : ℤ) = 0 := by obtain ⟨c₁, hc1, hmod⟩ := exists_nonzero_digit_dvd hd (Nat.coprime_one_left d) μ hdiv refine ⟨c₁, hc1, ?_⟩ simpa using hmod obtain ⟨c₁, hc1_ne, hc1_mod⟩ := h1 refine ⟨c₁, ?_⟩ unfold step dsimp only rw [if_neg hc1_ne, pow_zero, one_mul, if_pos hc1_mod] congr 1 have hres_eq : res d a (c₁.val : ℤ) = res d a m := by apply res_congr rw [hm_eq] have hdvd' : (d : ℤ) ∣ (c₁.val : ℤ) - (-μ) := by rw [sub_neg_eq_add] exact Int.dvd_of_emod_eq_zero hc1_mod exact Int.ModEq.symm (Int.modEq_iff_dvd.mpr (by simpa [sub_eq_add_neg] using hdvd')) rw [hres_eq] congr 1 rw [hm_eq] ring · -- `μ > 0`: take `c₁ = 0` (`p ≡ 0 mod d`) have hm_eq : m = μ := abs_of_pos hpos refine ⟨⟨0, hdpos⟩, ?_⟩ unfold step dsimp only rw [if_pos rfl, pow_zero, one_mul, Nat.cast_zero, zero_add, if_neg hdiv] simp [N, hm_eq] obtain ⟨c₁, hc₁⟩ := hfirst -- Step 2: factor `N = d^k * s` with `s ≥ 1` and `¬ (d ∣ s)` obtain ⟨k, sNat, hs_pos, hs_ndvd, hN_fac⟩ := exists_pow_mul_not_dvd hd N.toNat (by omega) have hN_eq : N = (d : ℤ) ^ k * (sNat : ℤ) := by have h1 : (N.toNat : ℤ) = N := Int.toNat_of_nonneg (by omega) rw [← h1, hN_fac] push_cast rfl have hs_posZ : 1 ≤ (sNat : ℤ) := by exact_mod_cast hs_pos have hs_ndvdZ : (sNat : ℤ) % (d : ℤ) ≠ 0 := by intro h apply hs_ndvd have : (d : ℤ) ∣ (sNat : ℤ) := Int.dvd_of_emod_eq_zero h exact_mod_cast this have hs_le : (sNat : ℤ) ≤ (d : ℤ) * m := by have hdk : (1 : ℤ) ≤ (d : ℤ) ^ k := one_le_pow₀ (by omega) have : (sNat : ℤ) ≤ N := by rw [hN_eq] nlinarith exact this.trans hN_le -- Step 3: divide out `d^k` and take `q ≡ 0 (mod d)` to reach `⟨0, m'⟩` with `0 ≤ m' < m` obtain ⟨w₂, m', hm'_nonneg, hm'_lt, hw₂⟩ := level_one_to_level_zero_lt hd hcop m hm_pos k (sNat : ℤ) hs_posZ hs_ndvdZ hs_le have hm'_nat_lt : m'.natAbs < μ.natAbs := by rw [← hm_nat] omega obtain ⟨w₃, hw₃⟩ := ih m'.natAbs hm'_nat_lt m' rfl refine ⟨c₁ :: (w₂ ++ w₃), ?_⟩ rw [run_cons, hc₁, hN_eq, run_append, hw₂] exact hw₃ /-- **Theorem 1.2 (positive offsets `m ≥ 1`).** -/ theorem offset_absorbable_pos {d a : ℕ} (hd : 2 ≤ d) (ha_gt : 1 < a) (ha_lt : a < d ^ 2) (hcop : Nat.Coprime a d) (m : ℕ) (_hm : 1 ≤ m) : ∃ w : List (Fin d), run d a ⟨0, (m : ℤ)⟩ w = absorbed := offset_absorbable hd ha_gt ha_lt hcop (m : ℤ) end DAdicCollatz.OffsetAbsorb #print axioms DAdicCollatz.OffsetAbsorb.offset_absorbable #print axioms DAdicCollatz.OffsetAbsorb.offset_absorbable_pos