import Bridges import FirstReturn namespace CollatzRoute /-- Ten raw forward steps take one entire progression onto another. -/ theorem ten_step_image (q : Nat) : orbit 10 (576*q+569) = 729*q+722 := by have a : step (576*q+569) = 1728*q+1708 := by unfold step; split <;> omega have b : step (1728*q+1708) = 864*q+854 := by unfold step; split <;> omega have c : step (864*q+854) = 432*q+427 := by unfold step; split <;> omega have d : step (432*q+427) = 1296*q+1282 := by unfold step; split <;> omega have e : step (1296*q+1282) = 648*q+641 := by unfold step; split <;> omega have f : step (648*q+641) = 1944*q+1924 := by unfold step; split <;> omega have g : step (1944*q+1924) = 972*q+962 := by unfold step; split <;> omega have h : step (972*q+962) = 486*q+481 := by unfold step; split <;> omega have i : step (486*q+481) = 1458*q+1444 := by unfold step; split <;> omega have j : step (1458*q+1444) = 729*q+722 := by unfold step; split <;> omega simp only [orbit,a,b,c,d,e,f,g,h,i,j] /-- Entry i is a doubling exponent for the residue 9*i+2 modulo 729. -/ def doublingExponents : Array Nat := #[ 150, 354, 126, 438, 318, 252, 240, 282, 378, 42, 246, 18, 330, 210, 144, 132, 174, 270, 420, 138, 396, 222, 102, 36, 24, 66, 162, 312, 30, 288, 114, 480, 414, 402, 444, 54, 204, 408, 180, 6, 372, 306, 294, 336, 432, 96, 300, 72, 384, 264, 198, 186, 228, 324, 474, 192, 450, 276, 156, 90, 78, 120, 216, 366, 84, 342, 168, 48, 468, 456, 12, 108, 258, 462, 234, 60, 426, 360, 348, 390, 0] set_option exponentiation.threshold 512 in set_option maxRecDepth 12000 in theorem doubling_table : ∀ i : Fin 81, let e := doublingExponents[i.val]?.getD 0 e ≤ 480 ∧ (2^e * (9*i.val+2))%729 = 722 := by decide theorem from_single_class (n : Nat) (hn : n%9=2) : ∃ q t : Nat, t ≤ 490 ∧ orbit t (576*q+569) = n := by let index : Fin 81 := ⟨(n%729)/9, by omega⟩ let e := doublingExponents[index.val]?.getD 0 have h := doubling_table index change e ≤ 480 ∧ (2^e * (9*index.val+2))%729=722 at h have hr : 9*index.val+2 = n%729 := by dsimp [index]; omega rw [hr, Nat.mul_mod_mod] at h let y := 2^e*n let q := y/729 have rep : 729*q+722 = y := by have hres : y%729 = 722 := h.2 dsimp [q]; omega refine ⟨q,10+e,by omega,?_⟩ rw [orbit_add,ten_step_image,rep] simpa only [Nat.add_zero,orbit] using strip_twos e 0 n theorem reaches_orbit_iff (t n : Nat) : ReachesOne n ↔ ReachesOne (orbit t n) := by induction t generalizing n with | zero => exact Iff.rfl | succ t ih => exact (reaches_step_iff n).trans (ih (step n)) /-- A constructive special case of sufficiency of arithmetic progressions. -/ theorem claim_iff_569 : Claim ↔ ∀ q : Nat, ReachesOne (576*q+569) := by constructor · intro all q exact all _ (by omega) · intro target apply claim_iff_two_mod_nine.mpr intro n hn obtain ⟨q,t,_,eq⟩ := from_single_class n hn rw [← eq] exact (reaches_orbit_iff _ _).mp (target q) def Merge (x y : Nat) : Prop := ∃ s t : Nat, orbit s x = orbit t y set_option maxRecDepth 12000 in theorem claim_iff_smaller_index : Claim ↔ ∀ q : Nat, 0 < q → ∃ p : Nat, p < q ∧ Merge (576*q+569) (576*p+569) := by constructor · intro all q hq obtain ⟨s, hs⟩ := all (576*q+569) (by omega) obtain ⟨t, ht⟩ := all (576*0+569) (by decide) exact ⟨0,hq,s,t,hs.trans ht.symm⟩ · intro smaller apply claim_iff_569.mpr intro q induction q using Nat.strongRecOn with | ind q ih => by_cases zero : q=0 · subst q; exact ⟨56,by decide⟩ · obtain ⟨p,hp,s,t,meet⟩ := smaller q (by omega) apply (reaches_orbit_iff s _).mpr rw [meet] exact (reaches_orbit_iff t _).mp (ih p hp) /-- Every index 9a+8 merges in exactly five edges from the smaller index 8a+7. -/ theorem index_eight_family (a : Nat) : 8*a+7 < 9*a+8 ∧ orbit 5 (576*(8*a+7)+569) = 576*(9*a+8)+569 := by obtain ⟨_,_,h⟩ := five_step_predecessor (64*a+63) rw [show 72*(64*a+63)+65 = 576*(8*a+7)+569 by omega, show 81*(64*a+63)+74 = 576*(9*a+8)+569 by omega] at h exact ⟨by omega,h⟩ theorem claim_iff_remaining_indices : Claim ↔ ∀ q : Nat, 0 < q → q%9 ≠ 8 → ∃ p : Nat, p < q ∧ Merge (576*q+569) (576*p+569) := by constructor · intro all q hq _ exact claim_iff_smaller_index.mp all q hq · intro other apply claim_iff_smaller_index.mpr intro q hq by_cases h8 : q%9=8 · let a := q/9 have rep : q=9*a+8 := by dsimp [a]; omega rw [rep] obtain ⟨lt,he⟩ := index_eight_family a exact ⟨8*a+7,lt,0,5,he.symm⟩ · exact other q hq h8 #print axioms index_eight_family #print axioms claim_iff_remaining_indices /-- Witnesses (q,p,s,t) for q=1 through 32. The general smaller-index obligation stays open. -/ def mergeExamples : List (Nat × Nat × Nat × Nat) := [ (1,0,142,48), (2,0,27,28), (3,2,19,16), (4,2,157,40), (5,1,32,33), (6,2,61,47), (7,2,23,19), (8,7,0,5), (9,5,38,58), (10,9,64,38), (11,8,104,39), (12,10,59,51), (13,8,104,31), (14,6,20,37), (15,11,34,31), (16,12,89,55), (17,15,0,5), (18,5,56,44), (19,18,28,15), (20,8,35,39), (21,0,66,15), (22,0,64,0), (23,11,24,23), (24,0,16,1), (25,20,45,24), (26,23,0,5), (27,8,63,20), (28,14,59,40), (29,20,84,24), (30,12,33,55), (31,20,55,39), (32,20,49,46)] set_option maxRecDepth 12000 in theorem check_merge_examples : mergeExamples.map Prod.fst = (List.range 32).map (·+1) ∧ mergeExamples.all (fun (q,p,s,t) => decide (p < q) && (orbit s (576*q+569) == orbit t (576*p+569))) = true := by decide #print axioms claim_iff_smaller_index #print axioms check_merge_examples #print axioms doubling_table #print axioms ten_step_image #print axioms from_single_class #print axioms claim_iff_569 end CollatzRoute