Project Euler Problem #56

May 21, 2010

Read the details of the problem here


Considering natural numbers of the form, ab, finding the maximum digital sum.


Very easy to create a brute force solution with Java’s BigInteger support. I guessed that the answer would be found up in the high end of the range specified and only searched there.

BigInteger.metaClass.digitSum() { ->

def answer = 0

(90G..99G).each { a ->
  (90G..99G).each { b ->
    answer = [ answer, a.pow(b).digitSum() ].max()

Run time is 95 milliseconds.


Groovy’s transparent integration with the Java BigInteger class made this a snap to put together. The listops, spread operator and metaClass extensions made for a clean syntax.

It’s actually possible to do the calculation as Groovy “one-liner” which has the same run time.

def answer =
                             .collect { it[0].pow(it[1]).digitSum() }

Not sure if I like this or not.


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: