Project Euler Problem #20

February 8, 2010

Read the details of the problem here


Find the sum of digits in 100!


Really straightforward if you’ve got BigInteger support.

def answer =
    ((2..100).inject(1G) { p, n -> p * n })

Runs in 0.48 seconds. Using BigInteger.ONE (in Groovy – 1G) as the seed to inject(){} seems like the cleanest way of forcing type that I’ve found for problems like this.


Groovy made this a simple one liner (though I’ve split it for readability).


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 )

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s

%d bloggers like this: