Why Summations Are the Currency of Algorithm Analysis
Almost every running-time analysis you will write in this book ends with a summation. The reason is structural: a loop that runs times and does work on iteration costs exactly primitive operations. Nested loops produce nested sums. Recursive calls unfolded into a recursion tree produce a sum over levels. Amortized analyses produce sums of credit. The whole subject of analysis of algorithms is, at one level, the systematic study of which sums can be replaced by closed forms — expressions you can evaluate in arithmetic ops instead of .
Consider the simplest possible non-trivial loop:
1total = 0
2for i in range(1, n + 1):
3 total += i
4return totalThe cost (counting the addition as one unit) is operations, and the output the loop computes is . Both halves of that sentence are summations. The cost summation gives us the running time; the value summation gives us a closed form — — that lets us replace the entire loop with a single arithmetic expression. Knowing summation identities is what lets you collapse code into code, and what lets you simplify a four-page running-time derivation into a two-line statement.
Working slogan. Loops produce sums. Recursion produces sums. Probabilistic analyses produce sums. Master a dozen summation identities and you have already mastered most of the algebra you will do in this book.
Sigma Notation: A Compact Vocabulary
The capital sigma notation packs a loop into one symbol. The expression
has four moving parts: the index , the lower bound 1, the upper bound , and the body . The bounds are inclusive on both ends: the sum has exactly terms when the index runs from 1 to .
Three identities do almost all of the algebraic work you will ever do with sigma notation:
- Linearity. for any constants . Constants slide out of sums; sums of sums split.
- Splitting at an index. for any . Useful for recursion tree analyses where different levels behave differently.
- Reindexing. (shift by 1), or more generally . This is the "rename the loop variable" identity.
A constant is just a sum of constants
The Arithmetic Series: Gauss's Trick
Legend: in the 1780s, a German schoolmaster tried to keep a class of ten-year-olds quiet by asking them to add the integers from 1 to 100. Carl Friedrich Gauss handed in the answer in seconds: . He had spotted that the sum could be paired up:
The same pairing works for any :
This is the most-used closed form in algorithm analysis. It appears every time a nested loop has the shape for i in range(n): for j in range(i):, because the inner loop runs times in total. Any time you see a half-triangle of work, you should instantly think .
A second proof: induction
Pairing is satisfying but only a sketch. Induction makes the same identity airtight. Base. For : LHS , RHS . Step. Assume . Then
which is the formula with replaced by . QED.
Sum of Squares, Sum of Cubes, and Faulhaber
For squared and cubed indices the closed forms are:
That last identity — the sum of cubes equals the square of the sum — is one of the small miracles of elementary number theory. It is also genuinely useful: it converts a cubic-looking nested-loop bound into a clean square.
Deriving the sum of squares by telescoping
A beautiful trick: take the algebraic identity and sum both sides from to . The left side telescopes (almost everything cancels):
The right side, by linearity of , equals . We already know . Plug in and solve for and out pops the formula. A five-line proof for an identity that, written out, looks intimidating.
The general pattern: Faulhaber
The pattern continues: is always a polynomial of degree in , with leading term . Faulhaber's 17th century formula gives the explicit polynomial. For algorithm analysis, the only fact you usually need is the asymptotic statement:
Why this matters in practice
The Geometric Series: Three Regimes
After the arithmetic series, the geometric series is the second load-bearing identity in algorithms. For any :
Proof. Let . Then . Subtract: . Divide. Two lines.
The behavior of this series splits sharply into three regimes, and which regime you are in dominates the asymptotic class:
| Regime | Closed form | Asymptotic | Where you see it |
|---|---|---|---|
| r < 1 (decreasing) | 1/(1-r) − r^n/(1-r) → 1/(1-r) | Θ(1) (bounded by a constant!) | Geometric backoff, decaying probabilities, fixed-point iteration |
| r = 1 (flat) | n | Θ(n) | Trivial case: just n copies of 1 |
| r > 1 (increasing) | (r^n − 1)/(r − 1) ≈ r^n/(r−1) | Θ(r^n) — last term dominates | Recursion-tree levels in T(n) = 2T(n/2) + n^2; doubling arrays |
The decreasing case is the source of one of the most counterintuitive results in algorithm design: a sum with infinitely many positive terms can still be a constant. is the canonical example. This is what makes amortized analysis of dynamic-array doubling work: copying happens infinitely often as the array grows, but the total copy cost over appends is bounded by a constant times , because the sequence of resize costs is geometric.
The dominant-term rule
The Harmonic Series and the Logarithm Hidden Inside
The harmonic numbers are the partial sums
Unlike the previous sums, has no clean closed form in elementary functions. But its asymptotic behavior is known with great precision:
where is the Euler-Mascheroni constant. For algorithm analysis the only thing that usually matters is the leading-order statement:
Why is the harmonic sum logarithmic? The integral argument
The continuous analogue of the discrete sum is the integral . Because is decreasing, the staircase of rectangles of height sandwiches the area under the curve from both sides. We get the two-sided bound
which already gives with no further work.
Where the harmonic sum hides in algorithms
- Quicksort average case. The expected number of comparisons satisfies a recurrence whose solution involves ; that is exactly why average-case quicksort is , not .
- Coupon collector. The expected number of trials to collect all coupons is . Used in load-balancer analysis, randomized algorithms, hashing.
- Hash chains. Expected length of the longest chain in a hash table with keys and buckets is — an analysis that uses harmonic-sum bounds.
- DCG ranking metric. Search engines and recommenders discount the -th rank by . Total ideal-DCG sums to a harmonic-like quantity.
- Disk seek scheduling, skip lists, treaps. Many analyses end with and conclude .
Telescoping Sums
A telescoping sum is a sum whose body can be written as . When summed, all interior terms cancel:
We already used this once (for the sum of squares). It is the discrete analogue of the fundamental theorem of calculus, and it is the cleanest way to evaluate a sum without guessing an answer first.
Worked example: a small partial-fraction telescope
Compute . Partial fractions: . So
Three lines, no induction, exact closed form. Telescoping is the algebra-level shortcut you should reach for whenever you see a difference structure.
Bounding Sums by Integrals
When no closed form exists, you can still pin down the asymptotic class of a sum by bounding it above and below with integrals. For a monotone function on :
If is increasing, If is decreasing, the inequalities flip:
This is, hands down, the most useful technique for asymptotic bounds when you cannot find a closed form. Examples:
- . That single line is the analysis of comparison-sort lower bounds and of (Stirling).
- .
- is bounded by — constant! In fact it converges to (the Basel problem).
A discrete-vs-continuous reflex
Interactive: The Summation Estimator
Pick a summation type, slide , and watch the amber staircase (the brute-force loop's running cumulative total) meet the blue dashed curve (the closed-form formula evaluated at the same ). They land at the same point because the closed form is, by construction, equal to the loop — just evaluable in instead of arithmetic.
Things to notice as you flip between the choices:
- Σ i grows like a parabola; the staircase hugs the quadratic curve.
- Σ 2^i shoots straight up: only the last bar matters — this is the "dominant-term rule" for increasing geometric series, visible to the eye.
- Σ (1/2)^i levels off at 2. You can crank to 100 and it stays bounded — that is what looks like for an infinite sum.
- Σ 1/i grows but very slowly. The shape is , just shifted up by .
Closed Form vs. Loop, in Two Languages
When you have a closed form, evaluating it is — a handful of arithmetic operations no matter how big is. The loop version is . For , that is the difference between a nanosecond and a second. Below we compute three sums both ways and assert they agree.
Python: side-by-side closed form vs. loop
TypeScript: same identities, BigInt where needed
The same idea in TypeScript — the closed-form path stays ; only the loop scales with . We switch to BigInt for the geometric sum because 2^n overflows JS's 253 safe-integer range past .
The point of two languages, again
Three Worked Algorithm-Cost Calculations
Time to apply the machinery. Three classical algorithms, three different summations, three sharply different asymptotic conclusions.
1. Insertion sort: an arithmetic-series argument for Θ(n²)
Insertion sort, in its worst case, scans backwards through the sorted prefix to find the right slot for the next element. Iteration (with from to ) does up to comparisons. Total worst-case comparison count:
Two lines, no hand-waving. The arithmetic-series identity converted a loop count into a clean asymptotic. Insertion sort is in the worst case — a fact that justifies why merge sort, quicksort, and heapsort exist.
2. Building a heap: top-down vs. bottom-up
A binary heap of elements can be built two ways. The naive way: insert elements one at a time, each insertion sifting up at most levels:
We used the integral bound (or equivalently Stirling's formula for ).
The clever way (Floyd's heap construction): start from the bottom and sift down. A node at height does at most work, and there are about nodes at height . Total work:
The inner sum is bounded by the closed form (a standard arithmetico-geometric series — differentiate the geometric formula). So . We just turned an algorithm into an algorithm by reordering the work and invoking a bounded geometric-like series. This is the actual reason heapify in the standard library uses bottom-up.
3. Tower of Hanoi: a geometric series for 2ⁿ − 1
Tower of Hanoi with disks satisfies the recurrence with . Unfolding:
The geometric-series identity took us from a recurrence to a closed form in one line. is exact, not asymptotic — for 64 disks the famous answer is moves. The same technique handles (merge sort: gives ) and any divide-and-conquer recurrence in the Master Theorem family.
Where These Sums Show Up in Real Systems
These are not classroom curiosities. Each summation identity is doing work in production code right now.
| System / domain | Sum that appears | What it tells you |
|---|---|---|
| Quicksort (numpy.sort, std::sort, V8 Array.sort fallback) | Average comparisons ≈ 2 n H_n | Average runtime is Θ(n log n), not Θ(n) |
| Dynamic-array doubling (Python list, C++ vector, Java ArrayList) | Total resize cost = Σ 2^i ≤ 2 · 2^k = O(n) | Amortized append is O(1) |
| Hash-table chain analysis | Expected chain length involves H_n | Average lookup is O(1); worst-case chain is Θ(log n / log log n) |
| TCP exponential backoff / Ethernet CSMA | Σ 2^i wait slots up to ceiling | Total wait grows geometrically until cap; expected collisions stay bounded |
| Recommendation ranking (DCG metric) | Σ rel_i / log_2(i+1) | Discount factor ≈ 1/log i caps top-k contribution; ties to harmonic asymptotic |
| Compound interest, mortgage amortization | Σ (1+r)^i annuity factor | Geometric series gives exact closed-form payment formula |
| PageRank power iteration | Convergence rate Σ λ_2^k | Geometric → number of iterations is O(log(1/ε)/log(1/λ_2)) |
| Skip lists / treaps | Expected height = H_n | O(log n) search/insert with high probability |
| Bloom filter false-positive rate | (1 − e^{−kn/m})^k via lim Σ identity | Optimal hash count k = (m/n) ln 2 |
| Coupon collector (load balancers, ML data sampling) | Σ n/(n−i+1) = n H_n | Need Θ(n log n) random draws to see all n items |
| Numerical integration (trapezoid rule) | Σ f(x_i) Δx | The sum IS the algorithm |
| Computer graphics LOD / mipmap memory | Σ (1/4)^i image size | Mipmap pyramid uses ≤ 4/3 of original memory (geometric, r = 1/4) |
Notice how often the same three or four series — arithmetic, geometric, harmonic, and their integrals — reappear. The world of algorithms is mostly built out of a small toolkit; you are not learning a list of formulas, you are learning the few that the rest of the field actually uses.
Pitfalls and Sanity Checks
A short list of mistakes that everyone makes once. The point is to make them once and never again.
- Off-by-one in the bounds. has terms, not . , but . Always count by subtracting endpoints and adding one.
- Forgetting to multiply a constant by . , not . Easy to drop in the middle of a longer derivation.
- Confusing geometric with arithmetic. is arithmetic (linear terms, ); is geometric (exponential terms, ). The position of the matters.
- Mistaking a divergent sum for a constant. The harmonic sum diverges: . It is as a partial sum, but it is not bounded. Contrast with , which is bounded.
- Treating as "goes to zero so I can drop it" when working out exact closed forms. It does asymptotically, but for finite a sum like is not exactly 2.
- Double-counting when splitting an index. If you split as , the ranges must be disjoint. would include twice.
Always sanity-check at small n
Quick Check
Compute Σᵢ₌₁¹⁰⁰ (3i + 2) using linearity and the arithmetic-series formula.
Quick Check
Which is the correct asymptotic class for Σᵢ₌₁ⁿ log i?
We now have the algebraic vocabulary — sigma notation, arithmetic and geometric and harmonic identities, telescoping, integral bounds — needed to convert almost any loop or recurrence into a clean asymptotic statement. In the next section we extend the same toolkit to recurrences directly, deriving the Master Theorem, which compresses the three-way comparison "leaves vs. internal vs. work per level" into a single recipe. Every recurrence we solve there will dissolve into one of the summations we just learned to evaluate.