TExcelTAD
Home › Discrete Mathematics › Bounding Sums: Integrals, Harmonic Numbers and Stirling

Bounding Sums: Integrals, Harmonic Numbers and Stirling

Many sums have no closed form, but for algorithm analysis a tight estimate is enough. The integral method squeezes a sum between two integrals. It gives the growth of the harmonic numbers Hn=1+12+⋯+1nH_n = 1 + \frac12 + \cdots + \frac1n (behind quicksort, hashing and the coupon collector) and, through ln⁡n!=∑ln⁡i\ln n! = \sum \ln i, Stirling's formula for factorials (behind the nlog⁡nn \log n lower bound for sorting).

The integral method

If ff is increasing on [1,n][1, n], each term f(i)f(i) lies between the area under ff on [i−1,i][i-1, i] and on [i,i+1][i, i+1]. Summing gives

f(1)+∫1nf(x) dx  ≤  ∑i=1nf(i)  ≤  f(n)+∫1nf(x) dxf(1) + \int_1^n f(x)\,dx \;\le\; \sum_{i=1}^{n} f(i) \;\le\; f(n) + \int_1^n f(x)\,dx

For a decreasing ff the same bounds hold with f(1)f(1) and f(n)f(n) swapped:

f(n)+∫1nf(x) dx  ≤  ∑i=1nf(i)  ≤  f(1)+∫1nf(x) dxf(n) + \int_1^n f(x)\,dx \;\le\; \sum_{i=1}^{n} f(i) \;\le\; f(1) + \int_1^n f(x)\,dx

So the sum is the integral up to an error of at most one term.

Harmonic numbers

With f(x)=1xf(x) = \frac{1}{x} (decreasing) and ∫1ndxx=ln⁡n\int_1^n \frac{dx}{x} = \ln n:

1n+ln⁡n≤Hn≤1+ln⁡n\frac{1}{n} + \ln n \le H_n \le 1 + \ln n

More precisely Hn=ln⁡n+γ+12n+O ⁣(1n2)H_n = \ln n + \gamma + \frac{1}{2n} + O\!\left(\frac{1}{n^2}\right), where γ≈0.5772\gamma \approx 0.5772 is Euler's constant. The harmonic numbers grow without bound, but very slowly: H106≈14.39H_{10^6} \approx 14.39.

python · try it yourself
import math

gamma = 0.5772156649015329
H, n = 0.0, 0
for target in [10, 100, 1000, 10 ** 6]:
    while n < target:
        n += 1
        H += 1 / n
    print(f"n={n:8d}  H_n={H:.6f}  ln n + gamma={math.log(n) + gamma:.6f}  "
          f"bounds [{1 / n + math.log(n):.4f}, {1 + math.log(n):.4f}]")
n=      10  H_n=2.928968  ln n + gamma=2.879801  bounds [2.4026, 3.3026]
n=     100  H_n=5.187378  ln n + gamma=5.182386  bounds [4.6152, 5.6052]
n=    1000  H_n=7.485471  ln n + gamma=7.484971  bounds [6.9088, 7.9078]
n= 1000000  H_n=14.392727  ln n + gamma=14.392726  bounds [13.8155, 14.8155]

Book stacking. How far can a stack of nn identical books (length 1) overhang the edge of a table? Placing the top book's centre at the edge of the one below, and so on, the overhang is 12Hn\frac{1}{2} H_n. Since Hn→∞H_n \to \infty, the overhang can be as large as you like, with enough books: Hn≥4H_n \ge 4 needs n=31n = 31, so 31 books give an overhang of 2 book lengths.

Factorials and Stirling's formula

Take logarithms: ln⁡n!=∑i=1nln⁡i\ln n! = \sum_{i=1}^{n} \ln i. With f=ln⁡f = \ln (increasing) and ∫1nln⁡x dx=nln⁡n−n+1\int_1^n \ln x\,dx = n \ln n - n + 1:

nln⁡n−n+1≤ln⁡n!≤nln⁡n−n+1+ln⁡nn \ln n - n + 1 \le \ln n! \le n \ln n - n + 1 + \ln n

Exponentiating, n!n! is between nnen−1\frac{n^n}{e^{n-1}} and nn+1en−1\frac{n^{n+1}}{e^{n-1}}. The precise version is Stirling's formula:

n!∼2πn(ne)nn! \sim \sqrt{2\pi n} \left(\frac{n}{e}\right)^n

The relative error is about 112n\frac{1}{12n}, so it is already within 1% at n=10n = 10.

python · try it yourself
import math

for n in [1, 5, 10, 20, 50]:
    exact = math.factorial(n)
    stirling = math.sqrt(2 * math.pi * n) * (n / math.e) ** n
    print(f"n={n:2d}  n!={exact:.6e}  Stirling={stirling:.6e}  ratio={exact / stirling:.6f}")
print("log2(52!) =", round(math.log2(math.factorial(52)), 2), "bits to describe a shuffled deck")
n= 1  n!=1.000000e+00  Stirling=9.221370e-01  ratio=1.084438
n= 5  n!=1.200000e+02  Stirling=1.180192e+02  ratio=1.016784
n=10  n!=3.628800e+06  Stirling=3.598696e+06  ratio=1.008365
n=20  n!=2.432902e+18  Stirling=2.422787e+18  ratio=1.004175
n=50  n!=3.041409e+64  Stirling=3.036345e+64  ratio=1.001668
log2(52!) = 225.58 bits to describe a shuffled deck

Why sorting needs nlog⁡nn \log n comparisons

A comparison sort must distinguish all n!n! orderings of its input, and each comparison has 2 outcomes, so it needs at least log⁡2n!\log_2 n! comparisons in the worst case. By Stirling, log⁡2n!=nlog⁡2n−nlog⁡2e+O(log⁡n)\log_2 n! = n \log_2 n - n \log_2 e + O(\log n), so every comparison sort needs Ω(nlog⁡n)\Omega(n \log n) comparisons. Merge sort meets this bound.

In software

HnH_n appears in the expected number of comparisons of quicksort (2nHn≈1.39 nlog⁡2n2n H_n \approx 1.39\,n \log_2 n), in the coupon collector problem (about nHnn H_n random draws to see all nn items, important for hash tables and load balancing) and in the expected number of "new maximum" events in a random stream (HnH_n). Stirling estimates the size of search spaces like 52!52! or the number of possible permutations in brute-force algorithms.

Exercises

Exercise 1: Bound a sum of square roots

Use the integral method to bound ∑i=1100i\sum_{i=1}^{100} \sqrt{i}.

Answer

Between 667 and 676 (the true value is about 671.46).

Solution

f(x)=xf(x) = \sqrt{x} is increasing and ∫1100x dx=23(1000−1)=666\int_1^{100} \sqrt{x}\,dx = \frac{2}{3}(1000 - 1) = 666. So the sum is between 1+666=6671 + 666 = 667 and 10+666=67610 + 666 = 676.

python · try it yourself
print(sum(i ** 0.5 for i in range(1, 101)))
671.4629471031477

Exercise 2: Coupon collector

About how many random draws (with replacement) from 100 equally likely stickers do you need on average to collect all 100?

Answer

100H100≈519100 H_{100} \approx 519.

Solution

When you have kk stickers, a new one appears with probability 100−k100\frac{100 - k}{100}, so the wait is 100100−k\frac{100}{100-k} draws on average. Summing over k=0,…,99k = 0, \ldots, 99 gives 100(1100+⋯+11)=100H100≈100⋅5.187=518.7100 \left(\frac{1}{100} + \cdots + \frac{1}{1}\right) = 100 H_{100} \approx 100 \cdot 5.187 = 518.7.

Exercise 3: Comparisons for 10 items

What is the minimum worst-case number of comparisons to sort 10 items, according to the log⁡2n!\log_2 n! bound?

Answer
Solution

10!=362880010! = 3628800 and log⁡23628800≈21.79\log_2 3628800 \approx 21.79, so at least 22 comparisons are needed (and 22 are in fact enough for 10 items).

Test yourself

How fast do the harmonic numbers HnH_n grow?

  1. Like nn
  2. Like ln⁡n\ln n
  3. They converge to 2
  4. Like n\sqrt{n}

Stirling's formula approximates n!n! by…

  1. nnn^n
  2. 2πn(n/e)n\sqrt{2\pi n} (n/e)^n
  3. 2n2^n
  4. ene^n

Why does every comparison sort need Ω(nlog⁡n)\Omega(n \log n) comparisons?

  1. Because of cache misses
  2. It must distinguish n!n! orderings, and log⁡2n!=Θ(nlog⁡n)\log_2 n! = \Theta(n \log n)
  3. Because merging is slow
  4. It doesn't