import Checkpoint namespace CollatzRoute /-- Starting at 4s-1 performs two odd shortcuts, four raw steps in total. -/ private theorem block_four (s : Nat) (hs : 0 < s) (h3 : s%3 = 0) : orbit 4 (4*s-1) = 9*s-1 ∧ ∀ t : Nat, 0 < t → t ≤ 4 → orbit t (4*s-1) % 9 ≠ 2 := by have a : step (4*s-1) = 12*s-2 := by unfold step; split <;> omega have b : step (12*s-2) = 6*s-1 := by unfold step; split <;> omega have c : step (6*s-1) = 18*s-2 := by unfold step; split <;> omega have d : step (18*s-2) = 9*s-1 := by unfold step; split <;> omega refine ⟨by simp only [orbit, a, b, c, d], ?_⟩ intro t ht hcap have choices : t=1 ∨ t=2 ∨ t=3 ∨ t=4 := by omega rcases choices with h|h|h|h <;> subst t <;> simp only [orbit, a, b, c, d] <;> omega private theorem prefix_four (k c : Nat) (hc : 0 < c) (h3 : c%3 = 0) : orbit (4*k) (4^k*c-1) = 9^k*c-1 ∧ ∀ t : Nat, 0 < t → t ≤ 4*k → orbit t (4^k*c-1) % 9 ≠ 2 := by induction k generalizing c with | zero => exact ⟨by simp [orbit], by intro t a b; omega⟩ | succ k ih => let s := 4^k*c have hs : 0 < s := Nat.mul_pos (Nat.pow_pos (by decide)) hc have hs3 : s%3 = 0 := by simp [s, Nat.mul_mod, h3] have block := block_four s hs hs3 have hstart : 4^(k+1)*c-1 = 4*s-1 := by simp only [s,Nat.pow_succ]; ac_rfl have hnext : 9*s-1 = 4^k*(9*c)-1 := by dsimp [s]; ac_rfl have tail := ih (9*c) (by omega) (by omega) have hend : 9^k*(9*c)-1 = 9^(k+1)*c-1 := by simp only [Nat.pow_succ]; ac_rfl constructor · rw [hstart, show 4*(k+1) = 4+4*k by omega, orbit_add, block.1, hnext, tail.1, hend] · intro t ht hlimit rw [hstart] by_cases early : t≤4 · exact block.2 t ht early · have ht' : t=4+(t-4) := by omega rw [ht', orbit_add, block.1, hnext] exact tail.2 (t-4) (by omega) (by omega) def excursionStart (k : Nat) := 4^k*21-1 def excursionReturn (k : Nat) := (9^k*21-1)/4 private theorem four_pow_mod_three (k : Nat) : 4^k%3 = 1 := by induction k with | zero => decide | succ k ih => simp [Nat.pow_succ, Nat.mul_mod, ih] private theorem nine_pow_mod_four (k : Nat) : 9^k%4 = 1 := by induction k with | zero => decide | succ k ih => simp [Nat.pow_succ, Nat.mul_mod, ih] theorem excursion_start_checkpoint (k : Nat) : excursionStart k % 9 = 2 := by have h := four_pow_mod_three k unfold excursionStart; omega theorem first_checkpoint_return (k : Nat) (hk : 0 < k) : orbit (4*k+2) (excursionStart k) = excursionReturn k ∧ excursionReturn k % 9 = 2 ∧ ∀ t : Nat, 0 < t → t < 4*k+2 → orbit t (excursionStart k) % 9 ≠ 2 := by let y := 9^k*21-1 have h4 := nine_pow_mod_four k have h9 : 9^k % 9 = 0 := by cases k with | zero => omega | succ j => simp [Nat.pow_succ] have step1 : step y = y/2 := by unfold step; split <;> dsimp [y] at * <;> omega have step2 : step (y/2) = y/4 := by unfold step; split <;> dsimp [y] at * <;> omega have hpre := prefix_four k 21 (by decide) (by decide) change orbit (4*k+2) (4^k*21-1) = y/4 ∧ y/4%9=2 ∧ _ constructor · rw [orbit_add, hpre.1] change step (step y) = y/4 rw [step1,step2] constructor · dsimp [y]; omega · intro t ht hlimit by_cases before : t ≤ 4*k · exact hpre.2 t ht before · have htime : t=4*k+1 := by omega rw [htime, orbit_add] change orbit 1 (orbit (4*k) (4^k*21-1)) % 9 ≠ 2 rw [hpre.1] change step y % 9 ≠ 2 rw [step1] dsimp [y]; omega private theorem two_pow_linear (k : Nat) : k+1 ≤ 2^k := by induction k with | zero => decide | succ k ih => rw [Nat.pow_succ]; omega theorem excursion_start_mod576 (k : Nat) (hk : 3 ≤ k) : excursionStart k % 576 = 191 := by have h3 : k=3+(k-3) := by omega have h64 : 4^k%64=0 := by rw [h3,Nat.pow_add] change 64 * 4^(k-3)%64=0 simp have hn := excursion_start_checkpoint k unfold excursionStart at * omega /-- Even on just residue 191, the first positive checkpoint return has unbounded gain. -/ theorem first_return_gain (B k : Nat) (large : 4*B+4 ≤ k) : B * excursionStart k < excursionReturn k := by let a := 4^k*21 have ap : 0 < a := Nat.mul_pos (Nat.pow_pos (by decide)) (by decide) have lg := two_pow_linear k have hsmall : 4*B+4 ≤ 2^k := by omega have scale : (4*B+4)*a ≤ 9^k*21 := by calc (4*B+4)*a ≤ 2^k*a := Nat.mul_le_mul_right a hsmall _ = 8^k*21 := by dsimp [a]; rw [← Nat.mul_assoc, ← Nat.mul_pow] _ ≤ 9^k*21 := Nat.mul_le_mul_right 21 (Nat.pow_le_pow_left (by decide) k) have lhs : 4*(B*a)+4*a ≤ 9^k*21 := by simpa only [Nat.add_mul,Nat.mul_assoc] using scale have lt0 : B*(a-1) ≤ B*a := Nat.mul_le_mul_left _ (by omega) change B*(a-1) < (9^k*21-1)/4 omega theorem unbounded_first_return (B : Nat) : ∃ k, 3 ≤ k ∧ excursionStart k % 576 = 191 ∧ B * excursionStart k < excursionReturn k := by let k := 4*B+4 have hk : 3 ≤ k := by dsimp [k]; omega exact ⟨k,hk,excursion_start_mod576 k hk,first_return_gain B k (Nat.le_refl _)⟩ /-- For 74 modulo 81 the *smaller* checkpoint is an actual predecessor, five raw steps back. -/ theorem five_step_predecessor (q : Nat) : 72*q+65 < 81*q+74 ∧ (72*q+65)%9 = 2 ∧ orbit 5 (72*q+65) = 81*q+74 := by have a : step (72*q+65) = 216*q+196 := by unfold step; split <;> omega have b : step (216*q+196) = 108*q+98 := by unfold step; split <;> omega have c : step (108*q+98) = 54*q+49 := by unfold step; split <;> omega have d : step (54*q+49) = 162*q+148 := by unfold step; split <;> omega have e : step (162*q+148) = 81*q+74 := by unfold step; split <;> omega exact ⟨by omega,by omega,by simp only [orbit,a,b,c,d,e]⟩ theorem subsequence_mod81 (j : Nat) : excursionStart (9*j+6) % 81 = 74 := by have mod27 : 4^(9*j+6) % 27 = 19 := by induction j with | zero => decide | succ j ih => rw [show 9*(j+1)+6 = (9*j+6)+9 by omega, Nat.pow_add, Nat.mul_mod, ih] unfold excursionStart; omega /-- A single infinite subsequence exhibits both effects at once, for any demanded gain. -/ theorem large_return_with_smaller_checkpoint (B : Nat) : ∃ k m, 3 ≤ k ∧ excursionStart k % 576 = 191 ∧ B * excursionStart k < excursionReturn k ∧ m % 9 = 2 ∧ m < excursionStart k ∧ orbit 5 m = excursionStart k := by let k := 9*B+6 have hk : 3 ≤ k := by dsimp [k]; omega have hmod := subsequence_mod81 B change excursionStart k % 81 = 74 at hmod let q := excursionStart k / 81 have repr : 81*q+74 = excursionStart k := by dsimp [q]; omega obtain ⟨lt, checkpoint, orbit5⟩ := five_step_predecessor q rw [repr] at lt orbit5 exact ⟨k,72*q+65,hk,excursion_start_mod576 k hk, first_return_gain B k (by dsimp [k]; omega),checkpoint,lt,orbit5⟩ #print axioms large_return_with_smaller_checkpoint #print axioms first_checkpoint_return #print axioms excursion_start_mod576 #print axioms unbounded_first_return end CollatzRoute