Sums and Closed Forms
Analysing an algorithm usually ends in a sum: the work of a loop is the sum of the work of its iterations. A closed form replaces the sum by a formula you can evaluate at once, such as . This lesson collects the closed forms every programmer should know and three methods to find new ones: the perturbation method, guess-and-verify, and derivatives of known sums.
The essential closed forms
| Sum | Closed form |
|---|---|
| (geometric, ) | |
| () | |
| () |
Each is proved by induction once you know it. Finding it is the creative part.
The perturbation method
Let . Multiply by ("perturb" the sum): . Subtracting, almost everything cancels:
The same idea works for , and for the money question: a loan of repaid by yearly payments at interest rate . Paying in years is worth today, so
from fractions import Fraction
def geometric(x, n):
return (1 - x ** n) / (1 - x)
x, n = Fraction(1, 3), 10
print("direct sum:", sum(x ** i for i in range(n)), " closed form:", geometric(x, n))
# loan: how much can you borrow with 10 yearly payments of 1000 at 5% interest?
r, p, n = 0.05, 1000, 10
print("by summing:", round(sum(p / (1 + r) ** k for k in range(1, n + 1)), 2))
print("closed form:", round(p * (1 - (1 + r) ** -n) / r, 2))direct sum: 29524/19683 closed form: 29524/19683
by summing: 7721.73
closed form: 7721.73
Guess and verify
When a sum looks polynomial, guess its degree and fit coefficients. grows like , so guess a cubic , fit it to , then prove the result by induction. SymPy automates both steps:
from sympy import symbols, summation, factor, simplify
i, n, x = symbols("i n x", positive=True, integer=True)
print("sum i =", factor(summation(i, (i, 1, n))))
print("sum i^2 =", factor(summation(i ** 2, (i, 1, n))))
print("sum i^3 =", factor(summation(i ** 3, (i, 1, n))))
print("sum i*2^i =", simplify(summation(i * 2 ** i, (i, 1, n))))sum i = n*(n + 1)/2
sum i^2 = n*(n + 1)*(2*n + 1)/6
sum i^3 = n**2*(n + 1)**2/4
sum i*2^i = 2*2**n*n - 2*2**n + 2
The last closed form, , appears in the analysis of building a binary heap.
Derivatives of sums
Differentiating a known sum gives new ones. From , differentiate and multiply by :
This sum shows up when computing the expected number of coin flips until the first head (it is 2) and the total work of a heap build (, not ).
Double sums
Nested loops give double sums. Swapping the order of summation often helps:
which is the number of iterations of for i in range(1, n + 1): for j in range(1, i + 1):. For with harmonic numbers, swapping gives .
In software
Closed forms turn loop costs into Big-O: a triangular double loop does iterations; a loop that doubles its step runs times; a dynamic array that doubles its capacity copies at most elements in total, so appends are amortized.
Exercises
Exercise 1: Triangle loop
How many times does the inner statement run? for i in range(n): for j in range(i, n): work()
Answer
.
Solution
For each the inner loop runs times: .
Exercise 2: Geometric sum
Compute with the closed form.
Answer
Solution
Exercise 3: Doubling array (code)
A dynamic array starts with capacity 1 and doubles whenever it is full. Count the element copies needed to append items and compare with .
Answer
1023 copies, less than .
Solution
Resizes happen at sizes , copying elements.
cap, size, copies = 1, 0, 0
for _ in range(1000):
if size == cap:
copies += size
cap *= 2
size += 1
print(copies, 2 * 1000)1023 2000
Test yourself
What is ?
- 5000
- 5050
- 10000
- 10100
What is ?
- 1
- 1.5
- 2
- infinite
The perturbation method for computes…