Taken from
Math Forum - Ask Dr. Math
First note that a sum of the form
(1 - 0) + (8 - 1) + (27 - 8) + ... + (n^3 - (n-1)^3)
will collapse, since we have 1 in the first term and -1 in the second,
8 in the second and -8 in the third, and so on, to
n^3 - 0
So if we can express x^2 in terms of such differences, we can find the
sum very easily. Notice now that
x^3 - (x-1)^3 = x^3 - (x^3 - 3x^2 + 3x - 1) = 3x^2 - 3x + 1
and
x^2 - (x-1)^2 = x^2 - (x^2 - 2x + 1) = 2x - 1
We can use these to write
x^2 = ([x^3 - (x-1)^3] + 3x - 1)/3
and
x = ([x^2 - (x-1)^2] + 1)/2
We can plug the second into the first of these to get
x^2 = ([x^3 - (x-1)^3] + 3([x^2 - (x-1)^2] + 1)/2 - 1)/3
[x^3 - (x-1)^3]/3 + [x^2 - (x-1)^2]/2 - 1/6
Now, summing this for x = 1 to x = n, we get
sum(x^2) = sum([x^3 - (x-1)^3]/3 + [x^2 - (x-1)^2]/2 - 1/6)
= sum[x^3 - (x-1)^3]/3 + sum[x^2 - (x-1)^2]/2 - sum(1/6)
= [n^3 - 0^3]/3 + [n^2 - 0^2]/2 - n/6
= n^3/3 + n^2/2 - n/6
= (2n^3 + 3n^2 - n)/6