Project Euler Problem #13

January 27, 2010

Read the details of the problem here

Summary

Find the first ten digits of the sum of one-hundred 50-digit numbers.

Solution

It’s a no-brainer.

def nums = [
37107287533902102798797998220837590246510135740250,
...
53503534226472524250874054075591789781264330331690 ]

def answer = nums.sum().toString()[0..9]

I only need to really sum the first 11-digits of each number, so longs would have sufficed but this was so tempting I just did the simplest thing.

This runs in 0.43 seconds. Slow, but I’m not going to worry about this for a problem of this sort.

Conclusion

Groovy made this a snap with automatic BigInteger casting and a suitable aggregation listop.

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: