A combination without replacement selects items from a set when each item can be chosen at most once and order does not matter. Lottery numbers are a typical case: drawing the same numbers in a different order does not produce a different selection. First check both conditions. If order matters, count permutations instead; if items can be selected again, use combinations with replacement. section 2.2.3 section 2.5
For n distinct items and r selections, start with n!/(n − r)! permutations without replacement. Each unordered selection appears r! times among those ordered arrangements, so divide by r!: C(n, r) = n!/[r!(n − r)!]. With five items and three selections, there are 60 ordered arrangements but only 60/6 = 10 combinations. section 2.2.3
>>> n = 5
>>> r = 3
>>> combinations = (math.factorial(n) / \
>>> (math.factorial(r) * math.factorial(n - r)))
>>> print(f'Number of combinations without replacement: '
>>> f'{combinations}')
Number of combinations without replacement: 10.0
>>> combinations = math.comb(n, r)
>>> print(f'Number of combinations without replacement: '
>>> f'{combinations}')
Number of combinations without replacement: 10
The Python listing calculates the five-choose-three example using factorials and math.comb(). Both give 10; the factorial expression displays 10.0 because it uses division. section 2.2.3
Exactly two heads in five flips
Choose which two of the five flip positions contain heads. Their order within that choice does not matter, and a position cannot be chosen twice, so math.comb(5, 2) gives 10 favorable sequences. Five independent coin flips have 32 possible sequences; the probability is 10/32, or about 31%. The combination counts favorable outcomes, not the total sequences. section 3.2.3
Figure 2.1 The top of Pascal’s triangle. The triangle is constructed from the top down, where numbers are derived by adding the two numbers immediately above. It so happens that the sum of numbers for each row is twice the sum of numbers from the row above. The number of combinations without replacement can be found at the intersection of the (n + 1)th row and the (r + 1)th position in that row. When n equals 5 (sixth row down) and r equals 3 (fourth position from the left), the number of combinations without replacement equals 10.
Pascal’s triangle provides another way to read the count: for n = 5 and r = 3, the sixth row’s fourth position is 10. section 2.2.3
