Counting
CS109
Fall 2026
Review
The Core Probability Toolkit
Review: Last Lecture
Mutually Exclusive Events
Independent Events
make AND easy:
make OR easy:
Story: Ultimate Probability
https://www.maikaisogawa.com/ultimate-frisbee-probability/
Practice: Lets do the Frisbee Problem.
6
End Review
Learning Goals for Today
Learning Goals and Outline
Announcements
Announcements
Counting!
Counting operations on n distinct objects
Sort, order matters
{perms}
Choose k
{combinations}
Counting Rules
# calculate 20!
math.factorial(20)
# calculate 10 choose 5
math.comb(10, 5)
# give me each possible permutation
itertools.permutations([1,2,3,4])
# give me each possible combination
itertools.combinations([1,2,3,4,5], 3)
Counting with Steps
13
Counting with Steps
14
How Many Unique Images?
Each pixel can be one of 17 million distinct colors
(17 million)n
Sahami, Piech, Yan, Cain, Woodrow, Gregg
Sahami, Piech, Yan, Cain, Woodrow, Gregg
How Many Unique Images?
Each pixel can be one of 17 million distinct colors
(17 million)n
Sahami, Piech, Yan, Cain, Woodrow, Gregg
Orderings of Letters
CHRIS�
18
🤔
Sahami, Piech, Yang, Cain, Woodrow, Gregg
Orderings of Letters
19
chirs | crish | hicrs | hsirc | irchs | rcish | rschi | shirc |
chisr | crshi | hicsr | hsrci | ircsh | rcshi | rscih | shrci |
chris | crsih | hircs | hsric | irhcs | rcsih | rshci | shric |
chrsi | cshir | hirsc | ichrs | irhsc | rhcis | rshic | sichr |
chsir | cshri | hiscr | ichsr | irsch | rhcsi | rsich | sicrh |
chsri | csihr | hisrc | icrhs | irshc | rhics | rsihc | sihcr |
cihrs | csirh | hrcis | icrsh | ischr | rhisc | schir | sihrc |
cihsr | csrhi | hrcsi | icshr | iscrh | rhsci | schri | sirch |
cirhs | csrih | hrics | icsrh | ishcr | rhsic | scihr | sirhc |
cirsh | hcirs | hrisc | ihcrs | ishrc | richs | scirh | srchi |
cishr | hcisr | hrsci | ihcsr | isrch | ricsh | scrhi | srcih |
cisrh | hcris | hrsic | ihrcs | isrhc | rihcs | scrih | srhci |
crhis | hcrsi | hscir | ihrsc | rchis | rihsc | shcir | srhic |
crhsi | hcsir | hscri | ihscr | rchsi | risch | shcri | srich |
crihs | hcsri | hsicr | ihsrc | rcihs | rishc | shicr | srihc |
Sahami, Piech, Yang, Cain, Woodrow, Gregg
Orderings of letters
20
Step 1:
Chose first letter
Step 2:
Chose 2nd letter
Step 3:
Chose 3rd letter
Step 4:
Chose 4th letter
Step 5:
Chose 5th letter
Sahami, Piech, Yang, Cain, Woodrow, Gregg
Orderings of letters
21
Step 1:
Chose first letter
Step 2:
Chose 2nd letter
Step 3:
Chose 3rd letter
Step 4:
Chose 4th letter
Step 5:
Chose 5th letter
H
C
S
I
R
(5 options)
(4 options)
(3 options)
(2 options)
(1 option)
Sahami, Piech, Yang, Cain, Woodrow, Gregg
To the code!
22
Sahami, Piech, Yang, Cain, Woodrow, Gregg
Permutations
23
Sahami, Piech, Yang, Cain, Woodrow, Gregg
Unique 6-digit passcodes with six smudges
24
Sahami, Piech, Yang, Cain, Woodrow, Gregg
Unique 6-digit passcodes with six smudges
25
Sahami, Piech, Yang, Cain, Woodrow, Gregg
Summary of Combinatorics
26
Sahami, Piech, Yang, Cain, Woodrow, Gregg
Summary of Combinatorics
27
Sahami, Piech, Yang, Cain, Woodrow, Gregg
Unique Bit Strings
28
1, 0, 1, 0, 0
Sahami, Piech, Yang, Cain, Woodrow, Gregg
Sort n distinct objects
29
Sahami, Piech, Yang, Cain, Woodrow, Gregg
Sort n distinct objects
30
Sahami, Piech, Yang, Cain, Woodrow, Gregg
Sort n distinct objects
31
Sahami, Piech, Yang, Cain, Woodrow, Gregg
Sort n distinct objects
32
Sahami, Piech, Yang, Cain, Woodrow, Gregg
Number of ways to sort coke cans
33
Sahami, Piech, Yang, Cain, Woodrow, Gregg
Sort semi-distinct objects
34
Order 𝑛
distinct objects
n!
Sahami, Piech, Yang, Cain, Woodrow, Gregg
Sort semi-distinct objects
35
Sahami, Piech, Yang, Cain, Woodrow, Gregg
Sort semi-distinct objects
36
Sahami, Piech, Yang, Cain, Woodrow, Gregg
BOBA
37
Sahami, Piech, Yang, Cain, Woodrow, Gregg
Strings
38
Sahami, Piech, Yang, Cain, Woodrow, Gregg
Strings
39
Sahami, Piech, Yang, Cain, Woodrow, Gregg
Strings
40
Sahami, Piech, Yang, Cain, Woodrow, Gregg
Strings
41
Sahami, Piech, Yang, Cain, Woodrow, Gregg
To the code
42
Sahami, Piech, Yang, Cain, Woodrow, Gregg
To the code
43
Sahami, Piech, Yang, Cain, Woodrow, Gregg
General Approach to counting permutations
44
This is called “multiset permutations”
Sahami, Piech, Yang, Cain, Woodrow, Gregg
Sort semi-distinct objects
45
Sahami, Piech, Yang, Cain, Woodrow, Gregg
Sort semi-distinct objects
46
Sahami, Piech, Yang, Cain, Woodrow, Gregg
Sort semi-distinct objects
47
Also:
Sahami, Piech, Yang, Cain, Woodrow, Gregg
Strings
48
Sahami, Piech, Yang, Cain, Woodrow, Gregg
Strings
49
Sahami, Piech, Yang, Cain, Woodrow, Gregg
Summary of Combinatorics
50
Sahami, Piech, Yang, Cain, Woodrow, Gregg
Combinations
Summary of Combinatorics
52
Sahami, Piech, Yang, Cain, Woodrow, Gregg
Combinations with cake
53
Consider the following generative process…
Sahami, Piech, Yang, Cain, Woodrow, Gregg
Combinations with cake
54
1
2
3
4
6
5
7
8
10
9
11
12
13
14
16
15
17
18
20
19
Sahami, Piech, Yang, Cain, Woodrow, Gregg
Combinations with cake
55
1
2
3
4
6
5
7
8
10
9
11
12
13
14
16
15
17
18
19
20
Sahami, Piech, Yang, Cain, Woodrow, Gregg
Combinations with cake
56
6
7
8
10
9
11
12
13
14
15
16
17
18
20
19
Sahami, Piech, Yang, Cain, Woodrow, Gregg
Combinations with cake
57
6
7
8
10
9
11
12
13
14
15
16
17
18
20
19
k! different permutations lead to the same mingle
Sahami, Piech, Yang, Cain, Woodrow, Gregg
Combinations with cake
58
6
7
8
10
9
11
12
13
14
15
16
17
18
20
19
k! different permutations lead to the same mingle
Sahami, Piech, Yang, Cain, Woodrow, Gregg
Combinations with cake
59
k! different permutations lead to the same mingle
(n-k)! different permutations lead to the same mingle
Sahami, Piech, Yang, Cain, Woodrow, Gregg
Combinations
60
Sahami, Piech, Yang, Cain, Woodrow, Gregg
Combinations
Sahami, Piech, Yang, Cain, Woodrow, Gregg
Probability textbooks
Sahami, Piech, Yang, Cain, Woodrow, Gregg
Probability textbooks
Sahami, Piech, Yang, Cain, Woodrow, Gregg
How many unique hands of 5 cards are there in a 52 card deck?
Sahami, Piech, Yang, Cain, Woodrow, Gregg
Probabilities with Equally Likely Outcomes
Redux from Day 1:
Sahami, Piech, Yang, Cain, Woodrow, Gregg
Straight Poker Hand (equally likely outcomes)
Consider 5 card poker hands.
Sahami, Piech, Yang, Cain, Woodrow, Gregg
Straight Poker Hand (equally likely outcomes)
Sahami, Piech, Yang, Cain, Woodrow, Gregg
Straight Poker Hand (equally likely outcomes)
Sahami, Piech, Yang, Cain, Woodrow, Gregg
Straight Poker Hand (equally likely outcomes)
Sahami, Piech, Yang, Cain, Woodrow, Gregg
Straight Poker Hand (equally likely outcomes)
Sahami, Piech, Yang, Cain, Woodrow, Gregg
Calculating probabilities in a real Texas Hold 'Em match?
Sahami, Piech, Yang, Cain, Woodrow, Gregg
Super Critical Problem!
To the Course Reader!!
74
Sahami, Piech, Yang, Cain, Woodrow, Gregg
Probability of Exactly k Heads in n Coin Flips
We flip the coin 10 times. Probability of heads is p.
We want to know the probability of getting 4 heads.
What is the probability of the outcome below?
Probability of Exactly k Heads in n Coin Flips
We flip the coin 10 times. Probability of heads is p.
We want to know the probability of getting 4 heads.
What is the probability of the outcome below?
Probability of Exactly k Heads in n Coin Flips
We flip the coin 10 times. Probability of heads is p.
We want to know the probability of getting 4 heads.
What is the probability of the outcome below?
All of the outcomes with exactly 4 heads have the same probability
Probability of Exactly k Heads in n Coin Flips
We flip the coin 10 times. Probability of heads is p.
We want to know the probability of getting 4 heads.
What is the probability of the outcome below?
Probability of Exactly k Heads in n Coin Flips
The probability of getting 4 heads,
in any ordering, is the “or” of all these mutually exclusive cases
Each outcome has probability
How many cases are there?
Probability of Exactly k Heads in n Coin Flips
The probability of getting 4 heads,
in any ordering, is the “or” of all these mutually exclusive cases
Each outcome has probability
How many cases are there?
Probability of Exactly k Heads in n Coin Flips
The probability of getting 4 heads,
in any ordering, is the “or” of all these mutually exclusive cases
Each outcome has probability
How many cases are there?
Chip Defect Detection
Chip Defect Detection
Serendipity