quicksort avg, harmonic costs — no elementary form
$\displaystyle\sum_{i=1}^{n}\dfrac{1}{i(i+1)}$
$\dfrac{n}{n+1}$
telescoping showcase
Series families at a glance
Arithmetic
Geometric
Step rule
add $d$ (common difference)
multiply by $r$ (common ratio)
Term
$a + i d$
$a r^i$
Sum of $n$ terms
$n\cdot\dfrac{\text{first}+\text{last}}{2}$
$a\,\dfrac{r^n-1}{r-1}$
Derivation trick
write forwards+backwards, add
multiply by $r$, subtract
Infinite version
diverges (unless $d=0$)
converges iff $\lvert r\rvert<1$, to $\dfrac{a}{1-r}$
Decision aid — "how do I evaluate this sum?"
Is the summand constant? → $c\,(n-m+1)$. Done.
Linear in $i$ ($a+id$)? → arithmetic series: $n\cdot\frac{\text{first}+\text{last}}{2}$.
Of the form $a r^i$? → geometric series: $a\frac{r^n-1}{r-1}$ (watch for $r=1$ → $an$).
A power $i^k$? → use the catalog (or derive via telescoping a $(k{+}1)$-power difference).
Summand a consecutive difference $t_i - t_{i-1}$? → telescope to $t_n - t_0$.
None of the above? → split by linearity into pieces you do know, then reassemble.
Still stuck / need a bound? → recognize $\sum 1/i = H_n = \Theta(\log n)$, or estimate growth.
Algorithm-analysis patterns (the CS payoff)
Loop shape
Step-count sum
Closed form
Growth
for i in range(n): for j in range(n):
$\sum_{i=0}^{n-1} n$
$n^2$
$\Theta(n^2)$
for i in range(n): for j in range(i+1,n):
$\sum_{i=0}^{n-1}(n{-}1{-}i)$
$\frac{n^2-n}{2}$
$\Theta(n^2)$
i=1; while i<n: i*=2
$\sum_{j=0}^{k-1} 1,\ k\approx\log_2 n$
$\log_2 n$
$\Theta(\log n)$
doubling work per pass
$\sum_{j=0}^{k-1} 2^j$
$2^k-1 < 2n$
$\Theta(n)$
linear loop × logarithmic loop
$\sum_{i=0}^{n-1}\log_2 n$
$n\log_2 n$
$\Theta(n\log n)$
Master move: running time = sum of per-pass work → shift the index to a known sum → collapse →
keep the leading term (Big-O discards constants and lower-order terms — formalized in Chapter 14).
Factorial growth (read as "astronomically large")
$$\text{const} \prec \log n \prec n \prec n\log n \prec n^2 \prec 2^n \prec n! \prec n^n$$
$n!$ overtakes $2^n$ between $n=3$ and $n=4$; it beats every fixed-base $c^n$ eventually
(since $n! \ge (n/2)^{n/2}$, a growing base).
Never enumerate $n!$ things for $n \gtrsim 12$: $12!\approx4.8\times10^8$, $20!\approx2.4\times10^{18}$.
Overflow: $13!$ exceeds signed 32-bit; $21!$ exceeds 64-bit. (Python ints are arbitrary-precision but
still slow/huge.)
Common pitfalls
Pitfall
Fix
Translating $\sum_{i=m}^{n}$ as range(m, n)
Use range(m, n + 1) — upper limit is inclusive.
$\sum_{i=1}^{n} c = c$
It's $c\,n$ — a constant is summed once per term.
Forgetting the starting index
Always state it; verify a closed form against the first 2–3 terms.
$0! = 0$
$0! = 1$ (empty product / one way to order nothing).
Using geometric formula when $r=1$
Division by zero; handle $r=1$ separately ($\sum = an$).
Expecting every sum to have a tidy closed form
$H_n$ doesn't — know its growth $\Theta(\log n)$ instead.
Index-shift without compensating
Shift limits and rewrite the summand; the terms must not change.
Toolkit additions (dmtoolkit/recurrences.py, continued from Ch. 6)
Pair each closed form with check_identity (Ch. 6) against summation to test it before trusting
it — evidence, then (via the proofs in 11.3) certainty.
One-sentence techniques to carry forward
Arithmetic: fold the sum onto itself (forwards + backwards), every column is first + last.
Geometric: multiply by $r$ and subtract; the middle cancels.
Telescoping: rewrite the summand as a consecutive difference; only the endpoints survive — and
this derives power-sum formulas, it doesn't just verify them.
Closed form vs. recurrence: the same sequence; closed form is $O(1)$ random access, a recurrence
is $O(n)$ sequential — converting one to the other is the work of Chapters 18–19.
We use cookies to improve your experience and show relevant ads. Privacy Policy