Fast Fibonacci with Matrix
1, 1, 2, 3, 5, 8, 13, 21, 34, 55, … Even though most of us aren’t good at math and calculation, but we might familiar with this sequence that people consider it the most beautiful sequence in the nature. The instruction to find the sequence is actually not that complicate: to find the next number is to find a sum of the two previous numbers, where the very first two numbers are defined as 1.
In other equations,
When
Students in computer-science should recognize that the above equation is a recursion. However, if we implement it as-is, we’ll run out of time even though we compute only around half-a-hundred
Can we find the Fibonacci number faster?
If we want the whole sequence (from the first index to our interesting index), the above dynamic programming is already satisfactory fast.
However, if we just want a Fibonacci number at index
Where
However, the core idea of this equation is the position of Fibonacci vectors. It enable us to chain the sequence of computation, like this
Generally, we say that for every
The formula is already pretty. But we can use it more efficiently, that is we will eliminate a variable
Rename
Observe that the exponentiation takes the most time in the computation. Which we’ll employ an exponentiation by squaring – divide the exponent a half recursively so we can cache the same terms. Hence the time is reduced to
P.S. feel that result is not beauty enough? Then use this equivalent formula instead (the proof is left as an exercise to the reader)

author