Post · 7 min read· pp. 27–31

Fibonacci Numbers

The Fibonacci sequence is defined as follows:

F0=0,F1=1,Fn=Fn−1+Fn−2F_0 = 0, F_1 = 1, F_n = F_{n-1} + F_{n-2}

The first elements of the sequence (OEIS A000045) are:

0,1,1,2,3,5,8,13,21,34,55,89,...0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...

Properties

Fibonacci numbers possess a lot of interesting properties. Here are a few of them:

  • Cassini's identity:

Fn−1Fn+1−Fn2=(−1)nF_{n-1} F_{n+1} - F_n^2 = (-1)^n

This can be proved by induction. A one-line proof by Knuth comes from taking the determinant of the 2x2 matrix form below.

  • The "addition" rule:

Fn+k=FkFn+1+Fk−1FnF_{n+k} = F_k F_{n+1} + F_{k-1} F_n

  • Applying the previous identity to the case k=nk = n, we get:

F2n=Fn(Fn+1+Fn−1)F_{2n} = F_n (F_{n+1} + F_{n-1})

  • From this we can prove by induction that for any positive integer kk, FnkF_{nk} is multiple of FnF_n.
  • For n≥3n \ge 3, the inverse is also true: if FmF_m is multiple of FnF_n, then mm is multiple of nn.
  • GCD identity:

GCD(Fm,Fn)=FGCD(m,n)GCD(F_m, F_n) = F_{GCD(m, n)}

  • Fibonacci numbers are the worst possible inputs for Euclidean algorithm (see Lame's theorem in Euclidean algorithm)

Fibonacci Coding

We can use the sequence to encode positive integers into binary code words. According to Zeckendorf's theorem, any natural number nn can be uniquely represented as a sum of Fibonacci numbers:

N=Fk1+Fk2+…+FkrN = F_{k_1} + F_{k_2} + \ldots + F_{k_r}

such that k1≥k2+2, k2≥k3+2, …, kr≥2k_1 \ge k_2 + 2,\ k_2 \ge k_3 + 2,\ \ldots,\ k_r \ge 2 (i.e.: the representation cannot use two consecutive Fibonacci numbers).

It follows that any number can be uniquely encoded in the Fibonacci coding. And we can describe this representation with binary codes d0d1d2…ds1d_0 d_1 d_2 \dots d_s 1, where did_i is 11 if Fi+2F_{i+2} is used in the representation. The code will be appended by a 11 to indicate the end of the code word. Notice that this is the only occurrence where two consecutive 1-bits appear.

\begin{eqnarray} 1 &=& 1 &=& F_2 &=& (11)_F \\ 2 &=& 2 &=& F_3 &=& (011)_F \\ 6 &=& 5 + 1 &=& F_5 + F_2 &=& (10011)_F \\ 8 &=& 8 &=& F_6 &=& (000011)_F \\ 9 &=& 8 + 1 &=& F_6 + F_2 &=& (100011)_F \\ 19 &=& 13 + 5 + 1 &=& F_7 + F_5 + F_2 &=& (1001011)_F \end{eqnarray}

The encoding of an integer nn can be done with a simple greedy algorithm:

  1. Iterate through the Fibonacci numbers from the largest to the smallest until you find one less than or equal to nn.
  2. Suppose this number was FiF_i. Subtract FiF_i from nn and put a 11 in the i−2i-2 position of the code word (indexing from 0 from the leftmost to the rightmost bit).
  3. Repeat until there is no remainder.
  4. Add a final 11 to the codeword to indicate its end.

To decode a code word, first remove the final 11. Then, if the ii-th bit is set (indexing from 0 from the leftmost to the rightmost bit), sum Fi+2F_{i+2} to the number.

Formulas for the nthn^{\text{th}} Fibonacci number

Closed-form expression

There is a formula known as "Binet's formula", even though it was already known by Moivre:

Fn=(1+52)n−(1−52)n5F_n = \frac{\left(\frac{1 + \sqrt{5}}{2}\right)^n - \left(\frac{1 - \sqrt{5}}{2}\right)^n}{\sqrt{5}}

This formula is easy to prove by induction, but it can be deduced with the help of the concept of generating functions or by solving a functional equation.

You can immediately notice that the second term's absolute value is always less than 11, and it also decreases very rapidly (exponentially). Hence the value of the first term alone is "almost" FnF_n. This can be written strictly as:

Fn=[(1+52)n5]F_n = \left[\frac{\left(\frac{1 + \sqrt{5}}{2}\right)^n}{\sqrt{5}}\right]

where the square brackets denote rounding to the nearest integer.

As these two formulas would require very high accuracy when working with fractional numbers, they are of little use in practical calculations.

Fibonacci in linear time

The nn-th Fibonacci number can be easily found in O(n)O(n) by computing the numbers one by one up to nn. However, there are also faster ways, as we will see.

We can start from an iterative approach using the formula Fn=Fn−1+Fn−2F_n = F_{n-1} + F_{n-2}. The implementation only needs to keep the two most recent values, starting from the base cases F0F_0 and F1F_1.

text
int fib(int n) {
    int a = 0;
    int b = 1;
    for (int i = 0; i < n; i++) {
        int tmp = a + b;
        a = b;
        b = tmp;
    }
    return a;
}

In this way, we obtain a linear solution with O(n)O(n) time and O(1)O(1) additional memory.

Matrix form

To go from (Fn,Fn−1)(F_n, F_{n-1}) to (Fn+1,Fn)(F_{n+1}, F_n), we can express the linear recurrence as a 2x2 matrix multiplication:

(1110)(FnFn−1)=(Fn+Fn−1Fn)=(Fn+1Fn)\begin{pmatrix} 1 & 1 \\ 1 & 0 \end{pmatrix} \begin{pmatrix} F_n \\ F_{n-1} \end{pmatrix} = \begin{pmatrix} F_n + F_{n-1} \\ F_{n} \end{pmatrix} = \begin{pmatrix} F_{n+1} \\ F_{n} \end{pmatrix}

This lets us treat iterating the recurrence as repeated matrix multiplication, which has nice properties. In particular,

(1110)n(F1F0)=(Fn+1Fn)\begin{pmatrix} 1 & 1 \\ 1 & 0 \end{pmatrix}^n \begin{pmatrix} F_1 \\ F_0 \end{pmatrix} = \begin{pmatrix} F_{n+1} \\ F_{n} \end{pmatrix}

where F1=1,F0=0F_1 = 1, F_0 = 0. In fact, since

(1110)=(F2F1F1F0)\begin{pmatrix} 1 & 1 \\ 1 & 0 \end{pmatrix} = \begin{pmatrix} F_2 & F_1 \\ F_1 & F_0 \end{pmatrix}

we can use the matrix directly:

(1110)n=(Fn+1FnFnFn−1)\begin{pmatrix} 1 & 1 \\ 1 & 0 \end{pmatrix}^n = \begin{pmatrix} F_{n+1} & F_n \\ F_n & F_{n-1} \end{pmatrix}

Thus, in order to find FnF_n in O(log⁡n)O(\log n) time, we must raise the matrix to n. (See Binary exponentiation)

text
struct matrix {
    long long mat[2][2];
    matrix friend operator *(const matrix &a, const matrix &b){
        matrix c;
        for (int i = 0; i < 2; i++) {
          for (int j = 0; j < 2; j++) {
              c.mat[i][j] = 0;
              for (int k = 0; k < 2; k++) {
                  c.mat[i][j] += a.mat[i][k] * b.mat[k][j];
              }
          }
        }
        return c;
    }
};

matrix matpow(matrix base, long long n) {
    matrix ans{ {
      {1, 0},
      {0, 1}
    } };
    while (n) {
        if(n&1)
            ans = ans*base;
        base = base*base;
        n >>= 1;
    }
    return ans;
}

long long fib(int n) {
    matrix base{ {
      {1, 1},
      {1, 0}
    } };
    return matpow(base, n).mat[0][1];
}

Fast Doubling Method

By expanding the above matrix expression for n=2⋅kn = 2\cdot k

(F2k+1F2kF2kF2k−1)=(1110)2k=(Fk+1FkFkFk−1)2\begin{pmatrix} F_{2k+1} & F_{2k}\\ F_{2k} & F_{2k-1} \end{pmatrix} = \begin{pmatrix} 1 & 1\\ 1 & 0 \end{pmatrix}^{2k} = \begin{pmatrix} F_{k+1} & F_{k}\\ F_{k} & F_{k-1} \end{pmatrix} ^2

we can find these simpler equations:

F2k+1=Fk+12+Fk2F2k=Fk(Fk+1+Fk−1)=Fk(2Fk+1−Fk).\begin{align} F_{2k+1} &= F_{k+1}^2 + F_{k}^2 \\ F_{2k} &= F_k(F_{k+1}+F_{k-1}) = F_k (2F_{k+1} - F_{k})\\ \end{align}.

Thus using above two equations Fibonacci numbers can be calculated easily by the following code:

text
pair<int, int> fib (int n) {
    if (n == 0)
        return {0, 1};

    auto p = fib(n >> 1);
    int c = p.first * (2 * p.second - p.first);
    int d = p.first * p.first + p.second * p.second;
    if (n & 1)
        return {d, c + d};
    else
        return {c, d};
}

The above code returns FnF_n and Fn+1F_{n+1} as a pair.

Periodicity modulo p

Consider the Fibonacci sequence modulo pp. We will prove the sequence is periodic.

Let us prove this by contradiction. Consider the first p2+1p^2 + 1 pairs of Fibonacci numbers taken modulo pp:

(F0, F1), (F1, F2), …, (Fp2, Fp2+1)(F_0,\ F_1),\ (F_1,\ F_2),\ \ldots,\ (F_{p^2},\ F_{p^2 + 1})

There can only be pp different remainders modulo pp, and at most p2p^2 different pairs of remainders, so there are at least two identical pairs among them. This is sufficient to prove the sequence is periodic, as a Fibonacci number is only determined by its two predecessors. Hence if two pairs of consecutive numbers repeat, that would also mean the numbers after the pair will repeat in the same fashion.

We now choose two pairs of identical remainders with the smallest indices in the sequence. Let the pairs be (Fa, Fa+1)(F_a,\ F_{a + 1}) and (Fb, Fb+1)(F_b,\ F_{b + 1}). We will prove that a=0a = 0. If this was false, there would be two previous pairs (Fa−1, Fa)(F_{a-1},\ F_a) and (Fb−1, Fb)(F_{b-1},\ F_b), which, by the property of Fibonacci numbers, would also be equal. However, this contradicts the fact that we had chosen pairs with the smallest indices, completing our proof that there is no pre-period (i.e the numbers are periodic starting from F0F_0).

Practice Problems