Mathematics

Finding the 1000 digit Fibonacci number using the Golden Ratio

Rather than sticking to using a brute force solution, I decided to find the 1000 digit fibonacci number using the Golden Ratio.

Abdush Shakoor May 18th, 2018

Back in February, I said that I'm going to start solving Project Euler problems using Python and yes, I have been doing it actively but sometimes, I don't find the time to post the solutions.

If you're someone who has tried Project Euler before, you'll know that you'll get to encounter problems that require you to implement smart solutions to crunch big numbers efficiently. One of those problems is Problem 25, which states to find the first term in the Fibonacci Sequence that contains 1000 digits.

This problem can be easily solved using a Brute Force solution, instead, I opted to try out a mathematical solution that makes use of the Golden Ratio and also, it gave me the perfect excuse to try writing those formulas using LaTeX markup.

What is Golden Ratio?

Famously known as Phi that represents the Golden Ratio is an irrational number that's approximately equal to 1.6180 and just like it's cousin, Pi, it has a never ending pattern of decimal digits.

Φ=1.6180339887...

According to the article in Wikipedia, back in the Twentieth Century, architects and artists had proportioned their works to approximate the Golden Ratio in the form of a Golden Rectangle, that is believed to be aesthetically pleasing.

Time for some Calculus!

This is the equation used to find the nth Fibonacci Number:

F(n)=log(Φ)n5

Now, let's modify this formula to find the smallest integer i.e. the first term of the 1000 digit number that fulfills this inequality:

log(Φ)n5>10999

Expand the formula a little bit more:

n×log(Φ)log(5)2>999×log(10)

n×log(Φ)>999×log(10)+log(5)2

We need to find n, so let's isolate it:

n=999×log(10)+log(5)2log(Φ)=4782.06

n4782

So, the first term that contains the 1000 digit Fibonacci Number is 4782.

Here's the solution using Python that solved in 0.000194 seconds:

    
    import math

    limit = 999
    golden_ratio = 1.6180

    n = round(((limit * math.log(10)) + (math.log(5)/2))/(math.log(golden_ratio)))

    print n
    

So, this is how it would look in our first equation above:

F(4782)=log(Φ)47825=SomeLargeNumber

Well, if you try to find the number using a calculator or compile it on your computer, you'll end up having a compile error that says "Math Error"!

Hope you liked reading this article!

References