Fibonacci sequence
- The sequence term of rank n.
- The sum of the first n terms.
Fibonacci Sequence
Fibonacci numbers form a sequence of positive integers in which each term is obtained by summing up the two preceding terms, the first two terms being equal to 0 and 1. The general term of Fibonacci's sequence is as follows,
`F_0 =0, F_1 =1` `F_n = F_(n-1) + F_(n-2)` for `n >= 2` |
Binet's Formula
Binet's formula is an explicit formula of the n-th term of the Fibonacci sequence. So, if `F_n` is the term of rank n then,
`F_n=1/sqrt(5)*(((1+sqrt(5))/2)^n-((1-sqrt(5))/2)^n)` |
Another way to write this formula using the Golden ratio is as follows,
`phi = (1+sqrt(5))/2` `F_n=1/sqrt(5)*(phi^n-(-phi)^-n)` |
Fibonacci numbers algorithms
Simple algorithm
//This algorithm simply applies the definition of Fibonacci sequence
function fibonacci(n)
if n = 0
return 0
if n = 1
return 1
return fibonacci(n-1) + fibonacci(n-2)
This algorithm is given for pedagogic purpose. It is not optimal and not recommended to use,
- it computes multiples times terms preceding n-th term.
- time execution is an exponential function of n.
Dynamic algorithm
function fibonacci(n)
//initialization of fibonacci numbers list
f = (0,1)
For i from 2 to n
add the following element to f list : f[i-1] + f[i-2]
return f[n]
The main advantage of this algorithm is that it avoids re-calculating the term at each iteration as in the first algorithm.
Algorithm using Binet's formula
//this function calculates Fibonacci numbers using the golden ratio
fonction fibonacci(n)
phi = (1+sqrt(5))/2
return (phi^n-(-phi)^(-n))/sqrt(5)
This algorithm ist faster as it does not calculate preceding term of rank less than n. its disadvantage is that depending on the machine it is executed on, from a certain rank of n, it will reach a limit for the number of computed decimal places and then round the result to the wrong values.
The first 50 terms of the Fibonacci sequence
Notations :
n is a positive integer
`F_n`: the Fibonacci term of rank n
`S_n`: the sum of the first terms `F_0 + F_1 + ... + F_n`
`F_n/F_(n-1)`: the ratio of two consecutive terms. This is an approximation of the golden ratio when n tends to infinity.
Golden ratio : ` phi = (1+sqrt(5))/2 approx` 1.6180339887499
We notice from the following table that the ratio `F_n/F_(n-1)` rapidly converges towards the golden ratio.
n | `F_n` | `S_n` | `F_n/F_(n-1)` |
---|---|---|---|
0 | 0 | 0 | |
1 | 1 | 1 | |
2 | 1 | 2 | 1 |
3 | 2 | 4 | 2 |
4 | 3 | 7 | 1.5 |
5 | 5 | 12 | 1.66666666666667 |
6 | 8 | 20 | 1.6 |
7 | 13 | 33 | 1.625 |
8 | 21 | 54 | 1.61538461538462 |
9 | 34 | 88 | 1.61904761904762 |
10 | 55 | 143 | 1.61764705882353 |
11 | 89 | 232 | 1.618181818182 |
12 | 144 | 376 | 1.61797752808989 |
13 | 233 | 609 | 1.618055555556 |
14 | 377 | 986 | 1.61802575107296 |
15 | 610 | 1596 | 1.61803713527851 |
16 | 987 | 2583 | 1.61803278688525 |
17 | 1597 | 4180 | 1.61803444782168 |
18 | 2584 | 6764 | 1.61803381340013 |
19 | 4181 | 10945 | 1.61803405572755 |
20 | 6765 | 17710 | 1.61803396316671 |
21 | 10946 | 28656 | 1.6180339985218 |
22 | 17711 | 46367 | 1.61803398501736 |
23 | 28657 | 75024 | 1.6180339901756 |
24 | 46368 | 121392 | 1.61803398820533 |
25 | 75025 | 196417 | 1.6180339889579 |
26 | 121393 | 317810 | 1.61803398867044 |
27 | 196418 | 514228 | 1.61803398878024 |
28 | 317811 | 832039 | 1.6180339887383 |
29 | 514229 | 1346268 | 1.61803398875432 |
30 | 832040 | 2178308 | 1.6180339887482 |
31 | 1346269 | 3524577 | 1.61803398875054 |
32 | 2178309 | 5702886 | 1.61803398874965 |
33 | 3524578 | 9227464 | 1.61803398874999 |
34 | 5702887 | 14930351 | 1.61803398874986 |
35 | 9227465 | 24157816 | 1.61803398874991 |
36 | 14930352 | 39088168 | 1.61803398874989 |
37 | 24157817 | 63245985 | 1.6180339887499 |
38 | 39088169 | 102334154 | 1.61803398874989 |
39 | 63245986 | 165580140 | 1.6180339887499 |
40 | 102334155 | 267914295 | 1.61803398874989 |
41 | 165580141 | 433494436 | 1.6180339887499 |
42 | 267914296 | 701408732 | 1.6180339887499 |
43 | 433494437 | 1134903169 | 1.6180339887499 |
44 | 701408733 | 1836311902 | 1.6180339887499 |
45 | 1134903170 | 2971215072 | 1.6180339887499 |
46 | 1836311903 | 4807526975 | 1.6180339887499 |
47 | 2971215073 | 7778742048 | 1.6180339887499 |
48 | 4807526976 | 12586269024 | 1.6180339887499 |
49 | 7778742049 | 20365011073 | 1.6180339887499 |
50 | 12586269025 | 32951280098 | 1.6180339887499 |
See also
Arithmetic sequence
Geometric sequence