Fibonacci Numbers¶
The Fibonacci sequence is defined as follows:
The first elements of the sequence (OEIS A000045) are:
Properties¶
Fibonacci numbers possess a lot of interesting properties. Here are a few of them:
 Cassini's identity:
 The "addition" rule:
 Applying the previous identity to the case $k = n$, we get:

From this we can prove by induction that for any positive integer $k$, $F_{nk}$ is multiple of $F_n$.

The inverse is also true: if $F_m$ is multiple of $F_n$, then $m$ is multiple of $n$.

GCD identity:
 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 $n$ can be uniquely represented as a sum of Fibonacci numbers:
such that $k_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 $d_0 d_1 d_2 \dots d_s 1$, where $d_i$ is $1$ if $F_{i+2}$ is used in the representation. The code will be appended by a $1$ to indicate the end of the code word. Notice that this is the only occurrence where two consecutive 1bits appear.
The encoding of an integer $n$ can be done with a simple greedy algorithm:

Iterate through the Fibonacci numbers from the largest to the smallest until you find one less than or equal to $n$.

Suppose this number was $F_i$. Subtract $F_i$ from $n$ and put a $1$ in the $i2$ position of the code word (indexing from 0 from the leftmost to the rightmost bit).

Repeat until there is no remainder.

Add a final $1$ to the codeword to indicate its end.
To decode a code word, first remove the final $1$. Then, if the $i$th bit is set (indexing from 0 from the leftmost to the rightmost bit), sum $F_{i+2}$ to the number.
Formulas for the $n^{\text{th}}$ Fibonacci number¶
Closedform expression¶
There is a formula known as "Binet's formula", even though it was already known by Moivre:
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 $1$, and it also decreases very rapidly (exponentially). Hence the value of the first term alone is "almost" $F_n$. This can be written strictly as:
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 $n$th Fibonacci number can be easily found in $O(n)$ by computing the numbers one by one up to $n$. However, there are also faster ways, as we will see.
We can start from an iterative approach, to take advantage of the use of the formula $F_n = F_{n1} + F_{n2}$, therefore, we will simply precalculate those values in an array. Taking into account the base cases for $F_0$ and $F_1$.
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, $O(n)$ time, saving all the values prior to $n$ in the sequence.
Matrix form¶
It is easy to prove the following relation:
Thus, in order to find $F_n$ in $O(log n)$ time, we must raise the matrix to n. (See Binary exponentiation)
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\cdot k$
we can find these simpler equations:
Thus using above two equations Fibonacci numbers can be calculated easily by the following code:
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};
}
Periodicity modulo p¶
Consider the Fibonacci sequence modulo $p$. We will prove the sequence is periodic.
Let us prove this by contradiction. Consider the first $p^2 + 1$ pairs of Fibonacci numbers taken modulo $p$:
There can only be $p$ different remainders modulo $p$, and at most $p^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 $(F_a,\ F_{a + 1})$ and $(F_b,\ F_{b + 1})$. We will prove that $a = 0$. If this was false, there would be two previous pairs $(F_{a1},\ F_a)$ and $(F_{b1},\ 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 preperiod (i.e the numbers are periodic starting from $F_0$).
Practice Problems¶
 SPOJ  Euclid Algorithm Revisited
 SPOJ  Fibonacci Sum
 HackerRank  Is Fibo
 Project Euler  Even Fibonacci numbers
 DMOJ  Fibonacci Sequence
 DMOJ  Fibonacci Sequence (Harder)
 DMOJ UCLV  Numbered sequence of pencils
 DMOJ UCLV  Fibonacci 2D
 DMOJ UCLV  fibonacci calculation
 LightOJ  Number Sequence
 Codeforces  C. Fibonacci
 Codeforces  A. Hexadecimal's theorem
 Codeforces  B. Blackboard Fibonacci
 Codeforces  E. Fibonacci Number