The Binomial Expansion
Multiplying out by hand is tedious, but the answer follows a beautiful pattern. The binomial theorem gives every term of at once, using numbers from Pascal's triangle. This lesson builds the triangle, connects it to counting (choosing items from ), and computes everything with Python's math.comb.
Expanding small powers
Three patterns stand out in :
- there are terms;
- the power of falls from to 0 while the power of rises from 0 to , and in every term the two powers add up to ;
- the coefficients are symmetric: 1 4 6 4 1.
Pascal's triangle
Write the coefficients in rows. Each number is the sum of the two numbers above it, with 1s down the edges:
n = 0: 1
n = 1: 1 1
n = 2: 1 2 1
n = 3: 1 3 3 1
n = 4: 1 4 6 4 1
n = 5: 1 5 10 10 5 1Why does adding work? . Each term of the new power comes from two old terms: one multiplied by , the other by . So each new coefficient is the sum of two neighbouring old ones.
def pascal(rows):
row = [1]
for n in range(rows):
print(f"n = {n}: " + " ".join(str(c) for c in row).center(40).rstrip())
row = [1] + [row[i] + row[i + 1] for i in range(len(row) - 1)] + [1]
pascal(8)n = 0: 1
n = 1: 1 1
n = 2: 1 2 1
n = 3: 1 3 3 1
n = 4: 1 4 6 4 1
n = 5: 1 5 10 10 5 1
n = 6: 1 6 15 20 15 6 1
n = 7: 1 7 21 35 35 21 7 1
Binomial coefficients
The -th entry of row (counting from ) is the binomial coefficient " choose ":
It counts the ways to choose items from when order does not matter. That is exactly what happens when you multiply out : each term picks either or from each of the brackets, and the term appears once for every way of choosing which brackets supply .
The binomial theorem
Python has math.comb(n, k) built in. Here it expands , with and , and SymPy confirms:
import math
import sympy as sp
n, a, b = 5, 2, -3 # (a·x + b)^n = (2x - 3)^5
terms = []
for k in range(n + 1):
coeff = math.comb(n, k) * a**(n - k) * b**k
terms.append(f"{coeff:+d}x^{n - k}")
print(" ".join(terms))
x = sp.symbols("x")
print(sp.expand((2 * x - 3)**5))+32x^5 -240x^4 +720x^3 -1080x^2 +810x^1 -243x^0
32*x**5 - 240*x**4 + 720*x**3 - 1080*x**2 + 810*x - 243
To find one particular term without expanding everything, use the general term directly. For example the term of has , so : .
Tip: Setting gives : the entries of row add up to , the number of subsets of an -element set. Setting , shows the alternating sum of a row is 0.
Approximations with the binomial
For small , , since the later terms contain This shortcut works for any power, even fractional ones like , via the binomial series.
import math
for x in [0.1, 0.01, 0.001]:
exact = (1 + x)**8
print(f"x = {x}: (1+x)^8 = {exact:.6f} 1 + 8x = {1 + 8 * x:.6f} with x^2 term = {1 + 8 * x + math.comb(8, 2) * x**2:.6f}")x = 0.1: (1+x)^8 = 2.143589 1 + 8x = 1.800000 with x^2 term = 2.080000
x = 0.01: (1+x)^8 = 1.082857 1 + 8x = 1.080000 with x^2 term = 1.082800
x = 0.001: (1+x)^8 = 1.008028 1 + 8x = 1.008000 with x^2 term = 1.008028
In software
- Counting and probability.
math.combcounts combinations: lottery tickets, poker hands, the number of test cases for features out of . The probability of exactly successes in coin-like trials is (the binomial distribution). - Bézier curves. The weights of a degree- Bézier curve are the terms of the expansion of , which is why they always add up to 1 (see Bézier Curves).
- Dynamic programming. Pascal's rule is a classic table-filling recurrence (Tabulation).
- Interest. explains why simple and compound interest agree for small rates or short times.
Exercises
Exercise 1: Row 6
Use Pascal's triangle to expand .
Answer
Solution
Row 6 of the triangle is 1 6 15 20 15 6 1. The power of falls from 6 to 0 while the power of rises from 0 to 6.
Exercise 2: One term
Find the coefficient of in .
Answer
−2835
Solution
import math
print(math.comb(7, 3) * 3**4 * (-1)**3)-2835
Exercise 3: Middle term
Find the constant term (no ) in .
Answer
160
Solution
Exercise 4: Rows sum to powers of two
Write a loop that checks math.comb(n, k) for to 20, and prints the sum for .
Answer
It holds for every ; for the sum is 1 048 576.
Solution
import math
ok = all(sum(math.comb(n, k) for k in range(n + 1)) == 2**n for n in range(21))
print(ok, sum(math.comb(20, k) for k in range(21)))True 1048576
Test yourself
What is the coefficient of in ?
- 5
- 10
- 15
- 20
How many terms does the expansion of have?
- 13
- 12
- 24
- 6
What does math.comb(6, 2) return?
- 12
- 30
- 36
- 15