TExcelTAD
Home › Calculus in Depth › The Integral Test and Estimates of Sums

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, ln⁡n+γ\ln n + \gamma, 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 ff be continuous, positive and decreasing on [1,∞)[1, \infty), and an=f(n)a_n = f(n). Then ∑n=1∞an\sum_{n=1}^{\infty} a_n converges if and only if ∫1∞f(x) dx\int_1^\infty f(x)\,dx converges.

Proof. On [i−1,i][i - 1, i] the decreasing ff satisfies f(i)≤f(x)≤f(i−1)f(i) \le f(x) \le f(i - 1). Integrate over the interval and add for i=2,…,ni = 2, \ldots, n:

a2+a3+⋯+an  ≤  ∫1nf(x) dx  ≤  a1+a2+⋯+an−1a_2 + a_3 + \cdots + a_n \;\le\; \int_1^n f(x)\,dx \;\le\; a_1 + a_2 + \cdots + a_{n-1}

The left sum is the area of rectangles of height f(i)f(i) standing to the left of each integer ii, which sit under the curve; the right sum uses rectangles of height f(i−1)f(i - 1), which sit above it.

  • If ∫1∞f\int_1^\infty f converges, the left inequality gives sn≤a1+∫1∞fs_n \le a_1 + \int_1^\infty f for every nn. The partial sums increase (positive terms) and are bounded, so they converge by the Monotone Convergence Theorem from Sequences.
  • If ∫1∞f=∞\int_1^\infty f = \infty, the right inequality gives sn−1≥∫1nf→∞s_{n-1} \ge \int_1^n f \to \infty, so the series diverges. ∎

The conditions only need to hold eventually (for x≥x \ge some NN), since the first finitely many terms never affect convergence.

Note: The test says nothing about the value: ∑1/n2=π2/6≈1.645\sum 1/n^2 = \pi^2/6 \approx 1.645 while ∫1∞dx/x2=1\int_1^\infty dx/x^2 = 1. The integral is only a yardstick.

pp-series

For f(x)=1/xpf(x) = 1/x^p with p>0p > 0 (for p≤0p \le 0 the terms do not tend to 0):

∫1∞x−p dx=lim⁡t→∞t1−p−11−p=1p−1if p>1,∞if p<1∫1∞x−1 dx=lim⁡ln⁡t=∞\begin{aligned} \int_1^\infty x^{-p}\,dx &= \lim_{t \to \infty} \frac{t^{1-p} - 1}{1 - p} = \frac{1}{p - 1} \quad \text{if } p > 1, \qquad \infty \quad \text{if } p < 1 \\ \int_1^\infty x^{-1}\,dx &= \lim \ln t = \infty \end{aligned}

So ∑1/np\sum 1/n^p converges iff p>1p > 1. The borderline is delicate: ∑1nln⁡n\sum \frac{1}{n \ln n} diverges (∫dxxln⁡x=ln⁡ln⁡x→∞\int \frac{dx}{x \ln x} = \ln \ln x \to \infty), while ∑1n(ln⁡n)2\sum \frac{1}{n (\ln n)^2} converges (∫=−1/ln⁡x\int = -1/\ln x is finite). Both are invisible to the ratio test.

Estimating the remainder

Let S=∑anS = \sum a_n and Rn=S−sn=an+1+an+2+⋯R_n = S - s_n = a_{n+1} + a_{n+2} + \cdots be the tail. The same rectangles, shifted to start at nn, give

∫n+1∞f(x) dx  ≤  Rn  ≤  ∫n∞f(x) dx\int_{n+1}^\infty f(x)\,dx \;\le\; R_n \;\le\; \int_n^\infty f(x)\,dx

and adding sns_n throughout traps the true sum in an interval:

sn+∫n+1∞f  ≤  S  ≤  sn+∫n∞fs_n + \int_{n+1}^\infty f \;\le\; S \;\le\; s_n + \int_n^\infty f

The midpoint of that interval is a much better estimate than sns_n alone. For ∑1/n3\sum 1/n^3, ∫n∞x−3 dx=12n2\int_n^\infty x^{-3}\,dx = \frac{1}{2n^2}:

python · try it yourself
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 Rn<10−6R_n < 10^{-6} for ∑1/n3\sum 1/n^3 it is enough that 12n2<10−6\frac{1}{2n^2} < 10^{-6}, i.e. n>500000≈707.1n > \sqrt{500000} \approx 707.1, so n=708n = 708 terms.

Harmonic numbers and Euler's constant

Let Hn=1+12+⋯+1nH_n = 1 + \frac{1}{2} + \cdots + \frac{1}{n}. The rectangle inequalities with f(x)=1/xf(x) = 1/x read ln⁡(n+1)≤Hn≤1+ln⁡n\ln(n + 1) \le H_n \le 1 + \ln n, so HnH_n grows like ln⁡n\ln n. More precisely, let dn=Hn−ln⁡nd_n = H_n - \ln n. Then

dn−dn+1=ln⁡(n+1)−ln⁡n−1n+1=∫nn+1(1x−1n+1)dx>0d_n - d_{n+1} = \ln(n + 1) - \ln n - \frac{1}{n + 1} = \int_n^{n+1} \left(\frac{1}{x} - \frac{1}{n + 1}\right)dx > 0

so dnd_n is decreasing, and dn≥ln⁡(n+1)−ln⁡n>0d_n \ge \ln(n + 1) - \ln n > 0 means it is bounded below. By the Monotone Convergence Theorem it converges; the limit is the Euler–Mascheroni constant γ≈0.5772\gamma \approx 0.5772. Thus

Hn=ln⁡n+γ+εn,εn→0(in fact εn≈12n)H_n = \ln n + \gamma + \varepsilon_n, \qquad \varepsilon_n \to 0 \qquad \left(\text{in fact } \varepsilon_n \approx \frac{1}{2n}\right)
python · try it yourself
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 2(n+1)Hn−4n≈2nln⁡n≈1.39 nlog⁡2n2(n + 1)H_n - 4n \approx 2n \ln n \approx 1.39\, n \log_2 n. The HnH_n appears because elements ii and jj are compared with probability 2j−i+1\frac{2}{j - i + 1}.
  • Coupon collector: collecting all nn distinct items when each draw is uniform takes nHn≈nln⁡n+γnn H_n \approx n \ln n + \gamma n 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 HnH_n 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 1/n1/n until 1/n<10−81/n < 10^{-8} looks converged but is still growing like ln⁡n\ln n. See time complexity for the big-O side.

Exercises

Exercise 1: Apply the test

Use the integral test to decide whether ∑n=1∞nn2+1\sum_{n=1}^{\infty} \frac{n}{n^2 + 1} converges.

Answer

It diverges.

Solution

f(x)=xx2+1f(x) = \frac{x}{x^2 + 1} is positive, continuous, and decreasing for x≥1x \ge 1 (f′(x)=1−x2(x2+1)2≤0f'(x) = \frac{1 - x^2}{(x^2 + 1)^2} \le 0).

∫1∞xx2+1 dx=lim⁡12ln⁡(t2+1)−12ln⁡2=∞\int_1^\infty \frac{x}{x^2 + 1}\,dx = \lim \tfrac{1}{2}\ln(t^2 + 1) - \tfrac{1}{2}\ln 2 = \infty

Exercise 2: Borderline series

For which p>0p > 0 does ∑n=2∞1n(ln⁡n)p\sum_{n=2}^{\infty} \frac{1}{n (\ln n)^p} converge?

Answer

Exactly for p>1p > 1.

Solution

Substitute u=ln⁡xu = \ln x, du=dx/xdu = dx/x:

∫2∞dxx(ln⁡x)p=∫ln⁡2∞duup\int_2^\infty \frac{dx}{x (\ln x)^p} = \int_{\ln 2}^\infty \frac{du}{u^p}

which is a pp-integral: finite iff p>1p > 1. The function is positive and decreasing for x≥2x \ge 2, so the integral test applies.

Exercise 3: How many terms?

How many terms of ∑1/n4\sum 1/n^4 guarantee a remainder below 10−910^{-9}? Check the actual error, given ∑1/n4=π4/90\sum 1/n^4 = \pi^4/90.

Answer

n=694n = 694 terms suffice (bound 13n3<10−9\frac{1}{3n^3} < 10^{-9}); the actual error is about 9.95×10−109.95 \times 10^{-10}.

Solution
Rn≤∫n∞x−4 dx=13n3<10−9  ⟺  n3>3.33×108  ⟺  n>693.4R_n \le \int_n^\infty x^{-4}\,dx = \frac{1}{3n^3} < 10^{-9} \iff n^3 > 3.33 \times 10^8 \iff n > 693.4
python · try it yourself
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 n=50n = 50 coupon types (seeded) over 2000 trials and compare the average number of draws with nH50n H_{50}.

Answer

nH50≈224.96n H_{50} \approx 224.96; the simulated mean is close to it (about 223.6).

Solution
python · try it yourself
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?

  1. ff is positive
  2. ff is decreasing
  3. ff is continuous
  4. ff is differentiable

If ∫1∞f=3\int_1^\infty f = 3 for a test-eligible ff with f(1)=2f(1) = 2, what can you say about S=∑f(n)S = \sum f(n)?

  1. S=3S = 3
  2. S≤5S \le 5
  3. S≥5S \ge 5
  4. Nothing

Roughly how large is Hn=1+12+⋯+1nH_n = 1 + \frac{1}{2} + \cdots + \frac{1}{n} for n=109n = 10^9?

  1. About 1000
  2. About 21.3
  3. About 9
  4. About 1.64