← Previous | ↑ All problems | Go to Project Euler | Next →

Project Euler Problem 6

Sum Square Difference

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.

Not sure if there's much to say on this problem besides that using two special cases of Faulhaber's formula for powers of 1 and 2 we can write

$$\sum_{k=1}^n k = \frac{n(n+1)}{2}$$

and

$$\sum_{k=1}^n k^2 = \frac{n(n+1)(2n+1)}{6}$$

So we can compute the answer directly using

$$\operatorname{SSD}(n) = \left( \sum_{k=1}^n k \right)^2 - \sum_{k=1}^n k^2 = \left( \frac{n(n+1)}{2} \right)^2 - \frac{n(n+1)(2n+1)}{6}$$

With a simple implementation

function square_of_sum(n)
    sum = n * (n + 1) ÷ 2
    return sum^2
end

function sum_of_squares(n)
    return n * (n + 1) * (2n + 1) ÷ 6
end

function sum_square_difference(n)
    return square_of_sum(n) - sum_of_squares(n)
end

we call sum_square_difference(100) to compute the solution which runs in 0.951 ns.