TExcelTAD
Home › Calculus in Depth › Series

Series

An infinite series a1+a2+a3+…a_1 + a_2 + a_3 + \ldots is defined through its partial sums: it has a sum exactly when the sequence of partial sums converges. This lesson proves the geometric series formula, the divergence of the harmonic series and the nnth-term test, and then uses geometric series where programmers meet them most: discounted rewards in reinforcement learning and the amortised cost of a growing array.

The survey lesson Series and Convergence has a gentler introduction.

Definition

Given a sequence {an}\{a_n\}, its partial sums are sn=a1+⋯+ans_n = a_1 + \cdots + a_n. The series ∑an\sum a_n converges with sum SS if sn→Ss_n \to S as a sequence (see Sequences), and diverges otherwise. A series is therefore a sequence in disguise, and everything about limits of sequences applies to {sn}\{s_n\}.

The geometric series

Theorem. For a≠0a \ne 0, the series ∑n=1∞arn−1=a+ar+ar2+⋯\sum_{n=1}^{\infty} ar^{n-1} = a + ar + ar^2 + \cdots converges if and only if ∣r∣<1\lvert r \rvert < 1, and then

∑n=1∞arn−1=a1−r\sum_{n=1}^{\infty} ar^{n-1} = \frac{a}{1 - r}

Proof. If r=1r = 1, sn=na→±∞s_n = na \to \pm\infty. Otherwise subtract rsnr s_n from sns_n; everything cancels but two terms:

sn=a+ar+⋯+arn−1rsn=a+ar+⋯+arn−1+arn(1−r)sn=a−arn⇒sn=a(1−rn)1−r\begin{aligned} s_n &= a + ar + \cdots + ar^{n-1} \\ r s_n &= \phantom{a + {}} ar + \cdots + ar^{n-1} + ar^n \\ (1 - r)s_n &= a - ar^n \quad\Rightarrow\quad s_n = \frac{a(1 - r^n)}{1 - r} \end{aligned}

If ∣r∣<1\lvert r \rvert < 1 then rn→0r^n \to 0 and sn→a1−rs_n \to \frac{a}{1 - r}. If ∣r∣>1\lvert r \rvert > 1 or r=−1r = -1, rnr^n has no limit, so neither does sns_n. ∎

A repeating decimal is a geometric series: 0.272727…=27100+271002+⋯=27/1001−1/100=2799=3110.272727\ldots = \frac{27}{100} + \frac{27}{100^2} + \cdots = \frac{27/100}{1 - 1/100} = \frac{27}{99} = \frac{3}{11}.

Telescoping series

When an=bn−bn+1a_n = b_n - b_{n+1}, the partial sums collapse: sn=b1−bn+1s_n = b_1 - b_{n+1}. Partial fractions often expose this structure:

1n(n+1)=1n−1n+1⇒sn=1−1n+1→1\frac{1}{n(n + 1)} = \frac{1}{n} - \frac{1}{n + 1} \quad\Rightarrow\quad s_n = 1 - \frac{1}{n + 1} \to 1
python · try it yourself
import sympy as sp
from fractions import Fraction

n, k = sp.symbols("n k", integer=True, positive=True)
print("partial fractions:", sp.apart(1 / (k * (k + 1)), k))
print("s_n =", sp.simplify(sp.summation(1 / (k * (k + 1)), (k, 1, n))))
print("geometric, r = 2/3:", sp.summation(5 * sp.Rational(2, 3)**(k - 1), (k, 1, sp.oo)))
print("0.272727... =", Fraction(27, 100) / (1 - Fraction(1, 100)))
partial fractions: -1/(k + 1) + 1/k
s_n = n/(n + 1)
geometric, r = 2/3: 15
0.272727... = 3/11

The harmonic series diverges

∑1/n\sum 1/n diverges even though its terms go to 0. Group the terms in blocks of length 1,2,4,8,…1, 2, 4, 8, \ldots:

s2k=1+12+(13+14)+(15+⋯+18)+⋯+(12k−1+1+⋯+12k)≥1+12+(14+14)+(18⋅4)+⋯+(2k−1⋅12k)=1+k2\begin{aligned} s_{2^k} &= 1 + \tfrac{1}{2} + \left(\tfrac{1}{3} + \tfrac{1}{4}\right) + \left(\tfrac{1}{5} + \cdots + \tfrac{1}{8}\right) + \cdots + \left(\frac{1}{2^{k-1} + 1} + \cdots + \frac{1}{2^k}\right) \\ &\ge 1 + \tfrac{1}{2} + \left(\tfrac{1}{4} + \tfrac{1}{4}\right) + \left(\tfrac{1}{8} \cdot 4\right) + \cdots + \left(2^{k-1} \cdot \frac{1}{2^k}\right) \\ &= 1 + \frac{k}{2} \end{aligned}

Each block has 2j−12^{j-1} terms, each at least 1/2j1/2^j, so each block contributes at least 12\frac{1}{2}. Hence s2k→∞s_{2^k} \to \infty. The growth is very slow: the integral test shows sn≈ln⁡ns_n \approx \ln n.

The nth-term test

Theorem. If ∑an\sum a_n converges, then an→0a_n \to 0.

Proof. an=sn−sn−1a_n = s_n - s_{n-1}, and both sns_n and sn−1s_{n-1} tend to the same SS, so an→S−S=0a_n \to S - S = 0. ∎

Its contrapositive is the test for divergence: if lim⁡an≠0\lim a_n \ne 0 (or does not exist), ∑an\sum a_n diverges. So ∑nn+1\sum \frac{n}{n + 1} and ∑cos⁡(1/n)\sum \cos(1/n) diverge at once. The converse is false; the harmonic series is the counterexample.

Linearity. If ∑an=A\sum a_n = A and ∑bn=B\sum b_n = B, then ∑(an+bn)=A+B\sum (a_n + b_n) = A + B and ∑c an=cA\sum c\,a_n = cA (apply the limit laws to the partial sums). Changing or dropping finitely many terms never affects convergence, only the sum.

In software: discounting in reinforcement learning

An RL agent that receives rewards r0,r1,r2,…r_0, r_1, r_2, \ldots values them with a discount factor 0≤γ<10 \le \gamma < 1:

G=r0+γr1+γ2r2+⋯=∑k=0∞γkrkG = r_0 + \gamma r_1 + \gamma^2 r_2 + \cdots = \sum_{k=0}^{\infty} \gamma^k r_k

If ∣rk∣≤Rmax⁡\lvert r_k \rvert \le R_{\max}, comparison with the geometric series (see The Comparison Tests) gives ∣G∣≤Rmax⁡1−γ\lvert G \rvert \le \frac{R_{\max}}{1 - \gamma}. That bound is why γ<1\gamma < 1 makes infinite-horizon problems well-defined, and why 11−γ\frac{1}{1 - \gamma} is called the effective horizon: with γ=0.99\gamma = 0.99 the agent effectively looks about 100 steps ahead. Returns are computed backwards with Gt=rt+γGt+1G_t = r_t + \gamma G_{t+1}.

python · try it yourself
import numpy as np

gamma = 0.99
print(f"constant reward 1 forever: G = 1/(1 − γ) = {1 / (1 - gamma):.1f}")
# weight of the first H steps in the total: 1 − γ^H
for H in (10, 100, 459):
    print(f"first {H:3d} steps carry {1 - gamma**H:.1%} of the weight")

rewards = np.array([0, 0, 1, 0, 0, 0, 5, 0, 0, 10], dtype=float)
G = 0.0
returns = []
for r in rewards[::-1]:          # backwards: G_t = r_t + γ G_{t+1}
    G = r + gamma * G
    returns.append(G)
print("G_0 =", round(returns[-1], 4))
print("direct ∑ γ^k r_k =", round(float(np.sum(gamma ** np.arange(10) * rewards)), 4))
constant reward 1 forever: G = 1/(1 − γ) = 100.0
first  10 steps carry 9.6% of the weight
first 100 steps carry 63.4% of the weight
first 459 steps carry 99.0% of the weight
G_0 = 14.8227
direct ∑ γ^k r_k = 14.8227

In software: amortised array growth

A dynamic array (Python list, C++ std::vector, Rust Vec) that doubles its capacity when full copies 1+2+4+⋯+2k−1=2k−1<2n1 + 2 + 4 + \cdots + 2^{k-1} = 2^k - 1 < 2n elements to reach n=2kn = 2^k items. That is a finite geometric sum, and it is why appends are O(1)O(1) amortised (see time complexity). Growing by a factor g>1g > 1 copies about n/(g−1)n/(g - 1) elements; growing by a constant amount instead gives the arithmetic sum ∼n2/(2c)\sim n^2/(2c), which is quadratic.

python · try it yourself
def copies(n, grow):
    cap, size, moved = 1, 0, 0
    for _ in range(n):
        if size == cap:
            moved += size
            cap = grow(cap)
        size += 1
    return moved

n = 1_000_000
print("doubling      :", copies(n, lambda c: 2 * c), "copies")
print("factor 1.5    :", copies(n, lambda c: max(c + 1, int(c * 1.5))), "copies")
print("+1000 per grow:", copies(n, lambda c: c + 1000), "copies")
doubling      : 1048575 copies
factor 1.5    : 2099753 copies
+1000 per grow: 499501000 copies

Exercises

Exercise 1: Geometric sums

Find the sums, or say why they diverge: a) ∑n=1∞3⋅(−12)n−1\sum_{n=1}^{\infty} 3 \cdot \left(-\frac{1}{2}\right)^{n-1}, b) ∑n=0∞2n5n+1\sum_{n=0}^{\infty} \frac{2^n}{5^{n+1}}, c) ∑n=1∞(43)n\sum_{n=1}^{\infty} \left(\frac{4}{3}\right)^n.

Answer

a) 2, b) 1/31/3, c) diverges.

Solution
a) a=3, r=−12:31+1/2=2b) =15∑(25)n=15⋅11−2/5=15⋅53=13c) r=43>1: diverges (terms do not even tend to 0)\begin{aligned} &\text{a) } a = 3,\ r = -\tfrac{1}{2}{:} \quad \frac{3}{1 + 1/2} = 2 \\ &\text{b) } = \tfrac{1}{5} \sum \left(\tfrac{2}{5}\right)^n = \tfrac{1}{5} \cdot \frac{1}{1 - 2/5} = \tfrac{1}{5} \cdot \tfrac{5}{3} = \tfrac{1}{3} \\ &\text{c) } r = \tfrac{4}{3} > 1\text{: diverges (terms do not even tend to 0)} \end{aligned}

Exercise 2: A repeating decimal

Write 1.4545…1.4545\ldots (45 repeating) as a fraction.

Answer

16/1116/11.

Solution
1+45100+451002+⋯=1+45/1001−1/100=1+4599=1+511=16111 + \frac{45}{100} + \frac{45}{100^2} + \cdots = 1 + \frac{45/100}{1 - 1/100} = 1 + \frac{45}{99} = 1 + \frac{5}{11} = \frac{16}{11}

Exercise 3: Telescoping

Find ∑n=1∞2n2+2n\sum_{n=1}^{\infty} \frac{2}{n^2 + 2n}.

Answer

3/23/2.

Solution
2n(n+2)=1n−1n+2sn=(1−13)+(12−14)+(13−15)+⋯=1+12−1n+1−1n+2→32\begin{aligned} \frac{2}{n(n + 2)} &= \frac{1}{n} - \frac{1}{n + 2} \\ s_n &= \left(1 - \tfrac{1}{3}\right) + \left(\tfrac{1}{2} - \tfrac{1}{4}\right) + \left(\tfrac{1}{3} - \tfrac{1}{5}\right) + \cdots = 1 + \tfrac{1}{2} - \frac{1}{n + 1} - \frac{1}{n + 2} \to \frac{3}{2} \end{aligned}
python · try it yourself
import sympy as sp

n = sp.symbols("n", integer=True, positive=True)
print(sp.summation(2 / (n**2 + 2*n), (n, 1, sp.oo)))
3/2

Exercise 4: Effective horizon

An agent gets reward 1 at every step. With γ=0.9\gamma = 0.9, what is its return? After how many steps have at least 95% of that return been collected? Check with a loop.

Answer

G=10G = 10; after 29 steps (1−0.929≈0.9531 - 0.9^{29} \approx 0.953).

Solution
G=11−0.9=10. The first H rewards give 1−0.9H1−0.9, a fraction 1−0.9H of G.1−0.9H≥0.95  ⟺  0.9H≤0.05  ⟺  H≥ln⁡0.05ln⁡0.9≈28.4  ⇒  H=29\begin{aligned} &G = \frac{1}{1 - 0.9} = 10. \text{ The first } H \text{ rewards give } \frac{1 - 0.9^H}{1 - 0.9}, \text{ a fraction } 1 - 0.9^H \text{ of } G. \\ &1 - 0.9^H \ge 0.95 \iff 0.9^H \le 0.05 \iff H \ge \frac{\ln 0.05}{\ln 0.9} \approx 28.4 \;\Rightarrow\; H = 29 \end{aligned}
python · try it yourself
g, total, H = 0.9, 0.0, 0
while total < 0.95 * 10:
    total += g**H
    H += 1
print(H, round(total, 4))
29 9.529

Test yourself

What is 1+13+19+127+⋯1 + \frac{1}{3} + \frac{1}{9} + \frac{1}{27} + \cdots?

  1. 4/34/3
  2. 3/23/2
  3. 2
  4. It diverges

an→0a_n \to 0. What can you conclude about ∑an\sum a_n?

  1. It converges
  2. It diverges
  3. Its sum is 0
  4. Nothing yet

In RL with γ=0.98\gamma = 0.98 and rewards bounded by 1, what bound holds for the return?

  1. ∣G∣≤1\lvert G \rvert \le 1
  2. ∣G∣≤98\lvert G \rvert \le 98
  3. ∣G∣≤50\lvert G \rvert \le 50
  4. GG is unbounded