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 may be finite: $\{1, 2, 6, 7, 100\}$
Or infinite: $\{1, 2, 4, 8, 16, \dots\}$
A sequence often has a function associated to it — the general term $a_k = f(k)$.
Python — finitesquares = [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]
Python — arithmetic recurrencedef 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 recurrencedef 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]
Ratio test: if $\lim \left|\frac{a_{k+1}}{a_k}\right| < 1$ the series converges absolutely.
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...
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 ✓")
A sequence is an ordered list of mathematical objects.
Sums (Σ) and series are elements added; products (Π) are elements multiplied.
Some series converge and some diverge; arithmetic series diverge, geometric series converge for $|r| < 1$.
Large operators only visually simplify counting; to computationally simplify counting, you will need combinatorics.
Python — key tools for this lessonimport 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)