Counting: choices, orders and groups
Why this matters
Probability starts with counting: the chance of something is often the number of ways it can happen divided by the number of ways anything can happen. Listing every possibility works for three or four, and fails long before a hundred. A handful of counting rules give the number at once: multiply the choices, use a factorial for orderings, divide out repeats for groups. They also explain some hard facts about AI: why a language model can never consider every possible sentence, and why attention, which compares every word with every other, gets expensive as text gets longer. This lesson builds the rules from slots, checks them in Python, and applies them to those two questions.
- Count possibilities by filling slots left to right and multiplying the choices
- Count orderings with factorials, ordered picks with n!/(n − r)!, and groups with n!/(r!(n − r)!)
- Count with math.factorial, math.perm and math.comb, and confirm small cases with a loop
- See why a model can't try everything: sentences grow like vⁿ, and attention's table of scores like n², in Python and PyTorch
The math: fill the slots, multiply
Purpose. Know how many possibilities there are without listing them.
The counting principle. When a choice is made in steps, the number of outcomes is the product of the number of choices at each step. Fill the slots left to right:
In plain words: for each way of filling the first slot, there are n₂ ways to fill the second, and so on.
A 4-digit PIN has 10 choices per slot: 10 × 10 × 10 × 10 = 10⁴ = 10,000. A code of 3 letters then 2 digits has 26 × 26 × 26 × 10 × 10 = 1,757,600.