Pre-Calculus for Programmers

Algebra foundations: the toolkit calculus never reteaches

9 / 397
Contents

Contents

Pre-Calculus for Programmers

0 of 397 complete

10 min read

Pascal's triangle and the binomial theorem

The coefficients of an expansion can be written down directly, including the one term you actually want.

The last lesson produced the rows 1 1, 1 2 1, 1 3 3 1, 1 4 6 4 1 by multiplying brackets out. Here is a completely different way of producing numbers: start with a 1, and build each new row by adding neighbouring pairs from the row above, treating anything off the end as 0.

Adding neighboursPython

Change range(7) to range(12) and run it again. Add up each row as you read down. The totals are 1, 2, 4, 8: each row sums to double the one above.

Those are the same numbers. Adding neighbours and multiplying out brackets produce identical rows, and that is not a coincidence: multiplying an expansion by (a+b)(a+b) once more sends each old coefficient into two neighbouring slots, so each new coefficient is the sum of the two above it.

The arrangement is called Pascal's triangle. Row nn holds the coefficients of (a+b)n(a+b)^{n}, counting the top row as row 0.

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
The first six rows. Row 4 gives the coefficients of (a+b)4(a+b)^{4}.

Getting one coefficient without building the triangle

Building thirty rows to reach row 30 is fine for a computer and hopeless in an exam, so there is a formula for a single entry. It needs one piece of notation first.

The factorial of a whole number nn, written n!n!, is the product of every whole number from 1 up to nn. So 4!=1⋅2⋅3⋅4=244! = 1 \cdot 2 \cdot 3 \cdot 4 = 24 and 5!=1205! = 120. By convention 0!=10! = 1, which keeps the formulas below working at the ends of a row.

The entry in row nn, position kk, counting both from 0, is called a binomial coefficient, written (nk)\binom{n}{k} and read "nn choose kk".

(nk)=n!k! (n−k)!\binom{n}{k} = \frac{n!}{k!\,(n-k)!}
It counts the number of ways to choose $k$ things from $n$, which is why it is read that way.

Work out (52)\binom{5}{2} in full. The top is 5!=1205! = 120. The bottom is 2!⋅3!=2⋅6=122! \cdot 3! = 2 \cdot 6 = 12. So (52)=12012=10\binom{5}{2} = \frac{120}{12} = 10, which is indeed the third entry of row 5 above.

In practice the factorials mostly cancel, so it is faster to write (52)=5⋅42⋅1=10\binom{5}{2} = \frac{5 \cdot 4}{2 \cdot 1} = 10: take kk factors counting down from nn on top, and k!k! underneath. Compute a whole row that way and compare it with the triangle.

One coefficient at a timePython

Change the 5s to 6s and run it again. Then check one entry against the triangle you printed earlier. Also try choose(30, 26) and compare it with choose(30, 4).

The binomial theorem

Putting the coefficients together with the powers gives the whole expansion in one statement.

(a+b)n=∑k=0n(nk) a n−k b k(a+b)^{n} = \sum_{k=0}^{n} \binom{n}{k}\,a^{\,n-k}\,b^{\,k}
The $\sum$ sign means: add up what follows, once for each $k$ from 0 to $n$.

Read the powers rather than memorising them. In each term the exponents add to nn, the power of aa starts at nn and falls, and the power of bb starts at 0 and rises. So (a+b)4=a4+4a3b+6a2b2+4ab3+b4(a+b)^{4} = a^{4} + 4a^{3}b + 6a^{2}b^{2} + 4ab^{3} + b^{4}, with the coefficients read straight off row 4.

Finding a particular term, every step

Find the coefficient of x3x^{3} in (2x−1)5(2x-1)^{5}.

  1. Name the pieces so the theorem applies literally: a=2xa = 2x, b=−1b = -1, n=5n = 5. The minus sign belongs to bb; that is what stops sign errors later.
  2. The power of xx comes from an−k=(2x)5−ka^{n-k} = (2x)^{5-k}, so x3x^{3} needs 5−k=35-k = 3, which gives k=2k = 2.
  3. The coefficient is (52)=5⋅42⋅1=10\binom{5}{2} = \frac{5\cdot4}{2\cdot1} = 10.
  4. Now the term: (52)(2x)3(−1)2\binom{5}{2}(2x)^{3}(-1)^{2}.
  5. (2x)3=8x3(2x)^{3} = 8x^{3}, remembering the 2 is cubed as well.
  6. (−1)2=1(-1)^{2} = 1, because an even power of a negative is positive.
  7. Multiply the numbers: 10⋅8⋅1=8010 \cdot 8 \cdot 1 = 80, so the term is 80x380x^{3} and the coefficient is 80.

That is a claim about one number, so it can be checked by expanding the whole thing and reading off the slot.

Checking one coefficient against the full expansionPython

Change range(5) to range(6) so it expands (2x−1)6(2x-1)^{6}, and predict the x3x^{3} slot from the theorem before you press play. It is (63)⋅23⋅(−1)3\binom{6}{3} \cdot 2^{3} \cdot (-1)^{3}.

Check yourself

What is the coefficient of x4x^{4} in (x+2)6(x+2)^{6}?

Row 4 of Pascal's triangle is 1 4 6 4 1. Without building row 5, write it down.

1 5 10 10 5 1. Each interior entry is the sum of the two above it: 1+4=51+4 = 5, 4+6=104+6 = 10, 6+4=106+4 = 10, 4+1=54+1 = 5, and the two ends are always 1 because (n0)=(nn)=1\binom{n}{0} = \binom{n}{n} = 1. The row is symmetric because choosing kk things to include is the same as choosing n−kn-k things to leave out, so (nk)=(nn−k)\binom{n}{k} = \binom{n}{n-k}.

All lessons in Pre-Calculus for Programmers