Proving the Gauss Sum by Induction in Lean
The classic Gauss sum formula states that the sum of the first natural numbers is
An intuitive way to think of it is we fold the list of numbers in half, and each corresponding pair sums to , e.g., , , etc.
Proving that the sum holds is straightforward by induction. For , we have . Now for our induction hypothesis, we grant that the above sum holds for some arbitrary . Then for the sum of the first we have
Substituting the induction hypothesis gives
Multiplying the latter term by and factoring out gives
which is the result when . QED.
When formalizing this in Lean over Nat, dividing by introduces truncating integer division, which makes algebraic manipulation unnecessarily awkward. Multiplying both sides by keeps everything in clean natural-number arithmetic; we also include the case when , so
The Lean formalization below uses a similar structure.
-- Tested with Lean v4.37.0-rc1 / Mathlib
import Mathlib
theorem sum_of_first_n_doubled (n : Nat) :
-- doubled to avoid dealing with halves from dividing by two
2 * (List.range' 0 (n + 1)).sum = n * (n + 1) := by
induction n with
| zero => rfl
| succ n ih =>
-- Break off final list element:
-- `[0, 1, …, n, n + 1] = [0, 1, …, n] ++ [n + 1]`
rw [List.range'_concat]
-- Distribute sum over append and unwrap singleton’s sum
simp only [List.sum_append, List.sum_singleton]
-- Distribute 2 over the addition to expose 2 * sum …
rw [Nat.mul_add]
-- … which `ih` gives as n * (n + 1)
rw [ih]
-- Polynomial normalization to close the equality goal
ring -- or `ring!` but not `omega`
Open the above proof in Lean Web and move your cursor around to see the progression of the tactic states until all goals are closed.
How the Inductive Step Works
- Base case (
zero):List.range' 0 1is[0], whose sum is0, so both sides reduce definitionally to0viarfl. - Peeling off the last term:
List.range'_concatsplits[0, 1, …, n + 1]into[0, 1, …, n] ++ [n + 1]. - Distributing
.sumand2 * _:List.sum_appendandList.sum_singletonturn the list sum into(List.range' 0 (n + 1)).sum + (n + 1), andNat.mul_adddistributes the factor of2so the inductive hypothesisihapplies directly. - Closing the algebra: After
rw [ih], the goal is quadratic inn(n * (n + 1) + 2 * (n + 1) = (n + 1) * (n + 2)), which is whyomega(a linear arithmetic solver) cannot close it, whereas Mathlib’sringnormalizes both polynomials to match.