1 of 84

Counting

CS109

Fall 2026

2 of 84

Review

3 of 84

The Core Probability Toolkit

4 of 84

Review: Last Lecture

Mutually Exclusive Events

Independent Events

make AND easy:

make OR easy:

5 of 84

Story: Ultimate Probability

https://www.maikaisogawa.com/ultimate-frisbee-probability/

6 of 84

Practice: Lets do the Frisbee Problem.

  • You flip two frisbees. For each frisbee, the probability that it lands “heads” is 0.6. The two frisbees are considered “even” if both frisbees are heads or both frisbees are tails.
  • What is the probability that the frisbees are even?

6

7 of 84

End Review

8 of 84

Learning Goals for Today

Learning Goals and Outline

​

  1. Step rule of counting
  2. Permutations
  3. Combinations
  4. Probabilities with Equally Likely Outcomes

​

​

9 of 84

Announcements

10 of 84

Announcements

  1. Tutorials start this week! You should receive an email from your Tutorial TA about where the first meeting will be. We will have rooms set up by the end of the week.
  2. If you have OAE Accomodations, please upload them here: https://forms.gle/uPs1bQMpNAxpDDSu9
  3. Pset 1 is due Monday October 5th at 11:00PM PDT
  4. We have regular office hours – we are there to help!

11 of 84

Counting!

12 of 84

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)

13 of 84

Counting with Steps

13

14 of 84

Counting with Steps

14

15 of 84

How Many Unique Images?

Each pixel can be one of 17 million distinct colors

(17 million)n

Sahami, Piech, Yan, Cain, Woodrow, Gregg

16 of 84

Sahami, Piech, Yan, Cain, Woodrow, Gregg

17 of 84

How Many Unique Images?

Each pixel can be one of 17 million distinct colors

(17 million)n

Sahami, Piech, Yan, Cain, Woodrow, Gregg

18 of 84

Orderings of Letters

  • How many letter orderings are possible for the following string?�

CHRIS�

18

🤔

Sahami, Piech, Yang, Cain, Woodrow, Gregg

19 of 84

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

20 of 84

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

21 of 84

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

22 of 84

To the code!

22

Sahami, Piech, Yang, Cain, Woodrow, Gregg

23 of 84

Permutations

23

Sahami, Piech, Yang, Cain, Woodrow, Gregg

24 of 84

Unique 6-digit passcodes with six smudges

24

Sahami, Piech, Yang, Cain, Woodrow, Gregg

25 of 84

Unique 6-digit passcodes with six smudges

25

Sahami, Piech, Yang, Cain, Woodrow, Gregg

26 of 84

Summary of Combinatorics

26

Sahami, Piech, Yang, Cain, Woodrow, Gregg

27 of 84

Summary of Combinatorics

27

Sahami, Piech, Yang, Cain, Woodrow, Gregg

28 of 84

Unique Bit Strings

28

1, 0, 1, 0, 0

Sahami, Piech, Yang, Cain, Woodrow, Gregg

29 of 84

Sort n distinct objects

29

Sahami, Piech, Yang, Cain, Woodrow, Gregg

30 of 84

Sort n distinct objects

30

Sahami, Piech, Yang, Cain, Woodrow, Gregg

31 of 84

Sort n distinct objects

31

Sahami, Piech, Yang, Cain, Woodrow, Gregg

32 of 84

Sort n distinct objects

32

Sahami, Piech, Yang, Cain, Woodrow, Gregg

33 of 84

Number of ways to sort coke cans

33

Sahami, Piech, Yang, Cain, Woodrow, Gregg

34 of 84

Sort semi-distinct objects

34

Order 𝑛

distinct objects

n!

Sahami, Piech, Yang, Cain, Woodrow, Gregg

35 of 84

Sort semi-distinct objects

35

Sahami, Piech, Yang, Cain, Woodrow, Gregg

36 of 84

Sort semi-distinct objects

36

Sahami, Piech, Yang, Cain, Woodrow, Gregg

37 of 84

BOBA

37

Sahami, Piech, Yang, Cain, Woodrow, Gregg

38 of 84

Strings

38

Sahami, Piech, Yang, Cain, Woodrow, Gregg

39 of 84

Strings

39

Sahami, Piech, Yang, Cain, Woodrow, Gregg

40 of 84

Strings

40

Sahami, Piech, Yang, Cain, Woodrow, Gregg

41 of 84

Strings

41

Sahami, Piech, Yang, Cain, Woodrow, Gregg

42 of 84

To the code

42

Sahami, Piech, Yang, Cain, Woodrow, Gregg

43 of 84

To the code

43

Sahami, Piech, Yang, Cain, Woodrow, Gregg

44 of 84

General Approach to counting permutations

44

This is called “multiset permutations”

Sahami, Piech, Yang, Cain, Woodrow, Gregg

45 of 84

Sort semi-distinct objects

45

Sahami, Piech, Yang, Cain, Woodrow, Gregg

46 of 84

Sort semi-distinct objects

46

Sahami, Piech, Yang, Cain, Woodrow, Gregg

47 of 84

Sort semi-distinct objects

47

Also:

Sahami, Piech, Yang, Cain, Woodrow, Gregg

48 of 84

Strings

48

Sahami, Piech, Yang, Cain, Woodrow, Gregg

49 of 84

Strings

49

Sahami, Piech, Yang, Cain, Woodrow, Gregg

50 of 84

Summary of Combinatorics

50

Sahami, Piech, Yang, Cain, Woodrow, Gregg

51 of 84

Combinations

52 of 84

Summary of Combinatorics

52

Sahami, Piech, Yang, Cain, Woodrow, Gregg

53 of 84

Combinations with cake

  •  

53

Consider the following generative process…

Sahami, Piech, Yang, Cain, Woodrow, Gregg

54 of 84

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

55 of 84

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

56 of 84

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

57 of 84

Combinations with cake

  •  

57

 

 

 

  1. Allow cake group to mingle

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

58 of 84

Combinations with cake

  •  

58

  1. Allow non-cake group to mingle

 

 

6

7

8

10

9

11

12

13

14

15

16

17

18

20

19

 

 

  1. Allow cake group to mingle

k! different permutations lead to the same mingle

Sahami, Piech, Yang, Cain, Woodrow, Gregg

59 of 84

Combinations with cake

  •  

59

  1. Allow non-cake group to mingle

 

 

 

 

 

 

  1. Allow cake group to mingle

k! different permutations lead to the same mingle

(n-k)! different permutations lead to the same mingle

Sahami, Piech, Yang, Cain, Woodrow, Gregg

60 of 84

Combinations

  •  

60

 

 

  1. Overcounted:�any ordering of chosen group is same choice
  1. Overcounted:�any ordering of unchosen group is same choice

Sahami, Piech, Yang, Cain, Woodrow, Gregg

61 of 84

Combinations

Sahami, Piech, Yang, Cain, Woodrow, Gregg

62 of 84

Probability textbooks

Sahami, Piech, Yang, Cain, Woodrow, Gregg

63 of 84

Probability textbooks

Sahami, Piech, Yang, Cain, Woodrow, Gregg

64 of 84

How many unique hands of 5 cards are there in a 52 card deck?

Sahami, Piech, Yang, Cain, Woodrow, Gregg

65 of 84

Probabilities with Equally Likely Outcomes

66 of 84

Redux from Day 1:

Sahami, Piech, Yang, Cain, Woodrow, Gregg

67 of 84

Straight Poker Hand (equally likely outcomes)

Consider 5 card poker hands.

  • A straight is 5 consecutive rank cards of any suit
  • An ace can be the lowest card, and and ace can also the highest card (2 3 4 5 6 7 8 9 10 J Q K A)
  • But, straights do not wrap around (e.g., Q K A 2 3 is not a straight)
  • There is no inherent ordering, except that all cards must be consecutive (e.g., 2C 3S 4D 5C 6H is the same as 2C 4D 5C 3S 6H)

Sahami, Piech, Yang, Cain, Woodrow, Gregg

68 of 84

Straight Poker Hand (equally likely outcomes)

  • The sample space is the number of ways you can choose five cards from 52 (which we just did):

Sahami, Piech, Yang, Cain, Woodrow, Gregg

69 of 84

Straight Poker Hand (equally likely outcomes)

  • There are 10 numbered cards to start with (A, 2, 3, 4, 5, 6, 7, 8, 9, 10)
  • (alternately, if you are going top to bottom, A, K, Q, J, 10, 9, 8, 7, 6, 5)
  • Each of the five cards in the straight have four choices for the suit
  • So, the number of events for a straight are:
  • Remember, ordering does not matter! We are dealing with sets of cards.

Sahami, Piech, Yang, Cain, Woodrow, Gregg

70 of 84

Straight Poker Hand (equally likely outcomes)

Sahami, Piech, Yang, Cain, Woodrow, Gregg

71 of 84

Straight Poker Hand (equally likely outcomes)

  • Poker aficionados will note that in poker there is a difference between a straight and a straight flush, which is a 5-card run in the same suit.
  • If we want to just calculate the probability for non-straight flush straights, we have to remove the four possible straight flushes for each of the ten starting cards:
  • The probability of getting a straight flush is vanishingly small!

Sahami, Piech, Yang, Cain, Woodrow, Gregg

72 of 84

Calculating probabilities in a real Texas Hold 'Em match?

  • If you ever watch Texas Hold 'Em poker on TV, you’ll see that they quickly calculate the odds on-screen to show the likely winners and losers.
  • Most of the time (unless they are right at the end, when exact probabilities are simple), the calculations are simulated:
    • Randomly deal the remaining cards thousands (or millions) of times
    • Evaluate each simulated outcome
    • Count wins/ties/losses
    • Calculate wins / total simulations, like we did on day 1!

Sahami, Piech, Yang, Cain, Woodrow, Gregg

73 of 84

Super Critical Problem!

74 of 84

To the Course Reader!!

74

Sahami, Piech, Yang, Cain, Woodrow, Gregg

75 of 84

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?

76 of 84

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?

77 of 84

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?

78 of 84

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?

79 of 84

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?

80 of 84

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?

81 of 84

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?

82 of 84

Chip Defect Detection

83 of 84

Chip Defect Detection

84 of 84

  • Say the population of Stanford is 17,000 people
    • You are friends with 80 people.
    • Walk into a room, see 450 random people. Independently.
    • What is the probability that you see someone you know?
    • Assume you are equally likely to see each person at Stanford

​

​

Serendipity