The Integral Test and Estimates of Sums
When the terms of a series come from a positive, decreasing function, the series and the corresponding improper integral converge or diverge together. The proof is a picture of rectangles, and the same picture gives two-sided error bounds for the tail, which is how you decide how many terms a computation needs. It also explains why harmonic numbers, , appear all over algorithm analysis.
For the summary table of tests see Convergence Tests; improper integrals are in Improper Integrals.
The test
Integral Test. Let be continuous, positive and decreasing on , and . Then converges if and only if converges.
Proof. On the decreasing satisfies . Integrate over the interval and add for :
The left sum is the area of rectangles of height standing to the left of each integer , which sit under the curve; the right sum uses rectangles of height , which sit above it.
- If converges, the left inequality gives for every . The partial sums increase (positive terms) and are bounded, so they converge by the Monotone Convergence Theorem from Sequences.
- If , the right inequality gives , so the series diverges. ∎
The conditions only need to hold eventually (for some ), since the first finitely many terms never affect convergence.
Note: The test says nothing about the value: while . The integral is only a yardstick.
-series
For with (for the terms do not tend to 0):
So converges iff . The borderline is delicate: diverges (), while converges ( is finite). Both are invisible to the ratio test.
Estimating the remainder
Let and be the tail. The same rectangles, shifted to start at , give
and adding throughout traps the true sum in an interval:
The midpoint of that interval is a much better estimate than alone. For , :
import math
from scipy.special import zeta
S_true = float(zeta(3)) # ζ(3) = ∑ 1/n³ ≈ 1.2020569
for n in (5, 10, 100):
s = sum(1 / k**3 for k in range(1, n + 1))
lo = s + 1 / (2 * (n + 1)**2)
hi = s + 1 / (2 * n**2)
mid = (lo + hi) / 2
print(f"n={n:3d} s_n={s:.7f} [{lo:.7f}, {hi:.7f}] mid error {abs(mid - S_true):.1e} s_n error {S_true - s:.1e}")
print(f"ζ(3) = {S_true:.7f}")n= 5 s_n=1.1856620 [1.1995509, 1.2056620] mid error 5.5e-04 s_n error 1.6e-02
n= 10 s_n=1.1975320 [1.2016642, 1.2025320] mid error 4.1e-05 s_n error 4.5e-03
n=100 s_n=1.2020074 [1.2020564, 1.2020574] mid error 4.9e-09 s_n error 5.0e-05
ζ(3) = 1.2020569
With 10 terms the midpoint is already as accurate as about 110 terms of the raw sum.
How many terms? To guarantee for it is enough that , i.e. , so terms.
Harmonic numbers and Euler's constant
Let . The rectangle inequalities with read , so grows like . More precisely, let . Then
so is decreasing, and means it is bounded below. By the Monotone Convergence Theorem it converges; the limit is the Euler–Mascheroni constant . Thus
import math
gamma = 0.5772156649015329
H = 0.0
for n in range(1, 10**6 + 1):
H += 1 / n
if n in (10, 1000, 10**6):
print(f"n={n:>7d} H_n={H:.8f} ln n + γ={math.log(n) + gamma:.8f} diff={H - math.log(n) - gamma:.2e} 1/(2n)={1 / (2 * n):.2e}")n= 10 H_n=2.92896825 ln n + γ=2.87980076 diff=4.92e-02 1/(2n)=5.00e-02
n= 1000 H_n=7.48547086 ln n + γ=7.48497094 diff=5.00e-04 1/(2n)=5.00e-04
n=1000000 H_n=14.39272672 ln n + γ=14.39272622 diff=5.00e-07 1/(2n)=5.00e-07
A million terms of the harmonic series add up to only about 14.4: divergent, but extremely slowly.
In software
- Quicksort: the expected number of comparisons is . The appears because elements and are compared with probability .
- Coupon collector: collecting all distinct items when each draw is uniform takes draws on average; this sizes hash-based sampling, randomized testing and "how many requests until every shard is hit".
- Hashing and caches: harmonic sums also show up in the analysis of linear probing, of the "record breaking" count in an online maximum (expected updates) and of LRU hit rates under Zipf-distributed traffic.
- Stopping rules: the remainder bound turns "sum until the terms look small" into a guaranteed tolerance. Summing until looks converged but is still growing like . See time complexity for the big-O side.
Exercises
Exercise 1: Apply the test
Use the integral test to decide whether converges.
Answer
It diverges.
Solution
is positive, continuous, and decreasing for ().
Exercise 2: Borderline series
For which does converge?
Answer
Exactly for .
Solution
Substitute , :
which is a -integral: finite iff . The function is positive and decreasing for , so the integral test applies.
Exercise 3: How many terms?
How many terms of guarantee a remainder below ? Check the actual error, given .
Answer
terms suffice (bound ); the actual error is about .
Solution
import math
n = 694
s = 0.0
for k in range(n, 0, -1): # add small terms first for accuracy
s += 1 / k**4
print(f"bound {1 / (3 * n**3):.3e} actual {math.pi**4 / 90 - s:.3e}")bound 9.972e-10 actual 9.951e-10
Exercise 4: Coupon collector
Simulate collecting coupon types (seeded) over 2000 trials and compare the average number of draws with .
Answer
; the simulated mean is close to it (about 223.6).
Solution
import numpy as np
rng = np.random.default_rng(7)
n, trials = 50, 2000
H = sum(1 / k for k in range(1, n + 1))
counts = []
for _ in range(trials):
seen, draws = set(), 0
while len(seen) < n:
seen.add(int(rng.integers(n)))
draws += 1
counts.append(draws)
print(f"n·H_n = {n * H:.2f}, simulated mean = {np.mean(counts):.2f}")n·H_n = 224.96, simulated mean = 223.56
Test yourself
Which condition is NOT required by the integral test?
- is positive
- is decreasing
- is continuous
- is differentiable
If for a test-eligible with , what can you say about ?
- Nothing
Roughly how large is for ?
- About 1000
- About 21.3
- About 9
- About 1.64