Sub topic 3
Permutations: when order matters
This chapter assumes you can use the multiplication principle (a task done in steps has the product of the step counts as its total) and can compute and simplify factorials such as 5! = 120. Both come from earlier chapters. Here you build the permutation formula from those two ideas and learn where it goes wrong.
What a permutation counts
A permutation is an ordered selection. Two selections that contain the same items in a different order count as different outcomes. Use permutations when the positions or roles are distinguishable: first place and second place, president and treasurer, the first digit and the last digit of a code.
Suppose 5 runners finish a race and only the gold, silver, and bronze medals are awarded. The result "Ana gold, Ben silver, Cy bronze" differs from "Ben gold, Ana silver, Cy bronze". The same three people are involved, but the outcomes are different, so order matters.
Building the formula from the multiplication principle
You do not need to memorize the formula. You can get it by filling positions one at a time.
For the medals:
- Gold can go to any of 5 runners.
- Silver can go to any of the 4 remaining runners.
- Bronze can go to any of the 3 remaining runners.
By the multiplication principle, the count is 5 × 4 × 3 = 60.
Each step has one fewer choice than the step before, because an item used in one position cannot be used again. This is selection without repetition.
The product 5 × 4 × 3 is the start of 5!, cut off after 3 factors. To write it with factorials, divide away the tail you do not want:
5 × 4 × 3 = (5 × 4 × 3 × 2 × 1) / (2 × 1) = 5! / 2!
In general, the number of ordered selections of r items from n distinct items is:
P(n, r) = n! / (n − r)!
This is also written nPr. Check it on the medals: 5! / (5 − 3)! = 120 / 2 = 60.
Checking the result with code
Python's math module has functions for all three counts. The permutation count equals the combination count times 3! = 6, because each unordered group of 3 can be arranged in 6 orders. You will use that relationship in the next chapter.
import math
print(math.factorial(5)) # 120
print(math.perm(5, 3)) # 60: ordered selections of 3 from 5
print(math.comb(5, 3)) # 10: unordered selections of 3 from 5math.perm(5, 3) gives 60, matching the medal count. math.comb(5, 3) gives 10, which is the number of ways to choose the three medalists if you ignore who got which medal. Dividing 60 by 10 gives 6, the number of orders of three people.
Special cases of P(n, r)
Two special cases are worth knowing, because they let you check your arithmetic.
| Case | Value | Why |
|---|---|---|
| r = n | P(n, n) = n! | You arrange every item. Dividing by 0! = 1 changes nothing. |
| r = 1 | P(n, 1) = n | One position, n choices. |
| r = 0 | P(n, 0) = 1 | There is exactly one way to select nothing. |
| r > n | 0 | You cannot fill more positions than you have items. |
The r = n case is the one you use most often. Arranging 6 books on a shelf is P(6, 6) = 6! = 720.
Worked example 1: a code with no repeated digits
How many 4-digit codes can be made from the digits 0 through 9 if no digit repeats? Leading zeros are allowed.
There are 4 positions and 10 digits, and a used digit is not available again. The count is 10 × 9 × 8 × 7 = 5,040, which is P(10, 4).
Now change the rule: digits may repeat. Each position then has 10 choices independently, so the count is 10 × 10 × 10 × 10 = 10,000. This is not a permutation in the P(n, r) sense. It is the plain multiplication principle. The phrase "no repeats" or "distinct" is the signal that you need the shrinking-choices version.
Worked example 2: a restriction on one position
Five people, A through E, line up for a photo. How many arrangements have A in the first position?
Fill the restricted position first. A is fixed in position 1, so that position has 1 choice. The remaining 4 people fill 4 positions: 4 × 3 × 2 × 1 = 24. The answer is 1 × 24 = 24.
A related question: how many arrangements have A at either end? A can be first or last (2 ways to place A), and the other 4 people fill the other 4 positions in 4! = 24 ways. The count is 2 × 24 = 48. Here the two cases (A first, A last) cannot happen together, so adding 24 + 24 gives the same 48. This uses the addition principle for cases and the multiplication principle within each case.
Worked example 3: adjacent items
Five people stand in a row, and two specific people, say P and Q, must stand next to each other. Treat the pair as a single block. You now arrange 4 objects (the block and the other 3 people), which can be done in 4! = 24 ways. Inside the block, P and Q can swap places in 2! = 2 ways. The count is 4! × 2! = 48.
If the problem instead asks for the number of arrangements where P and Q are not together, subtract from the total of 5! = 120. The answer is 120 − 48 = 72.
import math
# 5 people in a row; two specific people must sit together
together = math.factorial(4) * math.factorial(2) # treat the pair as one block, then order the pair
print(together) # 48
print(math.factorial(5) - together) # 72 arrangements where they are NOT togetherSubtracting a count from the total is the complement method, which a later chapter treats in full. It appears here because "not together" is far harder to count directly.
How the counting unfolds
The diagram shows the medal example as a sequence of decisions. Each level removes one runner from the pool, so the number of branches shrinks from 5 to 4 to 3.
Common errors
- Using P(n, r) when order does not matter. If choosing a 3-person committee, the group {Ana, Ben, Cy} is one outcome, not six. Using P overcounts by a factor of r!. The next chapter covers the fix.
- Forgetting that choices shrink. A student writes 5 × 5 × 5 for the medals. That allows one runner to win all three medals, which the problem rules out.
- Treating "distinct" items as identical, or the reverse. The formula P(n, r) assumes all n items are different. If the items include repeats, such as the letters in BANANA, you must divide out the repeated arrangements. A later chapter handles that.
- Starting with the free positions. In a restricted problem, this produces choice counts that depend on earlier picks. Fix the restricted positions first.
- Miscounting factorial cancellation. In 8! / 5!, the result is 8 × 7 × 6 = 336, not 8! − 5!. Cancel the shared tail; do not subtract.
Edge cases
- Zero positions. P(n, 0) = 1. This follows from the formula, since n! / n! = 1, and from the logic that there is one way to do nothing.
- Items that look alike but are labeled. Two red books with different titles are distinct for counting. Items are identical only when the problem says they cannot be told apart.
- Positions with different rules. If the first digit of a code cannot be 0, the first position has 9 choices (for a no-repeat code, the remaining positions then have 9, 8, 7 choices). Count the restricted position first: 9 × 9 × 8 × 7 = 4,536.
Exercise
How many 3-letter "words" (any arrangement of letters counts as a word) can be formed from the letters A, B, C, D, E, F, G if no letter is used more than once and the word must begin with a vowel? Treat A and E as the only vowels in the set.
Show answer
Fill the restricted position first. The first letter must be A or E, so it has 2 choices. The second letter can be any of the 6 remaining letters, and the third can be any of the 5 left after that. The count is 2 × 6 × 5 = 60.
Exercise
Eight runners compete. In how many ways can first, second, and third place be awarded, and how many of those have a specific runner, Dana, in first place?
Show answer
The total is P(8, 3) = 8 × 7 × 6 = 336. With Dana fixed in first place, the other two positions are filled from the 7 remaining runners: 7 × 6 = 42.
Exercise
Six books are placed on a shelf. Two of them are volumes 1 and 2 of one set, and they must be placed next to each other, with volume 1 on the left of volume 2. How many arrangements are there?
Show answer
Glue the two volumes into one block. Because the order inside the block is fixed (volume 1 then volume 2), there is only 1 way to arrange the block internally. You now arrange 5 objects (the block and the other 4 books): 5! = 120.