Home Project Euler Teaching Projects Gallery

Problem 6: Sum square difference

The sum of 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 + 3 + .. + 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 = 264\)

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


Algorthim:

Super easy problem. There are exist nice formulas for both summation: sum of squares: $$\sum_{i=1}^n i^2 = \frac{(n)(n + 1)(2n + 1)}{6}$$ sum of arthimetic sequence: $$\sum_{i=1}^n a_0 + bi = \frac{(a_0 + a_0 + bn)}{2} $$ Essentially, just average of the first and last term

Code:

Result:

Answer: 25164150
Runtime: 0.8 ms