**Problem 6**

The sum of the squares of the first ten natural numbers is,

1^{2} + 2^{2} + … + 10^{2} = 385

The square of the sum of the first ten natural numbers is,

(1 + 2 + … + 10)^{2} = 55^{2} = 3025

Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 385 = 2640.

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

**Solution**

(1 + 2 + … + 10)^2 = 55^2 = 3025

Hence the difference between the sum

of the squares of the first ten natural

numbers and the square of the sum is

3025 – 385 = 2640.

Find the difference between the sum of

the squares of the first one hundred natural

numbers and the square of the sum.

–>

<head>^{}

<title>Project Euler 006</title>

</head>

<body >

<script type="text/javascript">

n = 100;

answer=.25*Math.pow(n,4)+1/6*Math.pow(n,3)-.25*Math.pow(n,2)-1/6*n;

document.write("<b>" + answer + "</b>");

</script>

</body>

</html>

**Discussion**

You’ll notice that this is fast, but different from the IronRuby solution given a couple of days ago. if you know that this is a 4th order polynomial then you can solve for the coefficients using simple linear algebra techniques. That is, assume the solution is in the form of

ax^{4} + bx^{3} + cx^{2} + dx + e

Then you just need to solve for a, b, c, d, and e. This is a pretty trivial tasks if you have the right tools at your disposal. Since there are 5 unknowns here we need 5 equations. Let’s plug 0 in first.

a*0 + b*0 + c * 0 + d * 0 + e = 0

This means that e = 0 so now we actually only have 4 unknowns a, b, c, and d. If you repeat this process by replacing x with your number (1,2,3,4) and setting the right side of the equation to the square(sums) – sum(squares) then you will get this matrix:

n^{4} |
n^{3} |
n^{2} |
n |
s |

1 | 1 | 1 | 1 | 0 |

16 | 8 | 4 | 2 | 4 |

81 | 27 | 9 | 3 | 22 |

256 | 64 | 16 | 4 | 70 |

Where **s **is the solution to *sum(n) ^{2} – sum(n^{2})*

**.**If you plug this into a solver you will get:

**(1/4, 1/6, –1/4, –1/6)**

The only real trick is determining that a 4th order polynomial will be adequate to solve the problem. If you are really curious how to do that, leave a comment or please find me on Twitter (@azzlsoft) or email (rich@azzlsoft.com).

## Leave a Reply