Project Euler Problem #62

November 19, 2009

Read the details of the problem here


Find the smallest cube for which exactly five permutations of its digits are cube.


It’s a brute force approach with a guessed maxima but the coded solution is a fairly trivial one-liner (but I’ve split it for legibility).

def answer =
        (1..10000).collect { it ** 3 }
                  .groupBy { it.toString().toList().sort().join("") }
                  .findAll { it.value.size() == 5 }
                  .collect { it.value }

It runs in 1.14 seconds.


You’ve got to love the Groovy listops for problems like this. The code wasn’t the fastest to run but easily fell within my 15 second cut-off. It was very quick to put together and the code intent is clear to read. The only slight snag I found was there are actually two solutions that have five cube permutations but this was resolved easily to get the minimum value of the two solutions as the problem required.


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: