Lean / Reader
Lean reader

FibonacciMeans.lean

65 lines17 declarations2 × sorry / admit / native_decideimports Mathlib.Data.Matrix.Notation Mathlib.Data.ZMod.Basic Mathlib.LinearAlgebra.Matrix.ToLin
  1. import Mathlib.Data.Vector.Basic
  2. import Mathlib.Data.Matrix.Notation
  3. import Mathlib.Data.ZMod.Basic
  4. import Mathlib.LinearAlgebra.Matrix.ToLin
  5. namespace FibonacciMeans
  6. def witness : ℕ := 10746306683562733203036660980572774916552278570437493
  7. abbrev R := ZMod witness
  8. abbrev M := Matrix (Fin 5) (Fin 5) R
  9. def step : M := !![1,4,4,0,0; 2,7,6,0,0; 4,12,9,0,0; 0,0,1,1,0; 1,0,0,0,1]
  10. def initial : Fin 5 → R := ![1,1,1,0,0]
  11. def bits : ℕ → ℕ → List Bool
  12. | 0, _ => []
  13. | t+1, n => decide (n%2=1) :: bits t (n/2)
  14. def value : List Bool → ℕ
  15. | [] => 0
  16. | b::bs => 2*value bs + if b then 1 else 0
  17. def fast {α : Type} [Monoid α] (x : α) : List Bool → α
  18. | [] => 1
  19. | b::bs => let y := fast x bs; (y*y)*(if b then x else 1)
  20. theorem fast_correct {α : Type} [Monoid α] (x : α) (bs : List Bool) :
  21. fast x bs = x^(value bs) := by
  22. induction bs with
  23. | nil => simp [value,fast]
  24. | cons b bs ih =>
  25. cases b <;> simp [fast,value,ih,two_mul,pow_add]
  26. abbrev Rows := Mathlib.Vector (Mathlib.Vector R 5) 5
  27. def pack (m : M) : Rows := Mathlib.Vector.ofFn (fun i => Mathlib.Vector.ofFn (m i))
  28. def unpack (v : Rows) : M := fun i j => (v.get i).get j
  29. theorem unpack_pack (m : M) : unpack (pack m) = m := by
  30. funext i j
  31. simp [pack,unpack, Mathlib.Vector.get_ofFn]
  32. def dense : List Bool → Rows
  33. | [] => pack 1
  34. | b::bs => let y := dense bs; let s := pack (unpack y * unpack y)
  35. if b then pack (unpack s * step) else s
  36. theorem dense_correct (bs : List Bool) : unpack (dense bs) = fast step bs := by
  37. induction bs with
  38. | nil => exact unpack_pack _
  39. | cons b bs ih => cases b <;> simp [dense,fast,unpack_pack,ih]
  40. def digits := bits 174 witness
  41. theorem witness_residue :
  42. ((step^witness).mulVec initial) 4 = 0 := by
  43. have e : value digits = witness := by native_decide
  44. have hp : fast step digits = step^witness := (fast_correct step digits).trans (congrArg (fun n : ℕ => step^n) e)
  45. calc
  46. _ = (unpack (dense digits)).mulVec initial 4 :=
  47. congrArg (fun m : M => m.mulVec initial 4) ((dense_correct digits).trans hp).symm
  48. _ = 0 := by native_decide
  49. end FibonacciMeans
  50. #print axioms FibonacciMeans.witness_residue