Loading [MathJax]/jax/output/HTML-CSS/jax.js

Summation formula

We learned in Problem 1: Multiples of 3 and 5 that:

k=n(n+1)2

This gives the following formula for the square of the sum:

(1+2+...+10)2=(k)2=(n(n+1)2)2=n2(n+1)24

The sum of the squares can be found using the formula:

k2=n(n+1)(2n+1)6

There are many demonstrations to prove this equation, let's just look at one of them:

(k1)3=k33k2+3k1k3(k1)3=3k23k+1nk=1(k3(k1)3)=nk=13k23k+1nk=1k3nk=1(k1)3=3nk=1k23nk=1k+nk=11nk=1k3n1k=0k3=3nk=1k23n(n+1)2+nn3=3nk=1k23n(n+1)2+nnk=1k2=13n3+n(n+1)213nnk=1k2=n(n+1)(2n+1)6

The solution can be found in constant time with these two equations:

n2(n+1)24n(n+1)(2n+1)6=3n4+2n33n22n12

From solution2.py:

def sum_square_difference(n=100): return (3 * n**4 + 2 * n**3 - 3 * n**2 - 2 * n) // 12