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.
$$result = (\sum_{i=1}^m i)^2 - \sum_{i=1}^m i^2$$
It is a problem of 'multinomial theorem'.
Refer to Wikipedia: Multinomial_theorem
$$(x_1 + x_2 + ... + x_t)^n = \sum \frac{n!}{n_1!n_2!...n_t!} x_1^{n_1} x_2^{n_2}...x_t^{n_t}$$
and $$n_1 + n_2 + ... + n_t = n, 0 \le n_i \le n$$
Here, n = 2, so above equation become:
$$(\sum_{i=1}^m i)^2 = (1 + 2 + ... + m)^2 = \sum \frac{2!}{n_1!n_2!...n_m!} 1^{n_1}2^{n_2}...m^{n_m} $$
and $$n_1 + n_2 + ... + n_m = 2, 0 \le n_i \le n$$
So, $$if \; n_i = 2, then \; n_j = 0, 1 \le j \le t, j \ne i$$
So, $$result = (\sum_{i=1}^m i)^2 - \sum_{i=1}^m i^2 = \sum \frac{2!}{n_j!n_k!} j*k = \sum \frac{2}{1!1!} j*k = 2 \sum j*k$$
and here (j, k) is combination of [1 to m]
from itertools import combinations
def solve(end):
return 2 * sum((j * k for j, k in combinations(range(1, end+1), 2)))
solve(10)
solve(100)