Lean / Reader
Lean reader

MorseBase.lean

74 lines13 declarationsno sorry
  1. import Mathlib.Data.Nat.ModEq
  2. import Mathlib.Data.Vector.Basic
  3. import Mathlib.Tactic.NormNum
  4. namespace Morse785
  5. def weight (h : ℕ) := (2*h+1)^2
  6. /-- The total weight of all nonnegative walks of t unit steps, from 0 to h. -/
  7. def walks : ℕ → ℕ → ℕ
  8. | 0, h => if h=0 then 1 else 0
  9. | t+1, 0 => walks t 1
  10. | t+1, h+1 => walks t h * weight h + walks t (h+2)
  11. def link (n : ℕ) := walks (2*n) 0
  12. def floorProduct : ℕ → ℕ
  13. | 0 => 1
  14. | h+1 => floorProduct h * weight h
  15. theorem compulsory_rises (t h : ℕ) : floorProduct h ∣ walks t h := by
  16. induction t generalizing h with
  17. | zero =>
  18. cases h <;> simp [walks,floorProduct]
  19. | succ t ih =>
  20. cases h with
  21. | zero => simp [floorProduct]
  22. | succ h =>
  23. apply dvd_add
  24. · exact Nat.mul_dvd_mul_right (ih h) _
  25. · exact dvd_trans (dvd_mul_right (floorProduct (h+1)) (weight (h+1))) (ih (h+2))
  26. abbrev modulus : ℕ := 9765625
  27. def boundary (t : ℕ) : walks t 18 ≡ 0 [MOD modulus] :=
  28. Nat.modEq_zero_iff_dvd.mpr (dvd_trans (by decide : modulus ∣ floorProduct 18)
  29. (compulsory_rises t 18))
  30. abbrev Row := Mathlib.Vector ℕ 18
  31. def lookup (v : Row) (i : ℕ) : ℕ := if h : i<18 then v.get ⟨i,h⟩ else 0
  32. def next (v : Row) : Row := Mathlib.Vector.ofFn fun i =>
  33. match i.val with
  34. | 0 => lookup v 1 % modulus
  35. | h+1 => (lookup v h * weight h + lookup v (h+2)) % modulus
  36. def row : ℕ → Row
  37. | 0 => Mathlib.Vector.ofFn (fun i => if i.val=0 then 1 else 0)
  38. | t+1 => next (row t)
  39. theorem correct (t h : ℕ) (hh : h<18) :
  40. lookup (row t) h ≡ walks t h [MOD modulus] := by
  41. induction t generalizing h with
  42. | zero => simp [row,lookup,hh,Mathlib.Vector.get_ofFn,walks,Nat.ModEq]
  43. | succ t ih =>
  44. have above (j : ℕ) (hj : j≤18) :
  45. lookup (row t) j ≡ walks t j [MOD modulus] := by
  46. by_cases hlt : j<18
  47. · exact ih j hlt
  48. · have he : j=18 := by omega
  49. subst j
  50. simpa [lookup] using (boundary t).symm
  51. cases h with
  52. | zero =>
  53. simp only [row,lookup,hh,dif_pos,Mathlib.Vector.get_ofFn,next,walks]
  54. exact (Nat.mod_modEq _ _).trans (above 1 (by decide))
  55. | succ h =>
  56. simp only [row,lookup,hh,dif_pos,Mathlib.Vector.get_ofFn,next,walks]
  57. exact (Nat.mod_modEq _ _).trans (((above h (by omega)).mul_right _).add
  58. (above (h+2) (by omega)))
  59. theorem pair_step (n : ℕ) (v u : Row) (a : row n = v) (b : next (next v) = u) :
  60. row (n+2) = u := by rw [row,row,a,b]
  61. attribute [irreducible] row walks
  62. end Morse785