Documentation

Mathlib.Data.Nat.Fib.Basic

Fibonacci Numbers #

This file defines the fibonacci series, proves results about it and introduces methods to compute it quickly.

The Fibonacci Sequence #

Summary #

Definition of the Fibonacci sequence F₀ = 0, F₁ = 1, Fₙ₊₂ = Fₙ + Fₙ₊₁.

Main Definitions #

Main Statements #

Implementation Notes #

For efficiency purposes, the sequence is defined using Stream.iterate.

Tags #

fib, fibonacci

def Nat.fib (n : ) :

Implementation of the fibonacci sequence satisfying fib 0 = 0, fib 1 = 1, fib (n + 2) = fib n + fib (n + 1).

Note: We use a stream iterator for better performance when compared to the naive recursive implementation.

Equations
Instances For
    @[simp]
    theorem Nat.fib_zero :
    fib 0 = 0
    @[simp]
    theorem Nat.fib_one :
    fib 1 = 1
    @[simp]
    theorem Nat.fib_two :
    fib 2 = 1
    theorem Nat.fib_add_two {n : } :
    fib (n + 2) = fib n + fib (n + 1)

    Shows that fib indeed satisfies the Fibonacci recurrence Fₙ₊₂ = Fₙ + Fₙ₊₁.

    theorem Nat.fib_add_one {n : } :
    n 0fib (n + 1) = fib (n - 1) + fib n
    @[simp]
    theorem Nat.fib_eq_zero {n : } :
    fib n = 0 n = 0
    @[simp]
    theorem Nat.fib_pos {n : } :
    theorem Nat.fib_lt_fib_succ {n : } (hn : 2 n) :
    fib n < fib (n + 1)

    fib (n + 2) is strictly monotone.

    theorem Nat.fib_lt_fib {m : } (hm : 2 m) {n : } :
    fib m < fib n m < n
    theorem Nat.le_fib_self {n : } (five_le_n : 5 n) :
    theorem Nat.fib_two_mul (n : ) :
    fib (2 * n) = fib n * (2 * fib (n + 1) - fib n)
    theorem Nat.fib_two_mul_add_one (n : ) :
    fib (2 * n + 1) = fib (n + 1) ^ 2 + fib n ^ 2

    Computes (Nat.fib n, Nat.fib (n + 1)) using the binary representation of n. Supports Nat.fastFib.

    Equations
    • One or more equations did not get rendered due to their size.
    Instances For
      def Nat.fastFib (n : ) :

      Computes Nat.fib n using the binary representation of n. Proved to be equal to Nat.fib in Nat.fast_fib_eq.

      Equations
      Instances For
        theorem Nat.gcd_fib_add_self (m n : ) :
        (fib m).gcd (fib (n + m)) = (fib m).gcd (fib n)
        theorem Nat.gcd_fib_add_mul_self (m n k : ) :
        (fib m).gcd (fib (n + k * m)) = (fib m).gcd (fib n)
        theorem Nat.fib_gcd (m n : ) :
        fib (m.gcd n) = (fib m).gcd (fib n)

        fib n is a strong divisibility sequence, see https://proofwiki.org/wiki/GCD_of_Fibonacci_Numbers

        theorem Nat.fib_dvd (m n : ) (h : m n) :
        fib m fib n