Project Euler Problem #62

November 19, 2009

Read the details of the problem here

Summary

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

Solution

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 }
                  .flatten()
                  .min()

It runs in 1.14 seconds.

Conclusion

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.

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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: