Project Euler Problem #48

April 28, 2010

Read the details of the problem here

Summary

Find the last ten digits of 11 + 22 + … + 10001000.

Solution

It’s a one-liner in Groovy, using the useful BigInteger.modPow() function to raise the base to the required power and then only retain the trailing digits required.

def answer =
    (1G..1000G).sum { it.modPow(it, 10**10) }.toString()[-10..-1]

This executes in 0.11 seconds.

Conclusion

Groovy listops prove their worth once again when combined with Java’s BigInteger support.

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: