Re: iterative-version for computing a Fibonacci number
- From: "Gerard Flanagan" <grflanagan@xxxxxxxxxxx>
- Date: 23 Jul 2006 11:48:28 -0700
arnuld wrote:
Paul F. Dietz wrote:
( 0 1 )
A = ( )
( 1 1 )
times the vector ( F_0 F_1 ) is the vector ( F_n F_{n+1} ).
So, compute 1 + A^2 + A^4 + ... + A^n. This is equal
to (A^2 - I)^-1 (A^{n+2) - I), which can be computed in O(log n)
operations.
i did not even get the "a,b,c" of this method. keep in mind that i am
pretty weak at maths.
Take A to be the following 2x2 matrix (2 rows and 2 columns):
|1 1|
|1 0|
if you keep multiplying A by itself you get the following sequence:
A^2 =
|2 1|
|1 1|
A^3=
|3 2|
|2 1|
A^4=
|5 3|
|3 2|
A^5=
|8 5|
|5 3|
A^6=
|13 8|
|8 5|
A^7=
|21 13|
|13 8|
A^8=
|34 21|
|21 13|
and so on.
You will notice that the sequence of first numbers from each matrix is
the Fibonacci sequence.
Also note that the 'nth' power of A gives the 'n+1th' Fibonacci.
This technique is used in the Python code here:
http://en.wikipedia.org/wiki/Fibonacci_number_program#Matrix_equation
with reference to:
http://en.wikipedia.org/wiki/Exponentiating_by_squaring#Squaring_algorithm
(It in fact uses 3-tuples beginning with (1,1,0) because the symmetry
of the matrices in the sequence means that only three multiplications
are necessary).
Lisp and Scheme code (not using the matrix method) from the same page:
http://en.wikipedia.org/wiki/Fibonacci_number_program#Common_Lisp
http://en.wikipedia.org/wiki/Fibonacci_number_program#Scheme
hth
Gerard
(I am not a lisper, just lurking after reading this:
http://www.defmacro.org/ramblings/lisp.html )
.
- References:
- iterative-version for computing a Fibonacci number
- From: arnuld
- Re: iterative-version for computing a Fibonacci number
- From: Paul F. Dietz
- Re: iterative-version for computing a Fibonacci number
- From: arnuld
- iterative-version for computing a Fibonacci number
- Prev by Date: Re: need to swap white and black
- Next by Date: Re: Amazon used lisp & C exclusively?
- Previous by thread: Re: iterative-version for computing a Fibonacci number
- Next by thread: Re: iterative-version for computing a Fibonacci number
- Index(es):
Relevant Pages
|
Loading