#Skip to menu

Sum square difference

First read the problem description.

We know that the formula for the sum of squares is \(\sum_{k=1}^{n} k^2 = \frac{2n^3 + 3n^2 + n}{6}\) and that \(\sum_{k=1}^{n} k = \frac{n*(n+1)}{2}\) (see Arithmetic progression). So we simply need to do \(\sum_{k=1}^{n}k^2 - (\sum_{k=1}^{n}k)^2 = \frac{3n^4 + 2n^3 - 3n^2 - 2n}{12}\)

def equation(n):
    return (3*n**4 + 2*n**3 - 3*n**2 - 2*n) // 12

Source code of the solution(s):