Project Euler Problem #8

January 6, 2010

Read the details of the problem here


Discover the largest product of five consecutive digits in the 1000-digit number.


Nice and straightforward, this is just a programming rather than an algorithmic puzzle. Not much to say on this, the implemention is fairly minimal and trivial.

def s =
  "73167176531330624919225119674426574742355349194..." +

def answer = 0
for (i in 0..s.length()-5) {
   answer = [ answer, (s[i..i+4]).inject(1) { r, n -> r * n.toInteger() } ].max()

This runs on 0.66 seconds and was very straightforward to write.


Groovy, with its list#inject method, made it very easy to create what is essentially a one liner – albeit not the most efficient piece of code in the world. As always, performance was slower than hoped for but perfectly adequate for solving this problem.


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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: