The square of the sum vs. the sum of the squares

The sixth Project Euler problem poses an exercise that, to me, offers no major hurdles:

What is the difference between the sum of the squares and the square of the sums [of a sequence of natural numbers]?

The functional C# solution is fairly easy to write and read:

    1     public int SquareSumsLessSumSquares(IEnumerable<int> sequence)

    2     {

    3         var sum = sequence.Sum();

    4         var sumSquares = sequence.Select(i => i * i).Sum();

    5         return sum * sum - sumSquares;

    6     }

We get any sequence of integers (list, array, etc.) and we find the sum of its elements at line 3. The Select() in line 4 generates a new sequence with the square of each item from the original sequence and then we add them up. From there, getting the answer to Problem 6 is easy. Actually, the specific value that the problem asks for is to find out this difference for the first 100 natural numbers, so the corresponding call is:

    var result006 = SquareSumsLessSumSquares(Enumerable.Range(1, 100));

Now, given that the sequence we are focused on is 1, 2, 3, ..., 100, we can leverage a couple of math formulas:

 

y

 

Isn't the Wikipedia something? With this information in our hands we can re-write our solution this way:

    1     public int SquareSumsLessSumSquaresFirstNumbers(int n)

    2     {

    3         var sum = n * (n + 1) / 2;

    4         var sumSquares = n * (n + 1) * (2 * n + 1) / 6;

    5         return sum * sum - sumSquares;

    6     }

Clearly this latter way of solving the problem uses far less memory and CPU time. My first solution follows what is called a "brute force" approach, contemption intended. It works, for sure, but there are more elegant and efficient ways of solving the problem. The moral of this story is that a little research can take us a long way towards fresh solutions, probably better than our first approach. How frequently have you seen brute force solutions in production environments?

Published Sunday, April 27, 2008 9:56 PM by Edgar Sánchez

Comments

# projecteuler

Saturday, May 24, 2008 4:50 PM by projecteuler

Pingback from  projecteuler

# re: The square of the sum vs. the sum of the squares

Friday, February 06, 2009 2:06 AM by ...

nice site you have!

# re: The square of the sum vs. the sum of the squares

Wednesday, March 11, 2009 4:26 AM by Buy_Actigall

<a href=forums.slimdevices.com/member.php Actigall</a>

# re: The square of the sum vs. the sum of the squares

Wednesday, March 25, 2009 5:07 AM by Figure_Flattening_Garter_Belts

The Best Figure Flattening Garter Belts Online Available

# re: The square of the sum vs. the sum of the squares

Saturday, December 05, 2009 8:05 AM by Anon

This was interesting to use after I'd solved the problems:

www.mledwards.co.uk/project-euler-algorithms

# re: The square of the sum vs. the sum of the squares

Tuesday, March 23, 2010 5:43 AM by Hedy

Good afternoon. The most important thing she'd learned over the years was that there was no way to be a perfect mother and a million ways to be a good one. Help me! I can not find sites on the: Cheap hair extensions in southeast michigan. I found only this - <a href="www.sigcas.org/.../CheapHairExtensions">cheap hair extension wefts</a>. Cheap hair extensions, during the song dynasty, the recognition of long requirement was very deflated upon this nice crucial fusion of dates and types. Presenters of directorates who loom also change in the wide discomfort will especially provide no frequency what during the proposed precision, and can be written as a time of process, cheap hair extensions. :cool: Thanks in advance. Hedy from Austria.

Leave a Comment

(required) 
(required) 
(optional)
(required)