Skip to content

Discrete Math and Proof

Summations, series, and asymptotic sums

Adding many terms and estimating the total

Every time you ask "how many steps does this loop take?", you are secretly adding up a series. A loop that does units of work on its -th pass costs - and knowing that this equals is the difference between guessing "it's a loop, so roughly " and knowing "it's ." Summations are the arithmetic layer directly under Big-O.

The picture to hold in your head

A summation is a for-loop written in one line. The Greek is the loop; the little numbers say where the counter starts and stops; the expression to its right is the body that gets added each pass. The whole prize of this lesson is the closed form - a formula with no loop left in it, so you can read off the answer (and its Big-O) in a single glance instead of running the loop in your head.

Sigma: a loop you can solve on paper

The notation means "let walk from to , and add up each time." So . That is just running the loop. The magic is that many loops collapse into a formula. The most famous - the one legend says a young Gauss found in seconds - pairs the first term with the last, the second with the second-last, and so on:

Each of the terms pairs up to make , and there are such pairs - hence . The leading term is , so a nested loop that runs this many times is . That single fact is why summations and Big-O are the same subject seen from two angles.

The four closed forms worth memorising

Arithmetic (a fixed step each term) - sum grows quadratically:

Geometric (a fixed ratio each term) - the workhorse of divide-and-conquer:

Harmonic - the slow one; it diverges, but only as fast as a logarithm:

Watch the sum build itself

Time to see these grow. Below, each term is drawn as a bar; every new amber slice is the term just added, and the whole column's height is the running total . The blue line is the closed form - and the key "aha" is that every column top lands exactly on it. Pick a family, drag the number of terms, and in geometric mode drag the ratio : below the bars shrink and the total glides up to a ceiling; at or above they never stop growing.

Sigma-notation playground∑ i · arithmetic
the term aᵢ just addedsum of earlier termsclosed-form curve Sₙ
terms n8
running total Sₙ36
n(n+1)/236

∑ i from 1 to 8 = 36 = n(n+1)/2 - a nested loop over this is O(n²).

The aha: shrinking terms can still add to a finite total

Switch to geometric and set . The bars halve each step - - and the running total climbs toward, but never passes, the dashed ceiling at . That is convergence: infinitely many terms, one finite sum. Now drag up past . Each term is now bigger than the last, the ceiling vanishes, and the total diverges to infinity. The whole fate of the series hinges on one number: whether is below .

Telescoping, harmonic, and the integral squeeze

Not every sum needs a memorised formula. A telescoping sum is one where each term splits into two pieces that cancel their neighbours, so the whole thing collapses to just the ends. Watch the middle evaporate:

The from the first term cancels the of the second, and so on down the line - only the very first and very last survive. This trick underlies more running-time proofs than you'd expect.

The harmonic series has no tidy closed form, so we bound it instead. Because is a smooth decreasing curve, the sum of the rectangles under it is trapped between two integrals - this is the light-touch idea of bounding a sum with an integral:

Both walls are plus a constant, so . In the demo, harmonic mode draws exactly this: the staircase of bars hugs the smooth curve. The series does diverge - add enough terms and it exceeds any number - but so slowly that reaching a sum of just takes over million terms. That gentle logarithmic creep is precisely the cost of the balanced-tree and heap operations you have already met.

from math import log

n = 8
# arithmetic: 1 + 2 + ... + n
print(sum(range(1, n + 1)))          # 36  (the loop)
print(n * (n + 1) // 2)             # 36  (O(1) formula)

# geometric: 1 + r + r^2 + ... (n terms)
r = 0.5
print(sum(r**i for i in range(n)))    # 1.9921875
print((1 - r**n) / (1 - r))         # 1.9921875  (same)

# harmonic grows like ln n
print(sum(1/i for i in range(1, n+1)))  # 2.7179
print(log(n) + 0.5772)              # 2.6566  (H_n ≈ ln n + γ)
Closed forms beat loops - same answer, no iteration

Check yourself

A loop nested inside a loop, where the inner one runs i times on the i-th outer pass, does total work:

For a positive ratio r, the infinite geometric sum ∑ rⁱ (i from 0 to ∞) reaches a finite total only when:

Recall: the closed form for ∑ i from 1 to n, and why it forces a nested loop to be O(n²).

Try to state it, then check.

Primary source

MIT's 6.042J - Mathematics for Computer Science devotes full units to sums and series: Gauss's pairing trick, geometric and harmonic sums, telescoping, and bounding sums with integrals, all built for CS. For where these sums come from - the running-time recurrences of real algorithms - see 6.006 - Introduction to Algorithms.

Ask your teacher

Here is the connection worth carrying forward: summations are Big-O analysis (Lesson 5). Counting the work of a loop always means summing a series - and a nested loop is nothing more than . Master the closed forms and Big-O stops being memorised trivia and becomes something you can derive. Ask me how the geometric series explains why merge sort's collapses to , or how the harmonic series pins down the cost of building a balanced tree.