Project Euler Problem #16

February 4, 2010

Read the details of the problem here

Summary

What is the sum of the digits of the number 21000?

Solution

Very little to say about this one. The only slight twist is the ugly cast to a BigInteger otherwise Groovy will create a BigDecimal and give an engineering string representation.

def answer =
    ((BigInteger)(2**1000)).toString().toList()*.toInteger().sum()

Runs in 0.42 seconds.

Conclusion

Quick and easy to write in Groovy using 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: