Project Euler Problem #25

February 9, 2010

Read the details of the problem here


What is the first term in the Fibonacci sequence to contain 1000 digits?


I just took the brute-force naive approach for this as Java, with BigInteger support, makes it just too tempting not do so. Note that it’s quicker (by about 30%) to check the length of the string representation rather than comparing it to a terminal value.

def (fib1, fib2, answer) = [ 1G, 2G, 3 ]

while (fib2.toString().size() < 1000) {
    (fib1, fib2, answer) = [ fib2, fib1+fib2, answer+1 ]

This runs in 2.94 seconds. There are cleverer ways to do this using the Golden Ratio relationships to derive the value of the term mathematically but this took seconds to write and is obviously correct!


Groovy, with automatic support for Java’s BigInteger, made this very quick and simple to write.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: