Function of #cas

fibonacci

The Fibonacci number Fn, with F0=0 and F1=1, and a theorem about Mathlib’s Nat.fib.

#cas (fibonacci n)
gives the Fibonacci number Fn of the natural number n and proves Nat.fib n = F.

fibonacci uses the convention F0=0, F1=1 and Fn+2=Fn+1+Fn, and the theorem is about Mathlib’s Nat.fib. The interesting part is the cost of the proof. It takes a number of steps that grows with the number of binary digits of n, not with n itself, because Mathlib’s norm_num extension uses the doubling formulas, which give F2k and F2k+1 from Fk and Fk+1.

Details and Options

  • n is a natural number, a literal or a closed integer expression.
  • The theorem is Nat.fib n = F. norm_num proves it with Mathlib’s extension for Nat.fib (Mathlib.Tactic.NormNum.NatFib), which uses the doubling formulas of Nat.fastFib, so the number of proof steps grows with the number of binary digits of n.
  • The value has at most 10000 digits, the most that a call computes.
  • The call is evaluated when it is the whole input of #cas.
  • There are no options.

Examples (7)#

Basic Examples (1)

The twentieth Fibonacci number, with the theorem that the kernel checked:

In[1]:=
#cas (fibonacci 20)
Out[1]=

fibonacci

6765
Domain
ℕ
Input (natural numbers)
20

Checked by Lean’s kernel: fibonacci

Nat.fib 20 = 6765
The certificate: 1 checked step
  1. fibonacci

    Nat.fib 20 = 6765

    Checked by Lean’s kernel

Scope (1)

F100, with 21 digits:

In[2]:=
#cas (fibonacci 100)
Out[2]=

fibonacci

354224848179261915075
Domain
ℕ
Input (natural numbers)
100

Checked by Lean’s kernel: fibonacci

Nat.fib 100 = 354224848179261915075
The certificate: 1 checked step
  1. fibonacci

    Nat.fib 100 = 354224848179261915075

    Checked by Lean’s kernel

Applications (1)

In a proof, cas proves the equation, here without any axioms:

In[3]:=
theorem fib_twenty : Nat.fib 20 = 6765 := by
  cas

#print axioms fib_twenty
Out[3]=

fib_twenty depends on no axioms.

Properties and Relations (2)

rsolve solves the recurrence with the same initial values, and the closed form is Binet’s formula:

In[4]:=
#cas [rsolve a n, a 0 = 0, a 1 = 1] (a (n + 2) = a (n + 1) + a n)
Out[4]=

the sequence with these initial values

a(n)=(1+52)n−(1−52)n5
a n = (((1 + sqrt(5))/2)^n - ((1 - sqrt(5))/2)^n)/sqrt(5)
Domain
a : ℕ → ℝ
Input (recurrence with initial values)
a (n + 2) = a (n + 1) + a n, a 0 = 0, a 1 = 1

Checked by Lean’s kernel: A sequence has the recurrence and the initial values exactly when it is the closed form

∀ (a : ℕ → ℝ),
  (∀ (n : ℕ), a (n + 2) = a (n + 1) + a n) ∧ a 0 = 0 ∧ a 1 = 1 ↔
    ∀ (n : ℕ), a n = (((1 + √5) / 2) ^ n - ((1 - √5) / 2) ^ n) / √5
The certificate: 1 checked step
  1. A sequence has the recurrence and the initial values exactly when it is the closed form

    ∀ (a : ℕ → ℝ),
      (∀ (n : ℕ), a (n + 2) = a (n + 1) + a n) ∧ a 0 = 0 ∧ a 1 = 1 ↔
        ∀ (n : ℕ), a n = (((1 + √5) / 2) ^ n - ((1 - √5) / 2) ^ n) / √5

    Checked by Lean’s kernel


The powers of the matrix (1110) hold consecutive Fibonacci numbers. matrixPower gives the tenth power, with F11=89, F10=55 and F9=34:

In[5]:=
#cas [matrixPower 10] !![1, 1; 1, 0]
Out[5]=

power

(89555534)
!![89, 55; 55, 34]
Domain
ℚ^(2×2)
Input (matrix)
!![1, 1; 1, 0]

Checked by Lean’s kernel: Decided by kernel evaluation

!![1, 1; 1, 0] ^ 10 = !![89, 55; 55, 34]
The certificate: 1 checked step
  1. Decided by kernel evaluation

    !![1, 1; 1, 0] ^ 10 = !![89, 55; 55, 34]

    Checked by Lean’s kernel

Possible Issues (2)

Mathlib’s Nat.fib is defined on natural numbers, and a negative argument is refused:

In[6]:=
#cas (fibonacci (-5))
Out[6]=

Error

fibonacci takes a natural number as its argument; `-5` is negative

The value has at most 10000 digits. F47851 has 10000 and F47852 has 10001:

In[7]:=
#cas (fibonacci 47852)
Out[7]=

Error

fibonacci: the value has more than 10000 digits, the most that a call computes

See Also

rsolve · matrixPower · binomial · factorial