Project Euler Problem #6

January 6, 2010

Read the details of the problem here


Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.


Little need for commentary on this quick solution which is very descriptive of the problem being set.

def answer = (1..100).sum() ** 2 - (1..100).sum { it * it }

This runs in 445 ms. Very slow for what it’s doing but well within my permitted 15 seconds.


Sure, I could have solved this algebraically (as below) but Groovy listops were calling…

def answer = (100 ** 2 * 101 ** 2 / 4) - (100 * 101 * 201 / 6)

This algebraic solution, which doesn’t need any actual programming, is run by Groovy in 14 ms. Hmm, that’s an eternity for a simple statement like that!


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: