import Bridges import Checkpoint namespace CollatzRoute open Move /-- A usable endpoint for induction restricted to checkpoints must itself be one. -/ def atCheckpoint (x : Line) : Bool := x.slope % 9 == 0 && x.base % 9 == 2 theorem checkpoint_value {x : Line} (h : atCheckpoint x = true) (q : Nat) : x.value q % 9 = 2 := by simp [atCheckpoint] at h simp [Line.value, Nat.add_mod, Nat.mul_mod, h.1, h.2] def checkpointCertificate (x : Line) (word : List Move) : Bool := certifies x word && (walk? word x).any atCheckpoint theorem smaller_checkpoint {x : Line} {word : List Move} (h : checkpointCertificate x word = true) (q : Nat) : ∃ m, m % 9 = 2 ∧ m < x.value q ∧ (ReachesOne (x.value q) ↔ ReachesOne m) := by cases hw : walk? word x with | none => simp [checkpointCertificate, hw] at h | some y => simp [checkpointCertificate, hw] at h exact ⟨y.value q, checkpoint_value h.2 q, (endpoint_smaller hw h.1 q).2, walk_sound hw q⟩ def checkpointRemaining (words : List (List Move)) : List Nat := (((List.range 64).filter fun i => !(words.any (checkpointCertificate ⟨576,9*i+11⟩))).map fun i => (9*i+11) % 576) def SmallerCheckpoint (n : Nat) : Prop := ∃ m, m % 9 = 2 ∧ m < n ∧ (ReachesOne n ↔ ReachesOne m) theorem by_checkpoint_induction (rule : ∀ n : Nat, 2 < n → n%9 = 2 → (∀ m : Nat, m < n → m%9 = 2 → ReachesOne m) → ReachesOne n) : Claim := by apply claim_iff_two_mod_nine.mpr intro n hr induction n using Nat.strongRecOn with | ind n ih => by_cases small : n = 2 · subst n; exact ⟨1, by decide⟩ · exact rule n (by omega) hr ih theorem checkpoint_partition (words : List (List Move)) (n : Nat) (hn : 2 < n) (hr : n%9 = 2) : n%576 ∈ checkpointRemaining words ∨ SmallerCheckpoint n := by let i := ((n-11)/9)%64 let q := ((n-11)/9)/64 let x : Line := ⟨576, 9*i+11⟩ have hi : i < 64 := Nat.mod_lt _ (by decide) have repr : x.value q = n := by dsimp [Line.value, x, i, q]; omega cases hc : words.any (checkpointCertificate x) with | true => obtain ⟨w, _, hw⟩ := List.any_eq_true.mp hc exact Or.inr (repr ▸ smaller_checkpoint hw q) | false => left refine List.mem_map.mpr ⟨i, List.mem_filter.mpr ⟨List.mem_range.mpr hi, ?_⟩, ?_⟩ · change Bool.not (words.any (checkpointCertificate x)) = true simp [hc] · dsimp [Line.value, x] at repr; omega theorem claim_iff_checkpoint_remaining (words : List (List Move)) : Claim ↔ ∀ n : Nat, n%576 ∈ checkpointRemaining words → ReachesOne n := by constructor · intro goal n mem obtain ⟨i, _, eq⟩ := List.mem_map.mp mem exact goal n (by omega) · intro leftover apply by_checkpoint_induction intro n hn hr ih rcases checkpoint_partition words n hn hr with mem | ⟨m, hm, hlt, eq⟩ · exact leftover n mem · exact eq.mpr (ih m hlt hm) def checkpointWords : List (List Move) := [ [H, U, H], [U, H, H, H], [H, H, H, U, H], [H, H, H, H, H, H], [H, H, U, H, H, H], [U, H, U, H, H, H], [H, H, H, H, H, U, H], [H, H, U, H, H, U, H], [H, H, U, H, U, H, H, H], [U, H, H, U, H, H, U, H], [U, H, U, H, U, H, H, H], [H, H, U, H, U, H, H, U, H], [U, H, H, U, H, U, H, H, H]] def checkpointResidues : List Nat := [11, 47, 92, 137, 155, 191, 209, 263, 272, 299, 335, 380, 425, 443, 479, 497, 551, 560, 569] set_option maxRecDepth 10000 in set_option maxHeartbeats 2500000 in theorem checkpoint_table : checkpointRemaining checkpointWords = checkpointResidues := by decide theorem claim_iff_19_checkpoints : Claim ↔ ∀ n : Nat, n%576 ∈ checkpointResidues → ReachesOne n := by simpa only [checkpoint_table] using claim_iff_checkpoint_remaining checkpointWords theorem checkpoint_residue_count : checkpointResidues.length = 19 := by decide private theorem from_refined_class (n modulus base : Nat) (word : List Move) (hc : checkpointCertificate ⟨modulus, base⟩ word = true) (hr : n%modulus=base) : SmallerCheckpoint n := by have hrepr : (Line.mk modulus base).value (n/modulus) = n := by have eq := Nat.mod_add_div n modulus dsimp [Line.value]; omega exact hrepr ▸ smaller_checkpoint hc (n/modulus) /-- Four coarse classes split into two or three certified subclasses each. -/ theorem four_refined {n : Nat} (h : n%576 = 92 ∨ n%576 = 272 ∨ n%576 = 497 ∨ n%576 = 560) : SmallerCheckpoint n := by rcases h with h|h|h|h · have hsub : n%1152=92 ∨ n%1152=668 := by omega rcases hsub with hr|hr · exact from_refined_class n 1152 92 [H, H, U, H, U, H, U, H, H, H] (by decide) hr · exact from_refined_class n 1152 668 [H, H, U, H, U, H, U, H, H, U, H] (by decide) hr · have hsub : n%1152=272 ∨ n%1152=848 := by omega rcases hsub with hr|hr · exact from_refined_class n 1152 272 [H, H, H, H, U, H, H, U, H] (by decide) hr · exact from_refined_class n 1152 848 [H, H, H, H, U, H, H, H] (by decide) hr · have hsub : n%1152=497 ∨ n%1152=1073 := by omega rcases hsub with hr|hr · exact from_refined_class n 1152 497 [U, H, H, U, H, H, H, H, P, D, D] (by decide) hr · exact from_refined_class n 1152 1073 [U, H, H, U, H, H, H, H, U, H] (by decide) hr · have hsub : n%1728=560 ∨ n%1728=1136 ∨ n%1728=1712 := by omega rcases hsub with hr|hr|hr · exact from_refined_class n 1728 560 [H, H, H, P, D, D] (by decide) hr · exact from_refined_class n 1728 1136 [H, H, H, P] (by decide) hr · exact from_refined_class n 1728 1712 [H, H, H, P, D, D, D, D] (by decide) hr def finalResidues : List Nat := checkpointResidues.filter fun r => !(r == 92 || r == 272 || r == 497 || r == 560) theorem final_fifteen : finalResidues = [11,47,137,155,191,209,263,299,335,380,425,443,479,551,569] := by decide theorem claim_iff_15_checkpoints : Claim ↔ ∀ n : Nat, n%576 ∈ finalResidues → ReachesOne n := by constructor · intro goal n mem by_cases hn : n=0 · subst n; simp [final_fifteen] at mem · exact goal n (by omega) · intro leftover apply by_checkpoint_induction intro n hn hr ih rcases checkpoint_partition checkpointWords n hn hr with mem | small · rw [checkpoint_table] at mem by_cases extra : n%576 = 92 ∨ n%576 = 272 ∨ n%576 = 497 ∨ n%576 = 560 · obtain ⟨m, hm, hlt, eq⟩ := four_refined extra exact eq.mpr (ih m hlt hm) · apply leftover apply List.mem_filter.mpr exact ⟨mem, by simpa [and_assoc] using extra⟩ · obtain ⟨m, hm, hlt, eq⟩ := small exact eq.mpr (ih m hlt hm) /-- First hits for the fifteen least representatives only. -/ def leastExamples : List (Nat × Nat) := [ (11,14), (47,104), (137,90), (155,85), (191,44), (209,39), (263,78), (299,117), (335,68), (380,107), (425,58), (443,53), (479,53), (551,43), (569,56)] theorem least_examples_match : leastExamples.map Prod.fst = finalResidues := by decide set_option maxRecDepth 12000 in set_option maxHeartbeats 4000000 in theorem least_examples_first_hits : leastExamples.all (fun (n,t) => orbit t n == 1 && (List.range t).all fun s => orbit s n != 1) = true := by decide #print axioms least_examples_match #print axioms least_examples_first_hits #print axioms four_refined #print axioms final_fifteen #print axioms claim_iff_15_checkpoints #print axioms smaller_checkpoint #print axioms checkpoint_table #print axioms claim_iff_19_checkpoints #print axioms checkpoint_residue_count end CollatzRoute