Let's learn to count! You've probably been counting for some time, but we want to introduce you to a variety of mathematical tools for thinking carefully about counting: counting objects, counting ways of doing things, and more.

The sum rule: column A or column B

Our first counting rule is the sum rule: use addition to represent independent choices. Here are a few examples.

Example 1: Escape from Claremont

Suppose you're trying to plan your route into LA for an evening of contemporary dance at REDCAT.

You could drive, taking the 210/605/10, just taking the 10, or taking the 10 and then switching to the 60. That is, there are three ways to go.

But you could also take a train! You could take the CalTrain to Union Station, or you could bike to Azusa and take the Gold Line. That is, there are two ways to go via train.

In total, there are five different ways to get to REDCAT.

Example 2: Getting a Move On

You're restless and need to get some exercise. You could go running, for a bike ride, or try some bodyweight exercises.

If you run, you could do your usual route or go for a longer run.

If you ride your bike, you could ride in the streets or use your mountain bike on some singletrack.

If you do bodyweight exercises, you could do upper body, core, or legs. Or you could just do burpees in your living room, to torture yourself.

There are two ways to run, two ways to ride, and four ways to do bodyweight exercises. All told, you have eight possible activities. (Don't let indecision keep you on the couch! You'll have more energy if you move your body a little.)

The sum rule in full

The principle we just applied is called the sum rule:

Suppose there $n$ ways to do something, and there are another $m$ ways to do it differently, then there are $n + m$ ways to do it.

The key to applying the sum rule is that the options should be independent: you can either drive or take a train but you won't do both. (At least not in our example.) You're only going to do one form of exercise.

If you're going to do some combination of things from different categories, you'll need a different rule.

The product rule: column A and column B

Our second counting rule is the product rule: use multiplication to represent combinations. Here are a few examples.

Example 1: Oh, the Pastabilities!

Let's say you're opening an Italian restaurant, "Oh, the Pastabilities!", focused on pasta dishes: diners choose a pasta shape, a topping, and a sauce.

You offer five shapes of pasta:

(But there's a whole world of shapes with delightful names!)

While Italian pastas often have nothing but sauce, your restaurant is for the American market and will include protein-oriented toppings:

Finally, you offer a mere three sauces:

Given our menu of pastas, toppings, and sauces, we might want to know how many different dishes you can make.

How many different topping/sauce combinations are there? To enumerate them manually, it helps to be methodical. For each possible topping, consider the sauces it could go with:

Okay, that's three options. Next we consider the vegetables:

Similarly, we can come up with lists for meatballs:

And for seafood:

Totting it all up, there are twelve options. You might have noticed that there are 4 toppings and 3 sauces, and $12 = 4 \cdot 3$. It's no coincidence!

With our five pasta shapes, how many different 'pastabilities' are there? Well, there are 12 sauce/topping combinations and five pasta shapes, so... $60 = 5 \cdot 12$.

Example 2: Take Me Down to Tuple Town

Given a pair of sets $A$ and $B$, we can talk about Cartesian product of those sets as $A \times B$. (In Coq, we wrote A * B.) Using set builder notation (which we'll meet in full on day 24), we might say $A \times B = \{ (x, y) \mid x \in A, y \in B \}$, i.e., the set $A \times B$ is the set of pairs of values $(x,y)$ where $x$ comes from $A$ and $y$ comes from $B$. We want to consider all possible pairs.

So: how many such tuples are there? If there are $n$ elements of $A$ and $m$ elements of $B$, then there are $n \cdot m$ elements of $A \times B$. (Which maybe helps you understand the choice of $\times$ and the name 'product'!)

Concretely, how many tuples of type $\texttt{bool} \times \texttt{base}$ are there? Well, there are two booleans (true and false) and four bases (A, C, G, and T). So: 8.

The product rule in full

We can apply what is called the product rule:

Suppose you are performing two-part task. If there are $n$ ways to do the first part and $m$ ways to do the second part, there $n \cdot m$ ways to do the whole task.

Combining the sum and product rules: phone extensions

Much of the challenge in combinatorics comes from deciding which rules to apply in which order. Natural language idioms for specifying things---"at least three", "no more than 1", "exactly 2"---translate in a variety of ways. Here's an example where we use five-digit phone extensions to see a few of those idioms.

Every phone in Pomona college has a five-digit extension off of the base 909-60x. For example, my full office phone number is 909-607-4554, but my extension is typically written x7-4554. (As of Fall 2020, that's not going to do you much good---I haven't been to my office in months!)

Let's ignore the x and the hyphen and just shorten my extension to 74554. Let's count a variety of different phone extensions at Pomona.

How many extensions are there in total? There are five places in each extension, and each could be one of ten digits. They're all independent, so we'll use the product rule: $10 \cdot 10 \cdot 10 \cdot 10 \cdot 10 = 10^5 = 100,000$. The idiom of $x^n$ for a list of $n$ independent $x$ possible elements is a common one.

Exact counting

Let's consider extensions that do or don't have sixes in them.

How many extensions are there that have no sixes in them? Well, if we exclude all sixes, then there are only nine possible digits, so we'll find $9^5 = 59049$.

How many extensions are there that have only one six in them? The trick here is to use the sum rule first: there are five positions where we could put the six, i.e., 6abcd or a6bcd or ab6cd or abc6d or abcd6. In each case, the letters a, b, c, d represent one of nine possible digits (because we don't want an extra 6---there should be just one). So, we find:

$$9^4 + 9^4 + 9^4 + 9^4 + 9^4 = 5 \cdot 9^4 = 5 \cdot 6561 = 32805$$

How many extensions are there that have exactly two sixes in them? We can play the same trick, counting a bit more carefully:

There are ten possible arrangements, and there are $9^3$ possibilities for $a$, $b$, and $c$. So:

$$10 \cdot 9^3 = 10 \cdot 729 = 7290$$

How many extensions are there that have exactly three sixes in them? Same story. Let's enumerate the possible arrangements:

Ten again. (Huh... interesting! We'll learn more about this when we see more about choice.

$$10 \cdot 9^2 = 10 \cdot 81 = 810$$

How many extensions are there that have exactly four sixes in them? Now our enumerations are easier. There's just one possible place for the non-six digit:

So our computation is simpler:

$$5 \cdot 9^1 = 45$$

How many extensions are there that have exactly five sixes in them? Well, just one: x6-6666 a/k/a 66666. So: 1.

Checking our work

It ought to be the case that we've just listed every extension! Every extension has somewhere between zero and five sixes in it. We should be able to use the sum rule to check that our six possibilities account for every extension:

\[\begin{array}{rccccccccccc} 10^5 &=& 9^5 &+& 5 \cdot 9^4 &+& 10 \cdot 9^3 &+& 10 \cdot 9^2 &+& 5 \cdot 9^1 &+& 1 \\ &=& 59049 &+& 32805 &+& 7290 &+& 810 &+& 45 &+& 1 \\ &=& 100,000 \\ \end{array}\]

It all checks out! Not only is this a good way to check our reasoning/enumerations, the equation here is a convenient way to answer a variety of other questions.

Inexact counting

So far we've counted exactly. But we're often interested in inexact questions. For example, how many extensions are there that have at least one six in them?

We could probably do something fancy with enumerations like above, but that's finicky and error prone. Instead, we can use the equation above to just subtract away the cases we don't want.

How many extensions have at least one six in them? Well, take all of them---$10^5$---and just exclude the ones that don't have a six---$9^5$. That is:

\[ 10^5 - 9^5 = 40,951 \]

Sometimes this "negative" counting is much easier than the positive one. To count this way positively, we'd have to write:

\[ 5 \cdot 9^4 + 10 \cdot 9^3 + 10 \cdot 9^2 + 5 \cdot 9^1 + 1 = 32805 + 7290 + 810 + 45 + 1 = 40,951 \]

Which do you think is easier?

How many extensions are there with no more than two sixes in them? Here, we have a choice: we could take away the three/four/five cases or sum up the zero/one/two cases. Here, positive and negative counting seem to be about the same amount of work. We could do it either way:

\[ \begin{array}{rcl} 9^5 + 5 \cdot 9^4 + 10 \cdot 9^3 &=& 59049 + 32805 + 7290 \\ &=& 99,144 \\ &=& 100,000 - (810 + 45 + 1) \\ &=& 100,000 - (10 \cdot 9^2 + 5 \cdot 9^1 + 1) \\ \end{array} \]

A word about writing answers

Whenever we do combinatorial computations, we give two things: first, we give a formula that explains how we're counting (e.g., $10^5$ means we're counting 5 distinct things where each thing has ten possibilities) in addition to the final answer ($100,000$). Both of these are important, but you might be surprised to learn that the formula is more important than the final answer. The formula tells the reader how you got there. If you just them a number, well... why should they believe you?

Accordingly: please always give a formula. If it's convenient (or we ask you to), give the final answer, too. But it's much more important that your formula be in a form that reflects the way that you counted!

Inclusion/exclusion: avoiding double counting

There are fifteen cars for sale at the dealership. Ten of them have a sun roof. Six of them are hybrid vehicles. Four are both. How many cars have no sunroof and aren't hybrids?

The counting here is tricky---even the phrasing is tricky. Ten cars have a sun roof; six are hybrids... but only four have both. A quick count might confuse you: $10 + 6 + 4 = 20$, but there are only fifteen cars? How's that possible?

Well, the English language is making your life difficult. If there are ten cars with sun roofs and six hybrids, and four that are both, that means we're really looking at $10 + 6 - 4 = 12$ cars. Why? Double counting!

  1. SR
  2. SR
  3. SR
  4. SR
  5. SR
  6. SR
  7. SR, HYBRID
  8. SR, HYBRID
  9. SR, HYBRID
  10. SR, HYBRID
  11. HYBRID
  12. HYBRID
  13. neither
  14. neither
  15. neither

If we simply count sun roofs, we'll find ten. If we count hybrids, we'll get six. But four of those cars have both, i.e., they've been counted twice! To get an accurate tally, you need to account for that extra counting.

So there are 12 cars that have sun roofs and/or hybrid engines, so that means 3 must be living in the dark ages, literally and figuratively.

More generally, the inclusion/exclusion rule (sometimes called the subtraction rule) is:

Suppose you are performing two tasks simultaneously. If there are $n$ ways to perform the first task and $m$ ways to perform the second task and $k$ ways to do both, then there are $n + m - k$ ways to do the whole task.

Permutations: iterating the product rule

Suppose you have some books to read:

How many different orders could you read these books in? Well, you have four options for the first book, then three for the second, then two, then the last is determined. So: $4 \cdot 3 \cdot 2 \cdot 1 = 24$.

What if you were also going to read a fifth book, Station Eleven? Then there would be $5 \cdot 4 \cdot 3 \cdot 2 \cdot 1 = 5 \cdot 24 = 120$ possible orders.

We've just computed the number of permutations of five elements. Our formula is just a direct application of the product rule, with some reasoning about "what's left to do".

The general rule is defined in terms of the following function:

\[ \mathit{factorial}(n) = \begin{cases} 1 & n \le 1 \\ n \cdot \mathit{factorial}(n') & n = S(n') \\ \end{cases} \]

It's typical to use the notation $n!$, where:

\[ n! = \mathit{factorial}(n) = \prod_{i=1}^n i = n \cdot (n-1) \cdot \dots \cdot 2 \cdot 1 \]

We pronounce $n!$ as "$n$ factorial". (If you haven't seen it before, $\prod_{i=b}^n f(i)$ is product notation, the multiplication version of sum notation. $\prod_{i=b}^n f(i)$ denoting the product $f(b) \cdot f(b+1) \cdot \dots f(n-1) \cdot f(n)$. The symbol is the Greek capital letter pi.)

The permutation rule in full

We can now give the permutation rule in its abstract form:

If you are going to do $n$ different things in an arbitrary order, there are $n!$ possible orderings.

Partial permutations: stopping the iteration early

You're planning out electives for your major in underwater basketweaving, and you have a few options:

Now, due to overenrollment in the Department of Underwater Basketweaving, you can only take one per semester in your three remaining semesters. How many different ways can you take three of these electives? You can only take each one once, and you take only one per semester.

Well, we can use the product rule just like above: $5 \cdot 4 \cdot 3 = 60$. You have five choices for your first semester, four the next, and only three the last.

In a moment, we'll have another way to count.

The division rule: symmetries, quotients, "modulo", "up to"

Sometimes what you're doing has some notion of "symmetry" you'd like to ignore---for example, order may not matter.

Example 1: Bust a Deal, Face the Wheel

Suppose you're going to make a spinning wheel to give away door prizes at the 5C Hackathon. The door prize options are:

The idea is that folks coming in will spin the wheel and take whatever the needle points to. How many different wheel arrangements are there?

You might say that there are four elements so we should just use the permutation rule---there are 24 wheels. But that wouldn't be quite right... you'd be overcounting by 4x! Why?

Here's one wheel:

\[ \begin{array}{ccc} & T & \\ B & \bigcirc & L \\ & G & \\ \end{array} \]

Here's another wheel:

\[ \begin{array}{ccc} & B & \\ G & \bigcirc & T \\ & L & \\ \end{array} \]

Huh. These are the same wheel! The second is just rotated 90° clockwise. For any given arrangement of the wheel, there are four possible rotations. To avoid 4x overcounting, we have to divide the number of permutations by four to account for the symmetric cases.

So: there are really only 6 possible wheels. Here they are with each of their rotations:

We've written them out with the 'T' fixed as the first element. Doing so highlights another way to count the possible wheels: consider one element fixed, and then arrange the rest of them. So if there are four elements, fix the 'T' and then find $3! = 3 \cdot 2 \cdot 1 = 6$ possible arrangements.

Example 2: It's in the Bag

Suppose we wanted to pick two numbers from the list [1; 2; 3; 4]. This is a partial permutation, so we can just stop early. There are $4 \cdot 3 = 12$ possibilities

If we don't care about order, however, then there's really fewer: what's the difference between [1;2] and [2;1]? For each "way" of choosing two numbers [n] and [m], there are two equivalent ways: [n;m] and [m;n]. So if we were to choose two such numbers not caring about order, then there would be $\frac{12}{2} = 6$ possible orderings:

How can we tell that we haven't inappropriately removed an option or accidentally double-counted? We can use the order of our enumerations to make sure we cover all of our bases---if every set of options is sorted, we can be certain that only the right things show up. That is, we're using sorted lists as a canonical form.

The division rule in full

We can give the division rule in all its generality:

If there are $n$ ways to do something and for every way of doing it there are $m$ equivalent ways, then there are $\frac{n}{m}$ ways to do it.

Common applications of the division rule are for ignoring order (as we did above) or other "symmetries".

Knowing when to apply the division rule and applying it correctly can be challenging. It's hard to know exactly what to divide by! It's generally wise to have another way to check your work, like enumerating the options in some canonical form.

Partial permutations

We've done partial permutations by "stopping early", but you can give a different account using the division rule.

To choose the three of five electives, we can count by writing $5 \cdot 4 \cdot 3$. But we can also think of the problem differently: arrange all five courses, but then drop the last two (which you won't take). That is, we'd consider SW-CWA-RR-AR-MMW the same as SW-CWA-RR-MMW-AR the same, because they only differ in the last two courses... which aren't actually taken anyway. That is, we could compute:

\[ \frac{5!}{(5-3)!} = \frac{5!}{2!} = \frac{5 \cdot 4 \cdot 3 \cdot 2 \cdot 1}{2 \cdot 1} = 5 \cdot 4 \cdot 3 = 60 \]

This account of partial permutations is common enough that it's given a name, the function $P : \mathbb{N} \times \mathbb{N} \rightarrow \mathbb{N}$, where $P(n, k)$ is a $k$-permutation of $n$ items, i.e., selecting $k$ things out of $n$ things:

\[ P(n,k) = \frac{n!}{(n-k)!} \]

Less formally, $P(n,k) = n \cdot (n-1) \cdot \dots \cdot (n - k + 2) \cdot (n - k + 1)$. People will sometimes pronounce $P(n, k)$ as "$n$ permute $k$". By convention, $P(n, k) = 0$ when $k > n$; relatedly, $P(n, n) = \frac{n!}{(n-n)!} = \frac{n!}{1!} = n!$.

To choose two elements out of four, we can simply compute $P(4,2) = \frac{4!}{(4-2)!} = \frac{4!}{2!} = \frac{24}{2} = 12$. Phew: we got the same answer.

Wait, DIVISION?!

I know, I know: we're not going to have a formal definition for division. It shouldn't come up in any of the work we're doing, but if you need to use a fact about division in a proof, it should be fine---just ask first.

Choice: counting subsets, or partial permutations modulo order

Suppose you simply want to pick three of the five underwater basketweaving electives, without respect to order. How many ways to choose three of the five electives are there, without regard to order?

We have to use both the product and division rules. There are $P(5,3) = 5 \cdot 4 \cdot 3$ ways to choose the three courses. There are $3! = 3 \cdot 2 \cdot 1$ ways to order those courses, but we don't care about order. So we have $\frac{P(5,3)}{3!} = \frac{5 \cdot 4 \cdot 3}{3 \cdot 2 \cdot 1} = \frac{60}{6} = 10$ ways to choose a set of three electives.

The reasoning we just did is common enough that it gets its own definition, called "choice" or "choose". There are many notations in common use: $\newcommand{\C}[2]{\left( \begin{array}{@{}c@{}} {#1} \\ {#2} \\ \end{array} \right)}$ \[ n C k \equiv C(n, k) \equiv \C{n}{k} = \frac{P(n,k)}{k!} = \frac{n!}{(n-k)! \cdot k!} \]

People pronounce this notation "$n$ choose $k$", and it's how you compute choosing a set of $k$ things from $n$ things, i.e., choosing $k$ things from $n$ without caring about order.

A few conventions: we only use natural numbers for $n$ and $k$. If $k n$, we say $\C{n}{k} = 0$: there's no way to choose 5 things from 3 things.

I dislike the first notation (even though it matches the pronunciation well). The second notation is good when you're writing a program, but the third notation is nicer for math on paper. I use both. In fact, I give you a macro in the day 22 homework that makes it easy to use the latter notation:

\newcommand{\C}[2]{\ensuremath{\left( \begin{array}{@{}c@{}} {#1} \\ {#2} \end{array} \right)}}

\[ \C{n}{k} = \frac{n!}{(n-k)! \cdot k!} \]

We'll spend much of day 23 talking more about choice.