import Std /-! # A 24-state binary Turing machine for odd perfect numbers Checked with Lean 4.19.0. Run `lean OddPerfect24.lean`. The main theorem is `OddPerfect24.halts_zero_tape_iff`. Its tape is a function from the integers to Booleans, initially everywhere false, with the head at zero. The theorem `OddPerfect24.table_code` identifies the table in bbchallenge notation. For candidate n, the machine sets aside n units, charges one for divisor 1, and tries divisors m from 2 through n. The first unit of the final charge fails exactly when the proper divisors sum to n. Every other failure restarts the machine at n+2. The first candidate is 5. The proof first derives the tape transformations from individual instructions, then proves the divisor accounting and both directions for the unbounded search. The final section relates the finite tape encoding to the integer-indexed tape, step for step. -/ namespace OddPerfect24 inductive State where | A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X deriving DecidableEq, Repr inductive Dir where | L | R deriving DecidableEq, Repr structure Action where write : Bool move : Dir next : State deriving DecidableEq, Repr /-- One row per state, in order from A to X. Each instruction gives the written symbol, the head movement and the new state. V reading 0 halts. -/ def instruction : State → Bool → Option Action | .A, false => some ⟨true, .R, .O⟩ | .A, true => some ⟨false, .L, .T⟩ | .B, false => some ⟨false, .R, .I⟩ | .B, true => some ⟨false, .R, .C⟩ | .C, false => some ⟨false, .R, .D⟩ | .C, true => some ⟨true, .R, .C⟩ | .D, false => some ⟨true, .R, .E⟩ | .D, true => some ⟨true, .R, .D⟩ | .E, false => some ⟨false, .L, .G⟩ | .E, true => some ⟨false, .L, .F⟩ | .F, false => some ⟨false, .L, .H⟩ | .F, true => some ⟨true, .L, .F⟩ | .G, false => some ⟨false, .R, .E⟩ | .G, true => some ⟨true, .L, .G⟩ | .H, false => some ⟨true, .R, .B⟩ | .H, true => some ⟨true, .L, .H⟩ | .I, false => some ⟨false, .L, .K⟩ | .I, true => some ⟨true, .R, .J⟩ | .J, false => some ⟨true, .L, .S⟩ | .J, true => some ⟨true, .R, .J⟩ | .K, false => some ⟨false, .L, .L⟩ | .K, true => some ⟨true, .L, .K⟩ | .L, false => some ⟨false, .L, .M⟩ | .L, true => some ⟨true, .L, .L⟩ | .M, false => some ⟨true, .R, .N⟩ | .M, true => some ⟨true, .L, .M⟩ | .N, false => some ⟨false, .R, .U⟩ | .N, true => some ⟨false, .R, .O⟩ | .O, false => some ⟨false, .R, .P⟩ | .O, true => some ⟨true, .R, .O⟩ | .P, false => some ⟨false, .R, .Q⟩ | .P, true => some ⟨true, .R, .P⟩ | .Q, false => some ⟨true, .R, .R⟩ | .Q, true => some ⟨true, .R, .Q⟩ | .R, false => some ⟨false, .L, .S⟩ | .R, true => some ⟨false, .L, .K⟩ | .S, false => some ⟨false, .L, .A⟩ | .S, true => some ⟨true, .L, .S⟩ | .T, false => some ⟨false, .R, .B⟩ | .T, true => some ⟨true, .L, .T⟩ | .U, false => some ⟨false, .R, .V⟩ | .U, true => some ⟨false, .R, .W⟩ | .V, false => none | .V, true => some ⟨true, .R, .X⟩ | .W, false => some ⟨true, .R, .X⟩ | .W, true => some ⟨true, .R, .W⟩ | .X, false => some ⟨true, .L, .K⟩ | .X, true => some ⟨true, .R, .X⟩ /-- Serialization in bbchallenge notation. The alphabetically ordered list specifies the row order and the letters used for successor states. -/ def states : List State := [.A,.B,.C,.D,.E,.F,.G,.H,.I,.J,.K,.L,.M,.N,.O,.P,.Q,.R,.S,.T,.U,.V,.W,.X] private def encodeAction : Option Action → String | none => "---" | some a => String.mk [if a.write then '1' else '0', if a.move == .R then 'R' else 'L', Char.ofNat ('A'.toNat + states.idxOf a.next)] def machineCode : String := String.intercalate "_" (states.map fun q => encodeAction (instruction q false) ++ encodeAction (instruction q true)) set_option maxRecDepth 4096 in theorem table_code : machineCode = "1RO0LT_0RI0RC_0RD1RC_1RE1RD_0LG0LF_0LH1LF_0RE1LG_1RB1LH_0LK1RJ_1LS1RJ_0LL1LK_0LM1LL_1RN1LM_0RU0RO_0RP1RO_0RQ1RP_1RR1RQ_0LS0LK_0LA1LS_0RB1LT_0RV0RW_---1RX_1RX1RW_1LK1RX" := by decide /-- Two stacks describe the cells to the left and right of the head. The low bit is the nearest cell. High bits are zero, so this encodes a tape with finite support on the integer line, up to translation of the head. -/ structure Config where state : State left : Nat head : Bool right : Nat deriving DecidableEq, Repr def push (b : Bool) (t : Nat) : Nat := 2*t + b.toNat def top (t : Nat) : Bool := decide (t % 2 = 1) def pop (t : Nat) : Nat := t / 2 @[simp] theorem top_push (b : Bool) (t : Nat) : top (push b t) = b := by cases b <;> simp [top, push, Nat.mul_comm, Nat.add_mod] @[simp] theorem pop_push (b : Bool) (t : Nat) : pop (push b t) = t := by cases b <;> simp [pop, push] <;> omega def step (c : Config) : Option Config := do let a ← instruction c.state c.head match a.move with | .R => return ⟨a.next, push a.write c.left, top c.right, pop c.right⟩ | .L => return ⟨a.next, pop c.left, top c.left, push a.write c.right⟩ def blank : Config := ⟨.A, 0, false, 0⟩ /-- `none` means the machine has halted. It is absorbing. -/ def run : Nat → Option Config → Option Config | 0, c => c | n+1, c => run n (c.bind step) def Halts (c : Config) : Prop := ∃ n, run n (some c) = none def Reach (c e : Config) : Prop := ∃ n, run n (some c) = some e def Later (c e : Config) : Prop := ∃ n, 0 < n ∧ run n (some c) = some e private theorem run_add (n k : Nat) (c : Option Config) : run (n+k) c = run k (run n c) := by induction n generalizing c with | zero => simp [run] | succ n ih => simpa only [Nat.succ_add, run] using ih (c.bind step) @[simp] private theorem run_none (n : Nat) : run n none = none := by induction n with | zero => rfl | succ n ih => exact ih private theorem reflR (c : Config) : Reach c c := ⟨0, rfl⟩ private theorem transR {c d e : Config} (h : Reach c d) (g : Reach d e) : Reach c e := by obtain ⟨n, hn⟩ := h obtain ⟨k, hk⟩ := g exact ⟨n+k, by rw [run_add, hn, hk]⟩ private theorem prefixLater {c d e : Config} (h : Reach c d) (g : Later d e) : Later c e := by obtain ⟨n, hn⟩ := h obtain ⟨k, pos, hk⟩ := g exact ⟨n+k, by omega, by rw [run_add, hn, hk]⟩ private theorem prefixHalts {c d : Config} (h : Reach c d) (g : Halts d) : Halts c := by obtain ⟨n, hn⟩ := h obtain ⟨k, hk⟩ := g exact ⟨n+k, by rw [run_add, hn, hk]⟩ private theorem later_of_ne {c d : Config} (h : Reach c d) (hne : c ≠ d) : Later c d := by obtain ⟨n, hn⟩ := h cases n with | zero => exact False.elim (hne (Option.some.inj hn)) | succ n => exact ⟨n+1, by omega, hn⟩ /-- Scan a stack from the indicated direction. The first stack is behind the head; the second includes the scanned cell and the cells ahead. -/ private def face (q : State) : Dir → Nat → Nat → Config | .R, u, v => ⟨q, u, top v, pop v⟩ | .L, u, v => ⟨q, pop v, top v, u⟩ private def flip : Dir → Dir | .L => .R | .R => .L private theorem straight (q p : State) (d : Dir) (b w : Bool) (u v : Nat) (h : instruction q b = some ⟨w,d,p⟩) : Reach (face q d u (push b v)) (face p d (push w u) v) := by refine ⟨1, ?_⟩ cases d <;> simp [run, step, face, h] private theorem turn (q p : State) (d : Dir) (b w : Bool) (u v : Nat) (h : instruction q b = some ⟨w,flip d,p⟩) : Reach (face q d u (push b v)) (face p (flip d) (push w v) u) := by refine ⟨1, ?_⟩ cases d <;> simp_all [run, step, face, flip] private theorem stop (u v : Nat) : Halts (face .V .R u (push false v)) := by exact ⟨1, by simp [run, step, face, instruction]⟩ private def ones : Nat → Nat → Nat | 0, t => t | k+1, t => push true (ones k t) private theorem ones_join (n m t : Nat) : ones n (ones m t) = ones (n+m) t := by induction n with | zero => simp [ones] | succ n ih => simp [ones, Nat.succ_add, ih] private theorem sweep (q : State) (d : Dir) (k u v : Nat) (h : instruction q true = some ⟨true,d,q⟩) : Reach (face q d u (ones k v)) (face q d (ones k u) v) := by induction k generalizing u with | zero => exact reflR _ | succ k ih => apply transR (straight q q d true true u (ones k v) h) have eqn : ones k (push true u) = ones (k+1) u := by exact ones_join k 1 u simpa only [eqn] using ih (push true u) private abbrev z := push false /-- At B: i processed, k unprocessed cells of the first field; h ones before and t ones after the unique zero of the divisor field. -/ private def Q (s r i k h t : Nat) : Config := face .B .R (ones i (z (ones r (z (ones s 0))))) (ones k (z (ones h (z (ones t 0))))) /-- At K, on the cell immediately left of the divisor-field hole. -/ private def K (s r a h t : Nat) : Config := face .K .L (z (ones t 0)) (ones h (z (ones a (z (ones r (z (ones s 0))))))) /-- At A, about to transfer a cell to the full divisor field of size m. -/ private def A (s r a m : Nat) : Config := face .A .L (z (ones m 0)) (ones a (z (ones r (z (ones s 0))))) private def start (n : Nat) := Q 1 (n-1) 0 (n-2) 0 1 set_option maxRecDepth 8192 in private theorem startup : Reach blank (start 5) := ⟨129, by decide⟩ -- Each proof below is a sequence of actual transitions. An instruction -- marked "sweep" abbreviates k applications of one table entry. private theorem asRun (u : Nat) : push true u = ones 1 u := rfl private theorem shiftOne (n t : Nat) : ones n (push true t) = push true (ones n t) := ones_join n 1 t private theorem emptyRun (u : Nat) : ones 0 u = u := rfl private theorem blankZero : z 0 = 0 := rfl local macro "finishTape" : tactic => `(tactic| first | exact reflR _ | apply reflR | (simp only [Q, K, A, asRun, ones_join, emptyRun, blankZero, Nat.add_zero, Nat.zero_add, Nat.add_comm, Nat.add_left_comm, Nat.add_assoc] ; exact reflR _)) -- B advances once through the n-m cells while the divisor-field hole cycles. private theorem passNext (s r i k h t : Nat) : Reach (Q s r i (k+1) h (t+1)) (Q s r (i+1) k (h+1) t) := by unfold Q apply transR (straight .B .C .R true false _ _ rfl) apply transR (sweep .C .R k _ _ rfl) apply transR (straight .C .D .R false false _ _ rfl) apply transR (sweep .D .R h _ _ rfl) apply transR (straight .D .E .R false true _ _ rfl) apply transR (turn .E .F .R true false _ _ rfl) apply transR (straight .F .F .L true true _ _ rfl) apply transR (sweep .F .L h _ _ rfl) apply transR (straight .F .H .L false false _ _ rfl) apply transR (sweep .H .L k _ _ rfl) apply transR (turn .H .B .L false true _ _ rfl) finishTape private theorem passWrap (s r i k h : Nat) : Reach (Q s r i (k+1) h 0) (Q s r (i+1) k 0 h) := by unfold Q apply transR (straight .B .C .R true false _ _ rfl) apply transR (sweep .C .R k _ _ rfl) apply transR (straight .C .D .R false false _ _ rfl) apply transR (sweep .D .R h _ _ rfl) apply transR (straight .D .E .R false true _ 0 rfl) apply transR (turn .E .G .R false false _ 0 rfl) apply transR (straight .G .G .L true true _ _ rfl) apply transR (sweep .G .L h _ _ rfl) apply transR (turn .G .E .L false false _ _ rfl) simp only [ones, shiftOne] apply transR (turn .E .F .R true false _ _ rfl) apply transR (straight .F .H .L false false _ _ rfl) apply transR (sweep .H .L k _ _ rfl) apply transR (turn .H .B .L false true _ _ rfl) finishTape private theorem foundDivisor (s r a t : Nat) : Reach (Q s r a 0 0 t) (K s r a 0 t) := by unfold Q K apply transR (straight .B .I .R false false _ _ rfl) apply transR (turn .I .K .R false false _ _ rfl) finishTape private theorem missedDivisor (s r a h t : Nat) : Reach (Q s r a 0 (h+1) t) (A s r a (h+t+2)) := by unfold Q A apply transR (straight .B .I .R false false _ _ rfl) apply transR (straight .I .J .R true true _ _ rfl) apply transR (sweep .J .R h _ _ rfl) apply transR (turn .J .S .R false true _ _ rfl) apply transR (sweep .S .L h _ _ rfl) apply transR (straight .S .S .L true true _ _ rfl) apply transR (straight .S .A .L false false _ _ rfl) finishTape -- K charges one budget unit per divisor-field cell. private theorem debitNext (s r a h t : Nat) : Reach (K s (r+1) a h (t+1)) (K (s+1) r a (h+1) t) := by unfold K apply transR (sweep .K .L h _ _ rfl) apply transR (straight .K .L .L false false _ _ rfl) apply transR (sweep .L .L a _ _ rfl) apply transR (straight .L .M .L false false _ _ rfl) apply transR (straight .M .M .L true true _ _ rfl) apply transR (sweep .M .L r _ _ rfl) apply transR (turn .M .N .L false true _ _ rfl) simp only [ones, shiftOne] apply transR (straight .N .O .R true false _ _ rfl) apply transR (sweep .O .R r _ _ rfl) apply transR (straight .O .P .R false false _ _ rfl) apply transR (sweep .P .R a _ _ rfl) apply transR (straight .P .Q .R false false _ _ rfl) apply transR (sweep .Q .R h _ _ rfl) apply transR (straight .Q .R .R false true _ _ rfl) apply transR (turn .R .K .R true false _ _ rfl) finishTape private theorem debitDone (s r a h : Nat) : Reach (K s (r+1) a h 0) (A (s+1) r a (h+1)) := by unfold K A apply transR (sweep .K .L h _ _ rfl) apply transR (straight .K .L .L false false _ _ rfl) apply transR (sweep .L .L a _ _ rfl) apply transR (straight .L .M .L false false _ _ rfl) apply transR (straight .M .M .L true true _ _ rfl) apply transR (sweep .M .L r _ _ rfl) apply transR (turn .M .N .L false true _ _ rfl) simp only [ones, shiftOne] apply transR (straight .N .O .R true false _ _ rfl) apply transR (sweep .O .R r _ _ rfl) apply transR (straight .O .P .R false false _ _ rfl) apply transR (sweep .P .R a _ _ rfl) apply transR (straight .P .Q .R false false _ _ rfl) apply transR (sweep .Q .R h _ _ rfl) apply transR (straight .Q .R .R false true _ 0 rfl) apply transR (turn .R .S .R false false _ 0 rfl) apply transR (straight .S .S .L true true _ _ rfl) apply transR (sweep .S .L h _ _ rfl) apply transR (straight .S .A .L false false _ _ rfl) finishTape private theorem advance (s r a m : Nat) : Reach (A s r (a+1) m) (Q s r 0 a 0 m) := by unfold A Q apply transR (straight .A .T .L true false _ _ rfl) apply transR (sweep .T .L a _ _ rfl) apply transR (turn .T .B .L false false _ _ rfl) finishTape -- With the budget exhausted, U and V distinguish the position of the -- failed charge. Rejection recreates the tape for trial 2 of n+2. private theorem rejectProper (s a h t : Nat) : Reach (K s 0 (a+1) h t) (Q 1 (s+1) 0 (a+h+t+2) 0 1) := by unfold K Q apply transR (sweep .K .L h _ _ rfl) apply transR (straight .K .L .L false false _ _ rfl) apply transR (straight .L .L .L true true _ _ rfl) apply transR (sweep .L .L a _ _ rfl) apply transR (straight .L .M .L false false _ _ rfl) apply transR (turn .M .N .L false true _ _ rfl) apply transR (straight .N .U .R false false _ _ rfl) simp only [ones, shiftOne] apply transR (straight .U .W .R true false _ _ rfl) apply transR (sweep .W .R a _ _ rfl) apply transR (straight .W .X .R false true _ _ rfl) apply transR (sweep .X .R h _ _ rfl) apply transR (turn .X .K .R false true _ _ rfl) apply transR (sweep .K .L h _ _ rfl) apply transR (straight .K .K .L true true _ _ rfl) apply transR (sweep .K .L a _ _ rfl) apply transR (straight .K .L .L false false _ _ rfl) apply transR (straight .L .M .L false false _ _ rfl) apply transR (straight .M .M .L true true _ _ rfl) apply transR (sweep .M .L s _ _ rfl) apply transR (turn .M .N .L false true _ 0 rfl) simp only [ones, shiftOne] apply transR (straight .N .O .R true false _ _ rfl) apply transR (sweep .O .R s _ _ rfl) apply transR (straight .O .P .R false false _ _ rfl) apply transR (straight .P .Q .R false false _ _ rfl) apply transR (straight .Q .Q .R true true _ _ rfl) apply transR (straight .Q .Q .R true true _ _ rfl) apply transR (sweep .Q .R a _ _ rfl) apply transR (sweep .Q .R h _ _ rfl) apply transR (sweep .Q .R t _ _ rfl) apply transR (straight .Q .R .R false true _ 0 rfl) apply transR (turn .R .S .R false false _ 0 rfl) apply transR (straight .S .S .L true true _ _ rfl) apply transR (sweep .S .L t _ _ rfl) apply transR (sweep .S .L h _ _ rfl) apply transR (sweep .S .L a _ _ rfl) apply transR (straight .S .S .L true true _ _ rfl) apply transR (straight .S .S .L true true _ _ rfl) apply transR (straight .S .A .L false false _ _ rfl) apply transR (turn .A .O .L false true _ _ rfl) apply transR (straight .O .P .R false false _ _ rfl) apply transR (straight .P .P .R true true _ _ rfl) apply transR (straight .P .P .R true true _ _ rfl) apply transR (sweep .P .R a _ _ rfl) apply transR (sweep .P .R h _ _ rfl) apply transR (sweep .P .R t _ _ rfl) apply transR (straight .P .P .R true true _ _ rfl) apply transR (straight .P .Q .R false false _ 0 rfl) apply transR (straight .Q .R .R false true _ 0 rfl) apply transR (turn .R .S .R false false _ 0 rfl) apply transR (straight .S .S .L true true _ _ rfl) apply transR (straight .S .A .L false false _ _ rfl) apply transR (straight .A .T .L true false _ _ rfl) apply transR (sweep .T .L t _ _ rfl) apply transR (sweep .T .L h _ _ rfl) apply transR (sweep .T .L a _ _ rfl) apply transR (straight .T .T .L true true _ _ rfl) apply transR (straight .T .T .L true true _ _ rfl) apply transR (turn .T .B .L false false _ _ rfl) finishTape private theorem rejectFinal (s h t : Nat) : Reach (K s 0 0 (h+1) t) (Q 1 (s+1) 0 (h+t+2) 0 1) := by unfold K Q apply transR (straight .K .K .L true true _ _ rfl) apply transR (sweep .K .L h _ _ rfl) apply transR (straight .K .L .L false false _ _ rfl) apply transR (straight .L .M .L false false _ _ rfl) apply transR (turn .M .N .L false true _ _ rfl) apply transR (straight .N .U .R false false _ _ rfl) apply transR (straight .U .V .R false false _ _ rfl) simp only [ones, shiftOne] apply transR (straight .V .X .R true true _ _ rfl) apply transR (sweep .X .R h _ _ rfl) apply transR (turn .X .K .R false true _ _ rfl) apply transR (sweep .K .L h _ _ rfl) apply transR (straight .K .K .L true true _ _ rfl) apply transR (straight .K .L .L false false _ _ rfl) apply transR (straight .L .M .L false false _ _ rfl) apply transR (straight .M .M .L true true _ _ rfl) apply transR (sweep .M .L s _ _ rfl) apply transR (turn .M .N .L false true _ 0 rfl) simp only [ones, shiftOne] apply transR (straight .N .O .R true false _ _ rfl) apply transR (sweep .O .R s _ _ rfl) apply transR (straight .O .P .R false false _ _ rfl) apply transR (straight .P .Q .R false false _ _ rfl) apply transR (straight .Q .Q .R true true _ _ rfl) apply transR (straight .Q .Q .R true true _ _ rfl) apply transR (sweep .Q .R h _ _ rfl) apply transR (sweep .Q .R t _ _ rfl) apply transR (straight .Q .R .R false true _ 0 rfl) apply transR (turn .R .S .R false false _ 0 rfl) apply transR (straight .S .S .L true true _ _ rfl) apply transR (sweep .S .L t _ _ rfl) apply transR (sweep .S .L h _ _ rfl) apply transR (straight .S .S .L true true _ _ rfl) apply transR (straight .S .S .L true true _ _ rfl) apply transR (straight .S .A .L false false _ _ rfl) apply transR (turn .A .O .L false true _ _ rfl) apply transR (straight .O .P .R false false _ _ rfl) apply transR (straight .P .P .R true true _ _ rfl) apply transR (straight .P .P .R true true _ _ rfl) apply transR (sweep .P .R h _ _ rfl) apply transR (sweep .P .R t _ _ rfl) apply transR (straight .P .P .R true true _ _ rfl) apply transR (straight .P .Q .R false false _ 0 rfl) apply transR (straight .Q .R .R false true _ 0 rfl) apply transR (turn .R .S .R false false _ 0 rfl) apply transR (straight .S .S .L true true _ _ rfl) apply transR (straight .S .A .L false false _ _ rfl) apply transR (straight .A .T .L true false _ _ rfl) apply transR (sweep .T .L t _ _ rfl) apply transR (sweep .T .L h _ _ rfl) apply transR (straight .T .T .L true true _ _ rfl) apply transR (straight .T .T .L true true _ _ rfl) apply transR (turn .T .B .L false false _ _ rfl) finishTape private theorem acceptFinal (s t : Nat) : Halts (K s 0 0 0 t) := by unfold K apply prefixHalts (straight .K .L .L false false _ _ rfl) apply prefixHalts (straight .L .M .L false false _ _ rfl) apply prefixHalts (turn .M .N .L false true _ _ rfl) apply prefixHalts (straight .N .U .R false false _ _ rfl) apply prefixHalts (straight .U .V .R false false _ _ rfl) exact stop _ _ /-! ## From tape macros to trial division -/ private theorem pass_all (s r i k m : Nat) (hm : 0 < m) : Reach (Q s r i k (i%m) (m-1-i%m)) (Q s r (i+k) 0 ((i+k)%m) (m-1-(i+k)%m)) := by induction k generalizing i with | zero => exact reflR _ | succ k ih => have hlt := Nat.mod_lt i hm have inc : Reach (Q s r i (k+1) (i%m) (m-1-i%m)) (Q s r (i+1) k ((i+1)%m) (m-1-(i+1)%m)) := by by_cases h : i%m + 1 < m · have e : (i+1)%m = i%m+1 := by rw [← Nat.mod_add_mod, Nat.mod_eq_of_lt h] rw [e] have gap : m-1-i%m = (m-1-(i%m+1))+1 := by omega simpa only [←gap] using passNext s r i k (i%m) (m-1-(i%m+1)) · have full : i%m + 1 = m := by omega have e : (i+1)%m = 0 := by rw [← Nat.mod_add_mod, full, Nat.mod_self] rw [e, show m-1-i%m = 0 by omega, show m-1-0 = i%m by omega] exact passWrap s r i k (i%m) have next := transR inc (ih (i+1)) simpa only [show i+1+k = i+(k+1) by omega] using next private theorem trial_divides (s r a m : Nat) (hm : 0 < m) (hd : m ∣ a+m) : Reach (Q s r 0 a 0 (m-1)) (K s r a 0 (m-1)) := by have ep : a%m = 0 := by simpa using Nat.mod_eq_zero_of_dvd hd have path := pass_all s r 0 a m hm simp only [Nat.zero_add, Nat.zero_mod, Nat.sub_zero, ep] at path exact transR path (foundDivisor s r a (m-1)) private theorem trial_misses (s r a m : Nat) (hm : 0 < m) (hd : ¬ m ∣ a+m) : Reach (Q s r 0 a 0 (m-1)) (A s r a m) := by have path := pass_all s r 0 a m hm simp only [Nat.zero_add, Nat.zero_mod, Nat.sub_zero] at path have hz : a%m ≠ 0 := by intro he exact hd (Nat.dvd_iff_mod_eq_zero.mpr (by simpa using he)) have hlt := Nat.mod_lt a hm let h := a%m-1 have ep : a%m = h+1 := by dsimp [h]; omega let t := m-1-a%m have total : h+t+2 = m := by dsimp [h,t]; omega have dispatch := missedDivisor s r a h t rw [←ep, total] at dispatch exact transR path dispatch private theorem pay (s u a h t : Nat) : Reach (K s (u+(t+1)) a h t) (A (s+(t+1)) u a (h+t+1)) := by induction t generalizing s h with | zero => simpa using debitDone s u a h | succ t ih => have path := transR (debitNext s (u+(t+1)) a h t) (ih (s+1) (h+1)) simpa only [show u+(t+1)+1 = u+(t+1+1) by omega, show s+1+(t+1) = s+(t+1+1) by omega, show h+1+t+1 = h+(t+1)+1 by omega] using path private theorem drain (s r a h t : Nat) : Reach (K s r a h (t+r)) (K (s+r) 0 a (h+r) t) := by induction r generalizing s h with | zero => exact reflR _ | succ r ih => have path := transR (debitNext s r a h (t+r)) (ih (s+1) (h+1)) simpa only [show s+1+r = s+(r+1) by omega, show h+1+r = h+(r+1) by omega] using path /-- Sum of the positive divisors of n strictly less than k. -/ def sumBelow (n : Nat) : Nat → Nat | 0 => 0 | k+1 => sumBelow n k + if 0 < k ∧ k ∣ n then k else 0 private theorem sum_mono (n : Nat) {k l : Nat} (h : k ≤ l) : sumBelow n k ≤ sumBelow n l := by obtain ⟨v, rfl⟩ := Nat.exists_eq_add_of_le h clear h induction v with | zero => exact Nat.le_refl _ | succ v ih => exact Nat.le_trans ih (Nat.le_add_right _ _) private theorem sum_two (n : Nat) : sumBelow n 2 = 1 := by simp [sumBelow, Nat.one_dvd] private def Result (n : Nat) (c : Config) : Prop := (sumBelow n n = n ∧ Halts c) ∨ (sumBelow n n ≠ n ∧ Later c (start (n+2))) private theorem beforeResult {n : Nat} {c d : Config} (h : Reach c d) (g : Result n d) : Result n c := by rcases g with ⟨eq, stop⟩ | ⟨ne, cont⟩ · exact Or.inl ⟨eq, prefixHalts h stop⟩ · exact Or.inr ⟨ne, prefixLater h cont⟩ -- Amount exhaustion at a proper divisor and at the final divisor lead to -- different states. In either rejection case a positive number of steps -- elapses (K and B are distinct states). private theorem proper_restart (n a h t : Nat) (hn : a+h+t+2 = n) : Later (K n 0 (a+1) h t) (start (n+2)) := by apply later_of_ne · simpa [start, hn] using rejectProper n a h t · intro eq have := congrArg Config.state eq contradiction private theorem final_restart (n h t : Nat) (hn : h+t+2 = n) : Later (K n 0 0 (h+1) t) (start (n+2)) := by apply later_of_ne · simpa [start, hn] using rejectFinal n h t · intro eq have := congrArg Config.state eq contradiction private theorem trial_result (n a m s r : Nat) (hn : a+m = n) (hm : 2 ≤ m) (budget : s+r = n) (spent : s = sumBelow n m) : Result n (Q s r 0 a 0 (m-1)) := by induction a generalizing m s r with | zero => have mn : m = n := by omega subst m have one := sum_mono n hm rw [sum_two] at one have short : r < n := by omega apply beforeResult (trial_divides s r 0 n (by omega) (by simp)) have chargePath := drain s r 0 0 (n-1-r) rw [show n-1-r+r = n-1 by omega, budget, Nat.zero_add] at chargePath apply beforeResult chargePath cases r with | zero => exact Or.inl ⟨by omega, acceptFinal n (n-1-0)⟩ | succ r => exact Or.inr ⟨by omega, final_restart n r (n-1-(r+1)) (by omega)⟩ | succ a ih => by_cases hd : m ∣ n · have add : sumBelow n (m+1) = s+m := by simp [sumBelow, ←spent, show 0 < m by omega, hd] apply beforeResult (trial_divides s r (a+1) m (by omega) (hn ▸ hd)) by_cases fits : m ≤ r · have chargePath := pay s (r-m) (a+1) 0 (m-1) rw [show m-1+1 = m by omega, show r-m+m = r by omega, Nat.zero_add] at chargePath rw [show m-1+1 = m by omega] at chargePath apply beforeResult chargePath apply beforeResult (advance (s+m) (r-m) a m) exact ih (m+1) (s+m) (r-m) (by omega) (by omega) (by omega) add.symm · have chargePath := drain s r (a+1) 0 (m-1-r) rw [show m-1-r+r = m-1 by omega, budget, Nat.zero_add] at chargePath apply beforeResult chargePath have upper := sum_mono n (show m+1 ≤ n by omega) exact Or.inr ⟨by omega, proper_restart n a r (m-1-r) (by omega)⟩ · apply beforeResult (trial_misses s r (a+1) m (by omega) (hn ▸ hd)) apply beforeResult (advance s r a m) apply ih (m+1) s r (by omega) (by omega) budget simp [sumBelow, hd, ←spent] private theorem candidate (n : Nat) (hn : 5 ≤ n) : Result n (start n) := by apply trial_result n (n-2) 2 1 (n-1) (by omega) (by decide) (by omega) exact (sum_two n).symm /-! ## The unbounded search -/ private theorem time_left {c d : Config} {k t : Nat} (arrives : run t (some c) = some d) (halts : run k (some c) = none) : t ≤ k ∧ run (k-t) (some d) = none := by by_cases le : t ≤ k · refine ⟨le, ?_⟩ have h := run_add t (k-t) (some c) rw [show t+(k-t) = k by omega, halts, arrives] at h exact h.symm · have h := run_add k (t-k) (some c) rw [show k+(t-k) = t by omega, arrives, halts, run_none] at h contradiction private theorem witness_of_run (k n : Nat) (hn : 5 ≤ n) (odd : n%2 = 1) (halts : run k (some (start n)) = none) : ∃ m, m%2 = 1 ∧ sumBelow m m = m := by induction k using Nat.strongRecOn generalizing n with | ind k ih => rcases candidate n hn with ⟨eq, _⟩ | ⟨_, t, pos, reaches⟩ · exact ⟨n, odd, eq⟩ · obtain ⟨le, rem⟩ := time_left reaches halts exact ih (k-t) (by omega) (n+2) (by omega) (by omega) rem private theorem halts_of_witness (k n : Nat) (hn : 5 ≤ n) (perfect : sumBelow (n+2*k) (n+2*k) = n+2*k) : Halts (start n) := by induction k generalizing n with | zero => simp only [Nat.mul_zero, Nat.add_zero] at perfect rcases candidate n hn with ⟨_, halt⟩ | ⟨ne, _⟩ · exact halt · exact False.elim (ne perfect) | succ k ih => rcases candidate n hn with ⟨_, halt⟩ | ⟨_, t, _, reaches⟩ · exact halt · apply prefixHalts ⟨t, reaches⟩ apply ih (n+2) (by omega) simpa only [show n + 2*(k+1) = n+2 + 2*k by omega] using perfect /-- The usual list of positive proper divisors, in increasing order. -/ def properDivisors (n : Nat) : List Nat := (List.range n).filter (fun d => decide (0 < d ∧ d ∣ n)) /-- A perfect number is positive and equals the sum of its proper divisors. -/ def Perfect (n : Nat) : Prop := 0 < n ∧ (properDivisors n).sum = n private theorem sum_append_nat (xs ys : List Nat) : (xs++ys).sum = xs.sum + ys.sum := by induction xs with | nil => simp | cons x xs ih => simp [ih, Nat.add_assoc] private theorem sum_filter (n k : Nat) : ((List.range k).filter (fun d => decide (0 < d ∧ d ∣ n))).sum = sumBelow n k := by induction k with | zero => rfl | succ k ih => rw [List.range_succ, List.filter_append, sum_append_nat, ih, sumBelow] by_cases h : 0 < k ∧ k ∣ n <;> simp [h] /-- The exact 24-state machine halts on the all-zero tape iff there is a positive odd integer equal to the sum of its positive proper divisors. -/ theorem halts_blank_iff : Halts blank ↔ ∃ n : Nat, n%2 = 1 ∧ Perfect n := by constructor · intro ⟨k, halt⟩ obtain ⟨t, ht⟩ := startup obtain ⟨_, rem⟩ := time_left ht halt obtain ⟨n, odd, eq⟩ := witness_of_run (k-t) 5 (by decide) (by decide) rem exact ⟨n, odd, by omega, by simpa only [properDivisors, sum_filter] using eq⟩ · rintro ⟨n, odd, _, perfect⟩ simp only [properDivisors, sum_filter] at perfect have hn : 5 ≤ n := by by_cases h : 5 ≤ n · exact h · have small : n = 1 ∨ n = 3 := by omega rcases small with e | e · subst n; exact False.elim ((by decide : sumBelow 1 1 ≠ 1) perfect) · subst n; exact False.elim ((by decide : sumBelow 3 3 ≠ 3) perfect) obtain ⟨k, form⟩ : ∃ k, n = 5+2*k := ⟨(n-5)/2, by omega⟩ rw [form] at perfect exact prefixHalts startup (halts_of_witness k 5 (by decide) perfect) /-! ## The ordinary tape indexed by the integers Here the head has an absolute integer position. A transition writes exactly one cell and changes the position by one. The relation below proves that the two stacks used for the finite calculations represent this machine. -/ structure TapeConfig where state : State pos : Int tape : Int → Bool def zeroTape : TapeConfig := ⟨.A, 0, fun _ => false⟩ private def writeAt (f : Int → Bool) (p : Int) (w : Bool) (i : Int) : Bool := if i = p then w else f i def tapeStep (c : TapeConfig) : Option TapeConfig := do let a ← instruction c.state (c.tape c.pos) let pos := match a.move with | .L => c.pos-1 | .R => c.pos+1 return ⟨a.next, pos, writeAt c.tape c.pos a.write⟩ def tapeRun : Nat → Option TapeConfig → Option TapeConfig | 0, c => c | n+1, c => tapeRun n (c.bind tapeStep) private def bitAt : Nat → Nat → Bool | 0, t => top t | n+1, t => bitAt n (pop t) private structure Represents (c : Config) (e : TapeConfig) : Prop where state : e.state = c.state head : e.tape e.pos = c.head left : ∀ n : Nat, e.tape (e.pos - (↑n+1)) = bitAt n c.left right : ∀ n : Nat, e.tape (e.pos + (↑n+1)) = bitAt n c.right private def Aligned : Option Config → Option TapeConfig → Prop | some c, some e => Represents c e | none, none => True | _, _ => False private theorem step_represents {c : Config} {e : TapeConfig} (h : Represents c e) : Aligned (step c) (tapeStep e) := by simp only [step, tapeStep, h.state, h.head] cases hi : instruction c.state c.head with | none => trivial | some a => obtain ⟨w, d, q⟩ := a cases d with | L => change Represents ⟨q, pop c.left, top c.left, push w c.right⟩ ⟨q, e.pos-1, writeAt e.tape e.pos w⟩ constructor · rfl · change writeAt _ _ _ (e.pos-1) = _ simpa [writeAt, bitAt, show e.pos-1 ≠ e.pos by omega] using h.left 0 · intro n change writeAt _ _ _ ((e.pos-1)-(↑n+1)) = bitAt n (pop c.left) rw [show (e.pos-1)-(↑n+1) = e.pos-((↑(n+1) : Int)+1) by omega] simpa only [writeAt, if_neg (show e.pos-((↑(n+1) : Int)+1) ≠ e.pos by omega), bitAt] using h.left (n+1) · intro n change writeAt _ _ _ ((e.pos-1)+(↑n+1)) = bitAt n (push w c.right) cases n with | zero => simp [writeAt, bitAt] | succ k => rw [show (e.pos-1)+((↑(k+1) : Int)+1) = e.pos+(↑k+1) by omega] simpa [writeAt, bitAt, show e.pos+(↑k+1) ≠ e.pos by omega] using h.right k | R => change Represents ⟨q, push w c.left, top c.right, pop c.right⟩ ⟨q, e.pos+1, writeAt e.tape e.pos w⟩ constructor · rfl · change writeAt _ _ _ (e.pos+1) = _ simpa [writeAt, bitAt, show e.pos+1 ≠ e.pos by omega] using h.right 0 · intro n change writeAt _ _ _ ((e.pos+1)-(↑n+1)) = bitAt n (push w c.left) cases n with | zero => simp [writeAt, bitAt] | succ k => rw [show (e.pos+1)-((↑(k+1) : Int)+1) = e.pos-(↑k+1) by omega] simpa [writeAt, bitAt, show e.pos-(↑k+1) ≠ e.pos by omega] using h.left k · intro n change writeAt _ _ _ ((e.pos+1)+(↑n+1)) = bitAt n (pop c.right) rw [show (e.pos+1)+(↑n+1) = e.pos+((↑(n+1) : Int)+1) by omega] simpa only [writeAt, if_neg (show e.pos+((↑(n+1) : Int)+1) ≠ e.pos by omega), bitAt] using h.right (n+1) private theorem run_represents (n : Nat) (c : Option Config) (e : Option TapeConfig) (h : Aligned c e) : Aligned (run n c) (tapeRun n e) := by induction n generalizing c e with | zero => exact h | succ n ih => apply ih cases c <;> cases e · trivial · contradiction · contradiction · exact step_represents h private theorem initial_represents : Represents blank zeroTape := by have hz (n : Nat) : bitAt n 0 = false := by induction n with | zero => rfl | succ n ih => exact ih exact ⟨rfl, rfl, fun n => (hz n).symm, fun n => (hz n).symm⟩ private theorem halt_agrees (n : Nat) : tapeRun n (some zeroTape) = none ↔ run n (some blank) = none := by have h := run_represents n (some blank) (some zeroTape) initial_represents cases hc : run n (some blank) <;> cases he : tapeRun n (some zeroTape) <;> simp_all [Aligned] /-- Main theorem for a literal tape ℤ → {0,1}, started at position zero. Reaching none means encountering the unique undefined instruction. -/ theorem halts_zero_tape_iff : (∃ t : Nat, tapeRun t (some zeroTape) = none) ↔ ∃ n : Nat, n%2 = 1 ∧ Perfect n := by rw [← halts_blank_iff] exact ⟨fun ⟨t, h⟩ => ⟨t, (halt_agrees t).mp h⟩, fun ⟨t, h⟩ => ⟨t, (halt_agrees t).mpr h⟩⟩ #print axioms halts_zero_tape_iff end OddPerfect24