The Fibonacci problem is a well known mathematical problem that models population growth and was conceived in the 1200s. Leonardo of Pisa aka Fibonacci decided to use a recursive equation:
Since the Fibonacci sequence was conceived to model population growth, it would seem that there should be a simple equation that grows almost exponentially. Plus, this recursive calling is expensive both in time and memory.2. The cost of this function doesn’t seem worthwhile. To see the surprising formula that we end up with, we need to define our Fibonacci problem in a matrix language.3
Calling each of those matrices and vectors variables and recognizing the fact that
where we have used
While this isn’t recursive, there’s still an
The trick to do this rests on the mysterious and intimidating eigenvalues and eigenvectors. These are just a nice way to view the same data but they have a lot of mystery behind them. Most simply, for a matrix
for different eigenvalues
Or compacting the vectors
Because the Fibonacci eigenvector matrix is invertible,4
And then because a matrix and it’s inverse cancel
For this Fibonacci matrix, we find that
We would not expect this equation to give an integer. It involves the power of two irrational numbers, a division by another irrational number and even the golden ratio phi
This means we can define our function rather simply:
As one would expect, this implementation is fast. We see speedups of roughly
I’ve found that mathematics5 becomes fascinating, especially in higher level college courses, and can often yield surprising results. I mean, look at this blog post. We went from a expensive recursive equation to a simple and fast equation that only involves scalars. This derivation is one I enjoy and I especially enjoy the simplicity of the final result. This is part of the reason why I’m going to grad school for highly mathematical signal processing. Real world benefits
-
Yes, in some languages some compilers are smart enough to get rid of recursion for some functions.↩
-
I’m assuming you have taken a course that deals with matrices.↩
-
This happens when a matrix is diagonalizable.↩
-
Not math. Courses beyond calculus deserve a different name.↩