Counting is pretty close to the first piece of math we learn, and yet is very likely to be the hardest math there is. The subject of counting combinations of things is called combinatorics, and has produced many elegant pieces of reasoning.
As a first example, lets count the number of possible bigrams made out of the letters A, B and C. That means we have to choose two letters like BC, AA or AB. There are 3 choices for each of the first and second letters. Now we use the
Fundamental Principle of Counting: If we make a choice of two things, and there are m possibilities for the first thing and n possibilities for the second thing, then altogether there arepossible choices.
In our example there are
bigrams. The validity of the
fundamental principle is verified by writing all the choices out in a
table:
| A | B | C | |
| A | AA | AB | AC |
| B | BA | BB | BC |
| C | CA | CB | CC |
As another example consider all bigrams where the first thing is a
letter (26 possibilities) and the second thing is a decimal digit
(0-9). Then there are
possible bigrams, including A4,
Q8, etc.
Now we can say that the number of possible bigrams of English letters is 262=676, the number of English trigrams is 263 = 17,576, etc.
It gets more interesting when we put conditions on the
combinations being considered. For example, we might ask for
combinations that avoid all duplicates. The key to a monoalphabetic
cipher is a list of all 26 letters, just not in the same order.
An example key is
Many questions involving no duplicates may be handled by factorials.
For instance, what is the number of five card hands that may be drawn
from a standard 52 card deck? In general, when we pick up our hand, it
does not matter what order the cards were dealt in, but for the first
example lets suppose it does matter (in Stud Poker, order is
important, at least for betting purposes). For the first card dealt,
there are 52 choices. After dealing that card, there are only 51
cards remaining in the deck. Thus, there are 51 choices for the
second card dealt. Continuing in that way, there are 50, 49 and 48
choices for the third, fourth and fifth cards dealt, respectively. The
fundamental principle now says there are
Let's do this with a smaller deal and work out what the combinations
are. Suppose we choose 2 different letters in order from A, B,
C, D and E. Then there should be
possible
choices.
This can also be fit into a table:
| A | B | C | D | E | |
| A | AB | AC | AD | AE | |
| B | BA | BC | BD | BE | |
| C | CA | CB | CD | CE | |
| D | DA | DB | DC | DE | |
| E | EA | EB | EC | ED |
That is a bit of a hint for Problem 4(b) in Chapter 3. In Problem 4(a), you will have determined the number of passwords of 4 alphanumeric characters. That includes A-Z and 0-9 (forget about upper versus lower case). So all told there are 36 choices for each character. Problem 4(b) asks for the number where we require that we have to have at least one digit and one letter. Think about what the passwords are that are excluded. They would be all four digits or all four letters. Those two possibilities are disjoint, i.e. they cannot happen at the same time, and they are easier to count. Put these ideas together and you will have the answer to 4(b).
Next, let's discuss how to count combinations where we are unconcerned about the order of appearance, as in a five-card hand. Suppose the five cards are A, B, C, D and E, just for the sake of discussion. When they are dealt, the cards could appear in many orders, for instance:
| A | B | C | D | E |
| A | B | C | E | D |
| A | E | C | D | B |
| E | A | D | B | C |
| etc. |
We used this reasoning in counting the number of pairs of distinct letters
selected from a text of n letters. There are n choices for the
first letter and n-1 choices for the second. Since we were only
interested in whether these pairs matched, we did not care about the
order in which the letters were selected. Thus, we had to divide by
the number of reorderings of two letters, which is only
.
So the number of pairs of letters is
These are examples of a general formula for the number of choices of
k different things from a set of n things. The formula is
just
The binomial can be written tersely in terms of factorials.
We just have to fill out the product in the numerator all the way out
to 1, and then cancel the new terms in the denominator. For example,
You might suspect that computing with factorials is very difficult, but in fact there are many tricks and very good approximations for calculating with these. Binomial formulas are very effective and practical ways of answering problems of this kind.
Finally, there is one small wrinkle in counting code wheels used in various ciphers. When we fill in 26 letters in a circle, there is no way to specify where the ``beginning'' of a circle is. The letter Amust appear somewhere. Thus, we might as well start just after A and make subsequent choices of how to write in the remaining letters. Then, surprisingly, there are only 25! code wheels of this kind, not 26!.
To really convince yourself of this, consider only a three letter alphabet A, B, and C. If we write the letters in order on a wheel as ABC, or BCA or CAB, the resulting wheel is the same! Thus, there are only two such code wheels, with orders ABC and ACB(which is not a rotation of ABC).