Math Cheat Sheet¶
Quick-reference formulas that come up again and again when analysing algorithms by hand. Each entry gives the formula, where it tends to appear, and a small example.
At a glance¶
| Name | Formula | Growth |
|---|---|---|
| Arithmetic series | \(\displaystyle\sum_{i=1}^{n} i = \frac{n(n+1)}{2}\) | \(\Theta(n^2)\) |
| Sum of squares | \(\displaystyle\sum_{i=1}^{n} i^2 = \frac{n(n+1)(2n+1)}{6}\) | \(\Theta(n^3)\) |
| Sum of cubes | \(\displaystyle\sum_{i=1}^{n} i^3 = \left(\frac{n(n+1)}{2}\right)^2\) | \(\Theta(n^4)\) |
| Geometric series | \(\displaystyle\sum_{i=0}^{n} r^i = \frac{r^{n+1} - 1}{r - 1}\) | \(\Theta(r^n)\) for \(r > 1\) |
| Powers of two | \(\displaystyle\sum_{i=0}^{n} 2^i = 2^{n+1} - 1\) | \(\Theta(2^n)\) |
Summation rules¶
These three rules are what let you reduce an unfamiliar sum to one of the formulas on this page.
Pull out a constant factor. Anything that does not depend on the index can move outside the sum:
Split a sum of terms. A sum of a sum is the sum of the sums:
Sum a constant. Adding the same value \(n\) times:
Example
Arithmetic series¶
The sum of the first \(n\) positive integers:
More generally, any sequence that rises by a fixed difference \(d\) sums to the number of terms times the average of the first and last term:
Where it shows up: nested loops where the inner loop runs \(i\) times (insertion sort, bubble sort, comparing every pair), and any process whose cost per step grows by a fixed amount, such as a dynamic array with additive growth.
Example
\(1 + 2 + \dots + 100 = \dfrac{100 \cdot 101}{2} = 5050\)
Sum of squares¶
Where it shows up: triple-nested loops, and loops whose body costs \(\Theta(i^2)\) on the \(i\)-th iteration.
Example
\(1^2 + 2^2 + 3^2 + 4^2 = \dfrac{4 \cdot 5 \cdot 9}{6} = 30\)
Sum of cubes¶
The sum of the first \(n\) cubes is the square of the sum of the first \(n\) integers.
Example
\(1^3 + 2^3 + 3^3 + 4^3 = \left( \dfrac{4 \cdot 5}{2} \right)^2 = 10^2 = 100\)
The pattern
Summing a power raises the exponent by one. For any fixed \(p \ge 0\):
with leading term \(\dfrac{n^{p+1}}{p+1}\). That is often all a complexity analysis needs.
Sums that start at index 0¶
The formulas above start at \(i = 1\), but loops usually start at \(0\). There are two ways to bridge the gap.
Peel off the \(i = 0\) term. Take the first term out and the rest of the sum starts at 1:
For powers of \(i\) the peeled term is zero, so the sum is unchanged:
Watch the upper limit. A loop for i in range(n) runs \(i\) from \(0\) to
\(n - 1\), not to \(n\). Substitute \(n - 1\) for \(n\) in the formula:
Shift the index. Substituting \(j = i + 1\) moves the whole range up by one. The limits go up by one and every \(i\) inside becomes \(j - 1\):
Example
A sum with a non-zero first term, starting from 0:
Sums that start anywhere. A sum from \(a\) to \(b\) has \(b - a + 1\) terms, and is the difference of two sums that start at 1:
Geometric series¶
Each term is the previous one times a fixed ratio \(r \ne 1\):
The case that matters most is \(r = 2\):
In words: a sum of doubling terms is just under twice its last term. When \(r > 1\), the last term dominates and the whole sum is \(\Theta(r^n)\).
When \(|r| < 1\) the terms shrink, and even the infinite sum is finite:
Where it shows up: anything that doubles or halves. Resizing a dynamic array by doubling, the number of nodes in a complete binary tree, and the total work of algorithms that halve their input at each step.
Example
A complete binary tree with levels \(0\) to \(h\) has \(1 + 2 + 4 + \dots + 2^h = 2^{h+1} - 1\) nodes.