import Mathlib.Data.Nat.ModEqimport Mathlib.Data.Vector.Basicimport Mathlib.Tactic.NormNumnamespace Morse785def weight (h : ℕ) := (2*h+1)^2/-- The total weight of all nonnegative walks of t unit steps, from 0 to h. -/def walks : ℕ → ℕ → ℕ| 0, h => if h=0 then 1 else 0| t+1, 0 => walks t 1| t+1, h+1 => walks t h * weight h + walks t (h+2)def link (n : ℕ) := walks (2*n) 0def floorProduct : ℕ → ℕ| 0 => 1| h+1 => floorProduct h * weight htheorem compulsory_rises (t h : ℕ) : floorProduct h ∣ walks t h := byinduction t generalizing h with| zero =>cases h <;> simp [walks,floorProduct]| succ t ih =>cases h with| zero => simp [floorProduct]| succ h =>apply dvd_add· exact Nat.mul_dvd_mul_right (ih h) _· exact dvd_trans (dvd_mul_right (floorProduct (h+1)) (weight (h+1))) (ih (h+2))abbrev modulus : ℕ := 9765625def boundary (t : ℕ) : walks t 18 ≡ 0 [MOD modulus] :=Nat.modEq_zero_iff_dvd.mpr (dvd_trans (by decide : modulus ∣ floorProduct 18)(compulsory_rises t 18))abbrev Row := Mathlib.Vector ℕ 18def lookup (v : Row) (i : ℕ) : ℕ := if h : i<18 then v.get ⟨i,h⟩ else 0def next (v : Row) : Row := Mathlib.Vector.ofFn fun i =>match i.val with| 0 => lookup v 1 % modulus| h+1 => (lookup v h * weight h + lookup v (h+2)) % modulusdef row : ℕ → Row| 0 => Mathlib.Vector.ofFn (fun i => if i.val=0 then 1 else 0)| t+1 => next (row t)theorem correct (t h : ℕ) (hh : h<18) :lookup (row t) h ≡ walks t h [MOD modulus] := byinduction t generalizing h with| zero => simp [row,lookup,hh,Mathlib.Vector.get_ofFn,walks,Nat.ModEq]| succ t ih =>have above (j : ℕ) (hj : j≤18) :lookup (row t) j ≡ walks t j [MOD modulus] := byby_cases hlt : j<18· exact ih j hlt· have he : j=18 := by omegasubst jsimpa [lookup] using (boundary t).symmcases h with| zero =>simp only [row,lookup,hh,dif_pos,Mathlib.Vector.get_ofFn,next,walks]exact (Nat.mod_modEq _ _).trans (above 1 (by decide))| succ h =>simp only [row,lookup,hh,dif_pos,Mathlib.Vector.get_ofFn,next,walks]exact (Nat.mod_modEq _ _).trans (((above h (by omega)).mul_right _).add(above (h+2) (by omega)))theorem pair_step (n : ℕ) (v u : Row) (a : row n = v) (b : next (next v) = u) :row (n+2) = u := by rw [row,row,a,b]attribute [irreducible] row walksend Morse785