Lesson 12 of 15
Generating Functions
Encoding Sequences as Power Series
A generating function encodes a sequence as a formal power series:
We manipulate algebraically — multiplication, differentiation, partial fractions — to extract combinatorial information.
Convolution = Multiplication
If and , then:
The coefficient of counts ways to choose items from and that sum to .
Catalan Numbers via Generating Functions
The Catalan number OGF satisfies , giving:
import math
def catalan(n):
return math.comb(2 * n, n) // (n + 1)
def ogf_multiply(f, g, terms):
result = [0] * terms
for i, fi in enumerate(f):
for j, gj in enumerate(g):
if i + j < terms:
result[i + j] += fi * gj
return result
print(catalan(5)) # 42
print(ogf_multiply([1,1,1], [1,1,1], 5)) # [1, 2, 3, 2, 1]
Your Task
Implement catalan(n), ogf_multiply(f, g, terms), and extract_coefficient(ogf, n).
Pyodide loading...
Loading...
Click "Run" to execute your code.