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 syncApr 19, 2026
Problem#0006
LevelLevel 00
Solved By529,603
LanguagesC++, Python
Answer25164150
Length334 words
algebracombinatoricsarithmetic
Problem statement
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.
where the last factorization follows from 2n2+3n+1=(2n+1)(n+1) (verify: (2n+1)(n+1)=2n2+3n+1).
Dividing by 3: S2(n)=6n(n+1)(2n+1). □
Theorem 1 (Closed form for the difference). For all n≥1,
D(n)=12n(n−1)(n+1)(3n+2).
Proof. By Lemmas 1 and 2,
D(n)=S1(n)2−S2(n)=4n2(n+1)2−6n(n+1)(2n+1).
Extracting the common factor n(n+1):
D(n)=12n(n+1)[3n(n+1)−2(2n+1)].
Justification of the common denominator:4n2(n+1)2=123n2(n+1)2 and 6n(n+1)(2n+1)=122n(n+1)(2n+1).
Expanding the bracket:
3n(n+1)−2(2n+1)=3n2+3n−4n−2=3n2−n−2.
We factor the quadratic 3n2−n−2. The discriminant is Δ=1+24=25, yielding roots
n=61±5,i.e.,n=1andn=−32.
Therefore 3n2−n−2=3(n−1)(n+32)=(n−1)(3n+2).
Substituting:
D(n)=12n(n+1)(n−1)(3n+2).□
Corollary 1 (Non-negativity). D(n)≥0 for all n≥1, with equality if and only if n=1.
Proof. For n≥2, each factor n,n−1,n+1,3n+2 is positive, so D(n)>0. For n=1: D(1)=1⋅0⋅2⋅5/12=0. □
Theorem 2 (Combinatorial interpretation). The difference D(n) counts twice the sum of all pairwise products:
D(n)=21≤i<j≤n∑ij.
Proof. By the multinomial expansion of a square,
(i=1∑ni)2=i=1∑ni2+21≤i<j≤n∑ij.
This identity holds because (∑ai)2=∑iai2+2∑i<jaiaj, which is verified by expanding (a1+⋯+an)(a1+⋯+an) and separating diagonal terms (i=j) from off-diagonal terms (i=j), noting each unordered pair {i,j} with i=j contributes aiaj+ajai=2aiaj.
Subtracting ∑i2 from both sides yields D(n)=2∑i<jij. □
Remark. Theorem 2 makes the non-negativity of 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 n is given, it substitutes n into D(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=100. We compute
D(100)=12100×99×101×302.
Step by step:
100×99=9,900.
101×302=30,502.
9,900×30,502=301,969,800.
301,969,800÷12=25,164,150.
Verification.S1(100)=5,050 and S1(100)2=25,502,500. Also S2(100)=100×101×201/6=338,350. Then D(100)=25,502,500−338,350=25,164,150. ✓
Pseudocode
Algorithm: Difference Between Square of Sum and Sum of SquaresRequire: 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) in O(1) time and O(1) space.
Proof. The formula D(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) and S2(n) by iteration would require O(n) time and O(1) space. □
Answer
25164150
Source code
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;
}
Pythonproject_euler/problem_006/solution.py
"""Project Euler Problem 6: Sum Square Difference"""
def solve(n: int = 100) -> int:
"""D(n) = n(n-1)(n+1)(3n+2) / 12"""
return n * (n - 1) * (n + 1) * (3 * n + 2) // 12
answer = solve()
assert answer == 25164150
print(answer)