All Euler problems
Project Euler

Sum Square Difference

Compute the difference between the square of the sum and the sum of the squares of the first 100 natural numbers: D(100) = (sum_(i=1)^100 i)^2 - sum_(i=1)^100 i^2.

Source sync Apr 19, 2026
Problem #0006
Level Level 00
Solved By 529,603
Languages C++, Python
Answer 25164150
Length 334 words
algebracombinatoricsarithmetic

Problem Statement

This archive keeps the full statement, math, and original media on the page.

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.

Problem 6: Sum Square Difference

Mathematical Development

Notation

Throughout, we write

S1(n)=i=1ni,S2(n)=i=1ni2,D(n)=S1(n)2S2(n).S_1(n) = \sum_{i=1}^{n} i, \qquad S_2(n) = \sum_{i=1}^{n} i^2, \qquad D(n) = S_1(n)^2 - S_2(n).

Lemma 1 (Sum of the first nn natural numbers). For all n1n \ge 1,

S1(n)=n(n+1)2.S_1(n) = \frac{n(n+1)}{2}.

Proof. By induction on nn.

Base case: S1(1)=1=12/2S_1(1) = 1 = 1 \cdot 2 / 2.

Inductive step: Assume S1(k)=k(k+1)/2S_1(k) = k(k+1)/2. Then

S1(k+1)=S1(k)+(k+1)=k(k+1)2+(k+1)=k(k+1)+2(k+1)2=(k+1)(k+2)2.S_1(k+1) = S_1(k) + (k+1) = \frac{k(k+1)}{2} + (k+1) = \frac{k(k+1) + 2(k+1)}{2} = \frac{(k+1)(k+2)}{2}. \quad\square

Alternative proof (Gauss’s pairing argument). Write the sum in two orders:

S1=1+2++n,S1=n+(n1)++1.S_1 = 1 + 2 + \cdots + n, \qquad S_1 = n + (n-1) + \cdots + 1.

Adding termwise: 2S1=i=1n(n+1)=n(n+1)2S_1 = \sum_{i=1}^{n}(n+1) = n(n+1), so S1=n(n+1)/2S_1 = n(n+1)/2. \square

Lemma 2 (Sum of squares). For all n1n \ge 1,

S2(n)=n(n+1)(2n+1)6.S_2(n) = \frac{n(n+1)(2n+1)}{6}.

Proof. Consider the telescoping identity

(k+1)3k3=3k2+3k+1.(k+1)^3 - k^3 = 3k^2 + 3k + 1.

Summing both sides for k=1,2,,nk = 1, 2, \ldots, n:

k=1n[(k+1)3k3]=3S2(n)+3S1(n)+n.\sum_{k=1}^{n}\bigl[(k+1)^3 - k^3\bigr] = 3S_2(n) + 3S_1(n) + n.

The left-hand side telescopes:

k=1n[(k+1)3k3]=(n+1)31.\sum_{k=1}^{n}\bigl[(k+1)^3 - k^3\bigr] = (n+1)^3 - 1.

Substituting S1(n)=n(n+1)/2S_1(n) = n(n+1)/2 from Lemma 1:

(n+1)31=3S2(n)+3n(n+1)2+n3S2(n)=(n+1)313n(n+1)2n.\begin{aligned} (n+1)^3 - 1 &= 3S_2(n) + \frac{3n(n+1)}{2} + n \\[4pt] 3S_2(n) &= (n+1)^3 - 1 - \frac{3n(n+1)}{2} - n. \end{aligned}

Expanding (n+1)3=n3+3n2+3n+1(n+1)^3 = n^3 + 3n^2 + 3n + 1:

3S2(n)=n3+3n2+3n+113n2+3n2n=n3+3n2+2n3n2+3n2.3S_2(n) = n^3 + 3n^2 + 3n + 1 - 1 - \frac{3n^2 + 3n}{2} - n = n^3 + 3n^2 + 2n - \frac{3n^2 + 3n}{2}.

Combining over a common denominator:

3S2(n)=2n3+6n2+4n3n23n2=2n3+3n2+n2=n(2n2+3n+1)2=n(2n+1)(n+1)2,3S_2(n) = \frac{2n^3 + 6n^2 + 4n - 3n^2 - 3n}{2} = \frac{2n^3 + 3n^2 + n}{2} = \frac{n(2n^2 + 3n + 1)}{2} = \frac{n(2n+1)(n+1)}{2},

where the last factorization follows from 2n2+3n+1=(2n+1)(n+1)2n^2 + 3n + 1 = (2n+1)(n+1) (verify: (2n+1)(n+1)=2n2+3n+1(2n+1)(n+1) = 2n^2 + 3n + 1).

Dividing by 3: S2(n)=n(n+1)(2n+1)6S_2(n) = \dfrac{n(n+1)(2n+1)}{6}. \square

Theorem 1 (Closed form for the difference). For all n1n \ge 1,

D(n)=n(n1)(n+1)(3n+2)12.D(n) = \frac{n(n-1)(n+1)(3n+2)}{12}.

Proof. By Lemmas 1 and 2,

D(n)=S1(n)2S2(n)=n2(n+1)24n(n+1)(2n+1)6.D(n) = S_1(n)^2 - S_2(n) = \frac{n^2(n+1)^2}{4} - \frac{n(n+1)(2n+1)}{6}.

Extracting the common factor n(n+1)n(n+1):

D(n)=n(n+1)12[3n(n+1)2(2n+1)].D(n) = \frac{n(n+1)}{12}\bigl[3n(n+1) - 2(2n+1)\bigr].

Justification of the common denominator: n2(n+1)24=3n2(n+1)212\frac{n^2(n+1)^2}{4} = \frac{3n^2(n+1)^2}{12} and n(n+1)(2n+1)6=2n(n+1)(2n+1)12\frac{n(n+1)(2n+1)}{6} = \frac{2n(n+1)(2n+1)}{12}.

Expanding the bracket:

3n(n+1)2(2n+1)=3n2+3n4n2=3n2n2.3n(n+1) - 2(2n+1) = 3n^2 + 3n - 4n - 2 = 3n^2 - n - 2.

We factor the quadratic 3n2n23n^2 - n - 2. The discriminant is Δ=1+24=25\Delta = 1 + 24 = 25, yielding roots

n=1±56,i.e.,n=1  and  n=23.n = \frac{1 \pm 5}{6}, \quad\text{i.e.,}\quad n = 1 \;\text{and}\; n = -\tfrac{2}{3}.

Therefore 3n2n2=3 ⁣(n1) ⁣(n+23)=(n1)(3n+2)3n^2 - n - 2 = 3\!\left(n - 1\right)\!\left(n + \tfrac{2}{3}\right) = (n-1)(3n+2).

Substituting:

D(n)=n(n+1)(n1)(3n+2)12.D(n) = \frac{n(n+1)(n-1)(3n+2)}{12}. \quad\square

Corollary 1 (Non-negativity). D(n)0D(n) \ge 0 for all n1n \ge 1, with equality if and only if n=1n = 1.

Proof. For n2n \ge 2, each factor n,n1,n+1,3n+2n, n-1, n+1, 3n+2 is positive, so D(n)>0D(n) > 0. For n=1n = 1: D(1)=1025/12=0D(1) = 1 \cdot 0 \cdot 2 \cdot 5 / 12 = 0. \square

Theorem 2 (Combinatorial interpretation). The difference D(n)D(n) counts twice the sum of all pairwise products:

D(n)=2 ⁣ ⁣1i<jn ⁣ ⁣ij.D(n) = 2\!\!\sum_{1 \le i < j \le n}\!\! ij.

Proof. By the multinomial expansion of a square,

(i=1ni) ⁣2=i=1ni2+2 ⁣ ⁣1i<jn ⁣ ⁣ij.\left(\sum_{i=1}^n i\right)^{\!2} = \sum_{i=1}^n i^2 + 2\!\!\sum_{1 \le i < j \le n}\!\! ij.

This identity holds because (ai)2=iai2+2i<jaiaj\left(\sum a_i\right)^2 = \sum_i a_i^2 + 2\sum_{i < j} a_i a_j, which is verified by expanding (a1++an)(a1++an)(a_1 + \cdots + a_n)(a_1 + \cdots + a_n) and separating diagonal terms (i=ji = j) from off-diagonal terms (iji \ne j), noting each unordered pair {i,j}\{i, j\} with iji \ne j contributes aiaj+ajai=2aiaja_i a_j + a_j a_i = 2a_i a_j.

Subtracting i2\sum i^2 from both sides yields D(n)=2i<jijD(n) = 2\sum_{i < j} ij. \square

Remark. Theorem 2 makes the non-negativity of D(n)D(n) transparent: it is a sum of products of positive integers.

Editorial

The implementation evaluates the closed formula derived above, so no enumeration is needed. Once nn is given, it substitutes nn into D(n)=n(n1)(n+1)(3n+2)/12D(n) = n(n-1)(n+1)(3n+2)/12 and returns the result. This is sufficient because the algebraic reduction already transformed the original difference of sums into this polynomial expression.

Application to n=100n = 100. We compute

D(100)=100×99×101×30212.D(100) = \frac{100 \times 99 \times 101 \times 302}{12}.

Step by step:

  • 100×99=9,900100 \times 99 = 9{,}900.
  • 101×302=30,502101 \times 302 = 30{,}502.
  • 9,900×30,502=301,969,8009{,}900 \times 30{,}502 = 301{,}969{,}800.
  • 301,969,800÷12=25,164,150301{,}969{,}800 \div 12 = 25{,}164{,}150.

Verification. S1(100)=5,050S_1(100) = 5{,}050 and S1(100)2=25,502,500S_1(100)^2 = 25{,}502{,}500. Also S2(100)=100×101×201/6=338,350S_2(100) = 100 \times 101 \times 201 / 6 = 338{,}350. Then D(100)=25,502,500338,350=25,164,150D(100) = 25{,}502{,}500 - 338{,}350 = 25{,}164{,}150. \checkmark

Pseudocode

Algorithm: Difference Between Square of Sum and Sum of Squares
Require: An integer n ≥ 1.
Ensure: The value D = (∑_{k=1}^n k)^2 - ∑_{k=1}^n k^2.
1: Compute A ← n(n + 1) / 2.
2: Compute B ← n(n + 1)(2n + 1) / 6.
3: Set D ← A^2 - B.
4: Return D.

Complexity Analysis

Theorem 3 (Complexity). The closed-form algorithm computes D(n)D(n) in O(1)O(1) time and O(1)O(1) space.

Proof. The formula D(n)=n(n1)(n+1)(3n+2)/12D(n) = n(n-1)(n+1)(3n+2)/12 requires exactly 4 multiplications and 1 division, all on integers. No loops or data structures are needed. A brute-force approach computing S1(n)S_1(n) and S2(n)S_2(n) by iteration would require O(n)O(n) time and O(1)O(1) space. \square

Answer

25164150\boxed{25164150}

Code

Each problem page includes the exact C++ and Python source files from the local archive.

C++ project_euler/problem_006/solution.cpp
#include <bits/stdc++.h>
using namespace std;

int main() {
    // D(n) = n(n-1)(n+1)(3n+2) / 12
    long long n = 100;
    long long ans = n * (n - 1) * (n + 1) * (3 * n + 2) / 12;
    cout << ans << endl;
    return 0;
}