Lean / Reader
Lean reader

LazyLucasCore.lean

30 lines5 declarationsno sorryimports Mathlib.Tactic
  1. import Mathlib.Tactic
  2. namespace LazyLucas
  3. def lucas : ℕ → ℕ
  4. | 0 => 2
  5. | 1 => 1
  6. | n+2 => lucas n + lucas (n+1)
  7. def cap : ℕ → ℕ
  8. | 0 => 0
  9. | n+1 => cap n + lucas n
  10. /-- The number of chosen digits in a prefix of width k. At width two the
  11. weights are 2 and 1, in that order from the low-order bit upward. -/
  12. def bounded : ℕ → ℕ → ℕ
  13. | 0, _ => 0
  14. | 1, n => if n=2 then 1 else 0
  15. | 2, n => if n=0 then 0 else if n=3 then 2 else 1
  16. | k+3, n => if n ≤ cap (k+2) then bounded (k+2) n else 1+bounded (k+2) (n-lucas (k+2))
  17. theorem bounded_two (n : ℕ) : bounded 2 n = if n=0 then 0 else if n=3 then 2 else 1 := rfl
  18. theorem bounded_step {k n : ℕ} (hk : 2 ≤ k) : bounded (k+1) n =
  19. if n ≤ cap k then bounded k n else 1+bounded k (n-lucas k) := by
  20. obtain ⟨u,rfl⟩ := Nat.exists_eq_add_of_le hk
  21. rw [Nat.add_comm 2 u]; rfl
  22. end LazyLucas