⚙️ Setup¶
import itertools
from math import factorial, perm, comb
print("ready")
ready
meals = 4 * 3 * 5
with_dessert = 4 * 3 * 5 * 2
print(f"meals = 4 x 3 x 5 = {meals}")
print(f"with a dessert = 4 x 3 x 5 x 2 = {with_dessert}")
meals = 4 x 3 x 5 = 60 with a dessert = 4 x 3 x 5 x 2 = 120
Answer: each independent choice multiplies, so 4 x 3 x 5 = 60 meals, and adding a 2-way dessert choice doubles it to 120. The multiplication principle scales effortlessly: every new decision just multiplies in its own number of options.
n, r = 10, 3
print(f"P({n},{r}) = {n}!/({n}-{r})! = {perm(n,r)}")
print("check by enumeration:", len(list(itertools.permutations(range(n), r))))
P(10,3) = 10!/(10-3)! = 720 check by enumeration: 720
Answer: the three roles are different, so order matters and this is a permutation: P(10,3) = 10 x 9 x 8 = 720. (The president has 10 candidates, then 9 remain for VP, then 8 for treasurer.) If the three positions were interchangeable, it would instead be a combination.
print(f"committees = C(10,3) = {comb(10,3)}")
print(f"handshakes = C(10,2) = {comb(10,2)} (each handshake is a pair)")
committees = C(10,3) = 120 handshakes = C(10,2) = 45 (each handshake is a pair)
Answer: a committee has no internal order, so it is a combination: C(10,3) = 120, exactly the permutation 720 divided by 3! = 6. Handshakes are unordered pairs, so C(10,2) = 45. Whenever you are selecting a group rather than arranging one, reach for the combination.
pw = perm(26,4) # order matters: ABCD differs from DCBA
pizza = comb(12,4) # order does not matter on a pizza
print(f"(a) password, order MATTERS -> P(26,4) = {pw:,}")
print(f"(b) toppings, order IGNORED -> C(12,4) = {pizza}")
(a) password, order MATTERS -> P(26,4) = 358,800 (b) toppings, order IGNORED -> C(12,4) = 495
Answer: (a) a password is ordered, ABCD is not DCBA, so it is a permutation: P(26,4) = 358,800. (b) pizza toppings are an unordered set, so it is a combination: C(12,4) = 495. The deciding question is always the same: would reordering the same items count as a different outcome?
hands = comb(52,5)
# 13 ranks for the quad, then the 5th card is any of the remaining 48
four_kind = 13 * comb(48,1)
p = four_kind / hands
print(f"total hands = C(52,5) = {hands:,}")
print(f"four-of-a-kind = 13 x 48 = {four_kind:,}")
print(f"P(four-of-a-kind)= {four_kind}/{hands} = {p:.8f} (about 1 in {round(1/p):,})")
total hands = C(52,5) = 2,598,960 four-of-a-kind = 13 x 48 = 624 P(four-of-a-kind)= 624/2598960 = 0.00024010 (about 1 in 4,165)
Answer: count the favorable hands, choose the rank for the four-of-a-kind (13 ways), then any of the 48 remaining cards as the fifth, giving 13 x 48 = 624 hands out of C(52,5) = 2,598,960. So P ≈ 0.00024, about 1 in 4,165. Counting the favorable and total outcomes is the whole job; the probability is their ratio.