|
Fibonacci numbers are defined as follows
F(0) = 1
F(1) = 1
F(n) = F(n-1) + F(n-2)
-- Mathematical Handbook 2nd ed., G. Korn, T. Korn, 1968. paragraph 8.7-2
Please note that there exists a different definition, with F(0) = 0 and F(1) = 1. The conversion between the two is trivial.
ALGORITHMS AND BENCHMARKS SUMMARY
All implemented algorithms are discussed in detail on a ALGORITHMS page.
The benchmarks are discussed and analyzed in detail on a BENCHMARKS page too.
| 1. EXPONENTIAL COMPLEXITY |
ALGORITHM 1A Binary Recursion. |
Top 3 languages T(46) |
Recursively calculate F(n-1),
recursively calculate F(n-2),
and return the sum of the results.
|
- OCaml
49.3
- Assembly
55.4
- C
66.2
|
| 2. LINEAR COMPLEXITY |
ALGORITHM 2A Data Structure. |
Top 3 languages T(150,000) |
ALGORITHM 2B Simple Recursion. |
Top 3 languages T(500,000) |
ALGORITHM 2C Non-recursive Loop. |
Top 3 languages T(500,000) |
|---|
The previously calculated Fibonacci Numbers may be stored in a data structure, which elminiates the need for binary recursion.
- 2A-1: Infinite list.
- 2A-2: Infinite list with exceptions.
- 2A-3: Simple list
|
- Haskell
10.7
- OCaml
13.2
- perl
383.0
|
Only two values have to be remembered to calculate the next Fibonacci number. This algorithm keeps them as parameters of a recursive funciton.
|
- OCaml
28.0
- Haskell
35.8
- Scheme
37.7
|
Here the two previous Fibonacci numbers are stored in writable variables
|
- OCaml
27.4
- Scheme
37.8
- Java
91.0
|
| 3. LOGARITHM COMPLEXITY |
ALGORITHM 3A Matrix Equation. |
Top 3 languages T(1,000,000) |
ALGORITHM 3B Fast Recursion. |
Top 3 languages T(1,000,000) |
ALGORITHM 3C Binet's Formula. |
|
|---|
|F(n) F(n-1)| |1 1|^n
| |=| |
|F(n-1) F(n-2)| |1 0|
|
- OCaml
16.53
- Scheme
30.30
- Prolog
54.97
|
F(n)=F(n/2)2+F(n/2-1)2 for even n
F(n)=F((n-1)/2)*F((n-1)/2+1)+F((n-1)/2)*F((n-1)/2-1) for odd n
|
- OCaml
11.56
- Scheme
12.85
- Prolog
47.75
|
F(n) = (φn+1-(1-φ)n+1)/sqrt(5), where
φ = (1+sqrt(5))/2
|
|
EXAMPLES
LINKS
Wolfram's MathWorld entry
OEIS entry
|