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
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.
∑ 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
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 + γ)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
Ask your teacher