Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Counting: permutations and combinations

After this lesson: I can count choices and arrangements.

55 min lesson3 commandsCompilers and computer architecture lab: gcc, flex, bison and RISC-V in one VM

How many four-digit PINs are there?

Counting in discrete maths means working out how many choices or arrangements exist without listing them all. The answer for a four-digit PIN is 10,000, and you can find it in one line.

A security team uses the same skill to judge how hard a password is to guess. This lesson gives you three tools: the product rule, permutations and combinations.

By the end of this lesson

Note

I can count choices and arrangements.

Key words

WordWhat it meansExample
Product rulemultiply the choices at each step
Factoriala whole number times every smaller one down to 15! = 120
Permutationa choice where the order matters
Combinationa choice where the order does not matter
Arrangementone way of putting things in order

The cells students fill in are left blank in this preview.

The product rule multiplies the choices

A PIN has four places. Each place can be any of 10 digits, whatever the other places hold. So the number of PINs is 10 times 10 times 10 times 10.

This is the product rule: when you make one choice after another, multiply the number of options at each step.

Worked example: two kinds of password

PasswordChoices at each placeCount
4-digit PIN10 x 10 x 10 x 1010,000
3 lower-case letters, then 2 digits26 x 26 x 26 x 10 x 101,757,600
6 lower-case letters26 x 26 x 26 x 26 x 26 x 26308,915,776

The cells students fill in are left blank in this preview.

Check the first two counts

Command
python3 -c "print(10 ** 4, 26 ** 3 * 10 ** 2)"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

A permutation counts orders without repeats

Five runners race. How many ways can the top three places go to different runners? Gold has 5 choices. Silver has 4 left. Bronze has 3 left. That is 5 x 4 x 3 = 60.

A choice where the order matters and nobody repeats is a permutation. Putting all 5 in order gives 5 x 4 x 3 x 2 x 1, written 5! and called 5 factorial.

Worked example: the first factorials

nn factorialHow it grows
01By rule, one way to arrange nothing
111
221 x 2
362 x 3
4246 x 4
512024 x 5
6720120 x 6

The cells students fill in are left blank in this preview.

Factorial of 5 and the podium count

Command
python3 -c "import math; print(math.factorial(5), math.perm(5, 3))"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

A combination ignores the order

Now pick a team of 3 from 5 people, A to E. A team of A, B, C is the same team as C, B, A. The order does not matter.

Count the permutations, 60, then divide by the 6 orders each team can stand in. 60 / 6 = 10. That count is a combination.

Worked example: all ten teams of three

Team 1Team 2Team 3Team 4Team 5
A B CA B DA B EA C DA C E
A D EB C DB C EB D EC D E

The cells students fill in are left blank in this preview.

Count them by brute force

Command
python3 -c "import itertools; print(len(list(itertools.permutations('ABCDE', 3))), len(list(itertools.combinations('ABCDE', 3))))"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

A common mistake

Warning

Many people count a team with permutations. That gives 60 for a team of 3 from 5, but only 10 teams exist. Use permutations when the order matters, such as a podium. Use combinations when it does not, such as a team.

Try it: count with a loop

Code: python
total = 1
for place in range(4):
    total = total * 10
print("PINs:", total)
# Change range(4) to range(6). How many times bigger is the count?
teams = 1
for step in range(3):
    teams = teams * (5 - step)
print("podiums:", teams, "teams:", teams // 6)

About this lab and its source: Compilers and computer architecture lab: gcc, flex, bison and RISC-V in one VM

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.