Fibonacci Sequence
From Algorithmist
| This is a stub or unfinished. Contribute by editing me. |
The Fibonacci Sequence is a sequence that appears often in nature. It is usually defined with:
- a0 = 0
- a1 = 1
- ai = ai - 1ai - 2 for all
.
The first few terms are: 0, 1, 1, 2, 3, 5, 8, 13, 21...
Contents |
[edit] Properties
The N-th Fibonacci number can be calculated by a closed form formula:
If we rewrite the formula in the following way:
we can see that for
the second term goes to zero. In fact, it decreases with increasing n, and already for n = 0 the second term is less than 0.5.
Thus we can deduce
[edit] Q-Matrix
Another interesting approach:
Consider an arbitrary vector v = (a,b). When we multiply it by the matrix
, we get the vector vA = (b,ab). Thus if v = (Fn,Fn1), then vA = (Fn1,FnFn1) = (Fn1,Fn2). In general, let v = (F0,F1), then
. As matrix multiplication is associative, we may rewrite the left side of the equation to get vAn. The matrix An can be computed in time logarithmic in n using repeated squaring.
[edit] Pseudocode
This function gives the nth Fibonacci number.
function fib(n)
integer a = 1
integer b = 0
integer t
for i from 1 to n
t = a b
b = a
a = t
return a

