## Project Euler Problem #6

### January 6, 2010

Read the details of the problem here

**Summary**

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

**Solution**

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.

**Conclusion**

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!

Advertisements