import Inverse namespace CollatzRoute /-- Shortcut Collatz map. An odd shortcut traverses two raw edges. -/ def short (n : Nat) : Nat := if n % 2 = 0 then n / 2 else (3*n+1)/2 def HitsTwoModNine (n : Nat) : Prop := ∃ t, orbit t n % 9 = 2 theorem short_lifts {n : Nat} (h : HitsTwoModNine (short n)) : HitsTwoModNine n := by obtain ⟨t, ht⟩ := h by_cases he : n % 2 = 0 · exact ⟨t+1, by simpa [orbit, step, short, he] using ht⟩ · have half : 3*n+1 = 2*((3*n+1)/2) := by omega refine ⟨(1+t)+1, ?_⟩ rw [inverse_block (by omega) (show 3*n+1 = 2^1*((3*n+1)/2) from half)] simpa [short, he] using ht theorem short_pos {n : Nat} (hn : 0 < n) : 0 < short n := by unfold short; split <;> omega private theorem hit_four {n : Nat} (hr : n%9 = 4) : HitsTwoModNine n := by apply short_lifts exact ⟨0, by change short n % 9 = 2; unfold short; split <;> omega⟩ /-- An odd shortcut turns n+1 = a·(2u) into short(n)+1 = (3a)·u. The factor u strictly decreases even while the Collatz value itself grows. -/ private theorem hit_eight_aux (z : Nat) : ∀ a n : Nat, a%2 = 1 → a*z = n+1 → n%9 = 8 → HitsTwoModNine n := by induction z using Nat.strongRecOn with | ind z ih => intro a n ha hz hr by_cases he : n%2 = 0 · apply short_lifts apply hit_four simp only [short, he, ite_true]; omega · have evenz : z%2 = 0 := by have h := congrArg (fun k => k%2) hz simp only [Nat.mul_mod a z 2, ha, Nat.one_mul, Nat.mod_mod] at h omega have posz : 0 < z := by cases z with | zero => simp at hz | succ z => omega have za : 2*(z/2) = z := by omega have hp : 2*(a*(z/2)) = n+1 := by calc 2*(a*(z/2)) = a*(2*(z/2)) := by ac_rfl _ = n+1 := by rw [za, hz] apply short_lifts apply ih (z/2) (by omega) (3*a) (short n) · omega · rw [Nat.mul_assoc] simp only [short, he, ite_false] omega · simp only [short, he, ite_false]; omega private theorem hit_eight {n : Nat} (hr : n%9 = 8) : HitsTwoModNine n := hit_eight_aux (n+1) 1 n (by decide) (Nat.one_mul _) hr private theorem hit_seven {n : Nat} (hr : n%9 = 7) : HitsTwoModNine n := by apply short_lifts have h : short n%9 = 2 ∨ short n%9 = 8 := by unfold short; split <;> omega rcases h with target | eight · exact ⟨0, target⟩ · exact hit_eight eight private theorem hit_five {n : Nat} (hr : n%9 = 5) : HitsTwoModNine n := by apply short_lifts have h : short n%9 = 7 ∨ short n%9 = 8 := by unfold short; split <;> omega exact h.elim hit_seven hit_eight private theorem hit_one_residue {n : Nat} (hr : n%9 = 1) : HitsTwoModNine n := by apply short_lifts have h : short n%9 = 2 ∨ short n%9 = 5 := by unfold short; split <;> omega exact h.elim (fun h => ⟨0,h⟩) hit_five /-- Raw-rule version of Monks et al., Corollary 5.8 (2013). -/ theorem every_orbit_hits_two_mod_nine (n : Nat) (hn : 0 < n) : HitsTwoModNine n := by induction n using Nat.strongRecOn with | ind n ih => by_cases div3 : n%3 = 0 · apply short_lifts by_cases he : n%2 = 0 · apply ih (short n) _ (short_pos hn) simp only [short, he, ite_true]; omega · apply hit_five simp only [short, he, ite_false]; omega · have opts : n%9 = 1 ∨ n%9 = 2 ∨ n%9 = 4 ∨ n%9 = 5 ∨ n%9 = 7 ∨ n%9 = 8 := by omega rcases opts with h|h|h|h|h|h · exact hit_one_residue h · exact ⟨0,h⟩ · exact hit_four h · exact hit_five h · exact hit_seven h · exact hit_eight h theorem claim_iff_two_mod_nine : Claim ↔ ∀ y : Nat, y%9 = 2 → ReachesOne y := by constructor · intro goal y hy exact goal _ (by omega) · intro checkpoints n hn obtain ⟨s, hs⟩ := every_orbit_hits_two_mod_nine n hn obtain ⟨t, ht⟩ := checkpoints _ hs exact ⟨s+t, by rw [orbit_add]; exact ht⟩ /-- The smaller odd predecessor always sends us back to the same first checkpoint. -/ theorem predecessor_first_checkpoint (k : Nat) : 6*k+1 < 9*k+2 ∧ orbit 2 (6*k+1) = 9*k+2 ∧ ∀ t : Fin 2, orbit t.val (6*k+1) % 9 ≠ 2 := by have a : step (6*k+1) = 2*(9*k+2) := by rw [step_odd (by omega : (6*k+1)%2 = 1)]; omega refine ⟨by omega, by simp only [orbit, a, step_double], ?_⟩ intro t have casesT : t.val = 0 ∨ t.val = 1 := by omega rcases casesT with h | h · simp only [h, orbit]; omega · simp only [h, orbit, a]; omega #print axioms predecessor_first_checkpoint #print axioms every_orbit_hits_two_mod_nine #print axioms claim_iff_two_mod_nine end CollatzRoute