Introduction to Discrete Mathematics

Sequences and Series

Notes By Pr. El Hadiq Zouhair

1 / 14

Overview

$\{1,2,3,4,5,\dots\}\quad\{1,3,5,7,9,\dots\}\quad\{1,2,4,8,16,\dots\}$

Python / SymPy — imports (run once) from itertools import islice from sympy import symbols, Sum, Product, Symbol, oo, simplify, summation, Rational from sympy import sequence, catalan, prime, Function k, n, r, d, c, x, i = symbols('k n r d c x i')
2 / 14

Sequence

A sequence is an ordered list of mathematical objects, mapping $\{1, 2, 3, \dots, n\}$ to a set $S$.

A sequence often has a function associated to it — the general term $a_k = f(k)$.

Python — finite squares = [k**2 for k in range(1, 11)] print(squares) # [1, 4, 9, 16, 25, 36, 49, 64, 81, 100] from sympy import prime primes = [prime(k) for k in range(1, 11)] print(primes) # [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
Python — infinite (generator) def naturals(): k = 1 while True: yield k k += 1 from itertools import islice print(list(islice(naturals(), 10))) # [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
SymPy — symbolic sequence from sympy import sequence, symbols k = symbols('k') s = sequence(k**2, (k, 1, 10)) print(list(s)) # [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
3 / 14

Sums and Series

A sum is a finite ordered list of terms combined by addition:

$\displaystyle \sum_{x=1}^{n} f(x) \;=\; f(1) + f(2) + \dots + f(n)$

Similarly, a series is an infinite ordered set of terms combined by addition.

A series is said to be convergent if it approaches some limit, and divergent otherwise.

Python — finite print(sum(x for x in [1, 2, 3, 4, 5])) # 15 print(sum(k**2 for k in range(1, 11))) # 385
SymPy — symbolic Σ from sympy import symbols, Sum, oo k, n = symbols('k n') print(Sum(k**2, (k, 1, n)).doit()) # n*(n+1)*(2*n+1)/6
SymPy — infinite series from sympy import Sum, oo, Rational k = symbols('k') print(Sum(Rational(3,10)**k, (k, 1, oo)).doit()) # 3/7
4 / 14

Famous sequences: Primes & Catalan

The prime numbers are the most studied sequence in number theory.

The Catalan numbers $C_n = \frac{1}{n+1}\binom{2n}{n}$ count many combinatorial objects (polygon triangulations, balanced parentheses, etc.).

SymPy from sympy import prime, catalan print([prime(k) for k in range(1, 11)]) # [2, 3, 5, 7, 11, 13, 17, 19, 23, 29] print([catalan(k) for k in range(0, 11)]) # [1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862, 16796]
SymPy — generating function from sympy import Sum, symbols, oo, simplify, sqrt k, z = symbols('k z') # Σ C_k z^k = (1 − √(1 − 4z)) / (2z) gf = Sum(catalan(k)*z**k, (k, 0, oo)).doit() print(simplify(gf))

Series of this kind are generating functions — explored in more detail in the lesson on Generating Functions.

5 / 14

Arithmetic Progression

An arithmetic progression is a sequence of $n+1$ numbers $\{c + kd\}_{k=0}^{n}$ with a constant difference $d$ between successive terms.

Python c, d, n = 2, 3, 10 ap = [c + k*d for k in range(n+1)] print(ap) # [2, 5, 8, 11, 14, 17, 20, 23, 26, 29, 32] print(sum(ap)) # 187
SymPy — closed form from sympy import symbols, Sum, simplify c, d, k, n = symbols('c d k n') S = Sum(c + d*k, (k, 0, n)).doit() print(simplify(S)) # (n + 1)*(2*c + d*n)/2

Closed-form: $\displaystyle \sum_{k=0}^{n} (c + kd) \;=\; \frac{(n+1)(2c + nd)}{2}$

Arithmetic series diverge — extending to $\infty$ never converges (terms don't shrink).

6 / 14

Geometric Progression

A geometric progression is a sequence of numbers $\{d \cdot r^k\}$ with a constant ratio $r$ between successive terms.

Python d, r, n = 2, 3, 10 gp = [d * r**k for k in range(n+1)] print(gp) # [2, 6, 18, 54, 162, 486, 1458, 4374, 13122, 39366, 118098]
SymPy from sympy import symbols, Sum, simplify, oo d, r, k, n = symbols('d r k n') # Finite: Σ_{k=0..n} d·r^k = d·(r^{n+1} − 1)/(r − 1) print(simplify(Sum(d*r**k, (k, 0, n)).doit())) # Infinite (|r|<1): Σ_{k=0..∞} r^k = 1/(1−r) print(Sum(r**k, (k, 0, oo)).doit()) # 1/(1 − r) with |r|<1

Geometric series converge when $|r| < 1$: $\displaystyle \sum_{k=0}^{\infty} r^k = \frac{1}{1 - r}$

7 / 14

Recurrence Relation

A recurrence relation is a mathematical relationship expressing $a_n$ as some combination of $a_i$ with $i < n$.

Python — arithmetic recurrence def arith(k): return 2 if k == 0 else arith(k-1) + 3 print([arith(k) for k in range(6)]) # [2, 5, 8, 11, 14, 17] # iterative (faster) def arith_it(n): a, out = 2, [] for _ in range(n+1): out.append(a); a += 3 return out
Python — geometric recurrence def geom(k, memo={0: 2}): if k not in memo: memo[k] = 3 * geom(k-1) return memo[k] print([geom(k) for k in range(6)]) # [2, 6, 18, 54, 162, 486]
SymPy — solve a recurrence symbolically from sympy import Function, rsolve, symbols k = symbols('k') a = Function('a') # a(k) − a(k−1) = 3, a(0)=2 → a(k) = 3k + 2 print(rsolve(a(k) - a(k-1) - 3, a(k), {a(0): 2}))
8 / 14

Useful Sums and Series

These sums and series occur often in discrete mathematics, making them worth remembering.

ExpressionClosed form
$\displaystyle\sum_{i=0}^{n} d\,r^{\,i}$ $\dfrac{d\,(r^{\,n+1} - 1)}{r - 1}$
$\displaystyle\sum_{i=0}^{n} i$ $\dfrac{1}{2}\,n\,(n+1)$
$\displaystyle\sum_{i=0}^{n} i^{2}$ $\dfrac{1}{6}\,n\,(n+1)(2n+1)$
$\displaystyle\sum_{i=0}^{n} i^{3}$ $\dfrac{1}{4}\,n^{2}\,(n+1)^{2}$
$\displaystyle\sum_{i=0}^{\infty} r^{\,i}$  ($|r|<1$) $\dfrac{1}{1 - r}$
$\displaystyle\sum_{i=0}^{\infty} i\,r^{\,i}$  ($|r|<1$)$\dfrac{r}{(1 - r)^{2}}$
SymPy — verify them all in one block from sympy import symbols, Sum, simplify, oo, Rational i, n, r = symbols('i n r') print(simplify(Sum(i, (i, 0, n)).doit())) # n*(n+1)/2 print(simplify(Sum(i**2, (i, 0, n)).doit())) # n*(n+1)*(2n+1)/6 print(simplify(Sum(i**3, (i, 0, n)).doit())) # n**2*(n+1)**2/4 print(Sum(r**i, (i, 0, oo)).doit()) # 1/(1−r) for |r|<1 print(simplify(Sum(i*r**i, (i, 0, oo)).doit()))# r/(1−r)**2
9 / 14

Convergence — quick tests

Common convergence tests:

Python — partial-sum convergence (visual) import math def partials(seq, N): s = 0; out = [] for k, a in enumerate(seq): s += a; out.append((k, s)) if k >= N: break return out # Σ 1/k² → π²/6 ≈ 1.6449... gen = (1 / k**2 for k in range(1, 10_001)) print(sum(1/k**2 for k in range(1, 10_001))) # ≈ 1.6448... print(math.pi**2 / 6) # 1.6449...
SymPy — exact infinite sums from sympy import Sum, symbols, oo, pi, Rational k = symbols('k', integer=True, positive=True) print(Sum(1/k**2, (k, 1, oo)).doit()) # pi**2/6 print(Sum(1/k, (k, 1, oo)).doit()) # oo (harmonic series diverges)
10 / 14

Products

A product is an ordered set of terms combined together by the multiplication operator:

$\displaystyle \prod_{x=1}^{n} f(x) \;=\; f(1)\cdot f(2)\cdots f(n)$

Python — math.prod (3.8+) import math print(math.prod([1, 2, 3, 4, 5])) # 120 (= 5!) # functools.reduce for older Pythons: from functools import reduce from operator import mul print(reduce(mul, range(1, 6), 1)) # 120
SymPy — symbolic Π from sympy import symbols, Product, simplify, factorial x, n = symbols('x n') print(Product(x, (x, 1, n)).doit()) # factorial(n) print(simplify(Product(x, (x, 1, 5)).doit())) # 120

Some infinite products converge like convergent series. A famous example (Euler's product for cosine): $\displaystyle \prod_{i=1}^{\infty}\!\left(1 - \frac{4x^2}{\pi^2(2i - 1)^2}\right) = \cos(x)$

SymPy — Wallis-style product (partial) from sympy import Product, symbols, pi, Rational, simplify i = symbols('i', integer=True, positive=True) expr = 1 - Rational(4, 1) * (Rational(1,4))**2 / (pi**2 * (2*i - 1)**2) # (just a sanity check on the form — full product needs care with convergence) print(Product(expr, (i, 1, 5)).doit())
11 / 14

Example — closed-form proof

Prove that $\displaystyle\sum_{k=1}^{n} (2k - 1) = n^2$ for every $n \geq 1$.

By formula: this is an arithmetic progression with $c = 1, d = 2$, starting at $k = 1$: $\;S_n = \frac{n(2c + (n-1)d)}{2} = \frac{n(2 + 2(n-1))}{2} = n^2$.

Python — numerical sanity check (n up to 1000) for n in range(1, 1001): assert sum(2*k - 1 for k in range(1, n+1)) == n**2 print("Identity holds for all n ≤ 1000 ✓")
SymPy — symbolic proof from sympy import symbols, Sum, simplify k, n = symbols('k n') print(simplify(Sum(2*k - 1, (k, 1, n)).doit())) # n**2
12 / 14

Summary

Python — key tools for this lesson import math from itertools import islice from functools import reduce from operator import mul from sympy import (symbols, Sum, Product, sequence, oo, simplify, prime, catalan, rsolve, Function, Rational)
13 / 14

Wolfram → Python Quick Reference

OperationWolframPython (built-in)SymPy
Sequence (table) Table[f[k],{k,1,n}] [f(k) for k in range(1,n+1)] sequence(f(k),(k,1,n))
Finite sum Sum[f[k],{k,1,n}] sum(f(k) for k in …) Sum(f(k),(k,1,n)).doit()
Infinite sum Sum[f[k],{k,1,Infinity}] limit of partial sums Sum(f(k),(k,1,oo)).doit()
Finite product Product[f[k],{k,1,n}] math.prod(…) Product(f(k),(k,1,n))
Prime number Prime[k] via sympy prime(k)
Catalan number CatalanNumber[k] via sympy catalan(k)
Recurrence RSolve memoized recursion rsolve
Series expansion Series[f,{x,0,n}] f.series(x,0,n)
14 / 14