Construct an iterator corresponding to the number of ways to choose 5 balls from a bucket with 2 red balls, 5 green balls, and 3 black balls.
>
|
|
Print each combination. The 1s correspond to red balls, the 2s to green balls, and the 3s to black balls.
>
|
|
1: 1 1 2 2 2
2: 1 2 2 2 2
3: 2 2 2 2 2
4: 1 1 2 2 3
5: 1 2 2 2 3
6: 2 2 2 2 3
7: 1 1 2 3 3
8: 1 2 2 3 3
9: 2 2 2 3 3
10: 1 1 3 3 3
11: 1 2 3 3 3
12: 2 2 3 3 3
| |
Compute the number of ways to select four entrees from a menu of 10 dishes allowing multiple selections of any of the entrees. Because four is the most that can be selected, this is equivalent to assigning four as the multiplicity of all items.
>
|
|
Suppose one is allowed to choose any entree no more than twice. This can be expressed as
>
|
|
There are 615 ways to obtain such a combination.
|
Number of Distinct Ranks of a Poker Hand
|
|
•
|
A standard poker hand consists of five cards drawn from a 52 card deck, with four suits of 13 cards per suit.
|
•
|
The rank of a hand depends first on its category (straight flush, four of a kind, etc.), then on its rank within that category. The rank does not depend on the suits, with the exception that a flush (all five cards of the same suit) is a separate category.
|
•
|
The number of distinct hand ranks equals the number of possible flushes of a given suit plus the number of multicombinations of five cards from a deck with four cards of each rank.
|
•
|
The number of possible flushes of a given suit is simply the number of ways to choose a set of five objects from a set of thirteen distinct objects. The binomial function computes the result, . It can also be computed using the Number method of the Combination object:
|
>
|
|
•
|
Construct an iterator that generates each of the multicombinations of five cards drawn from a deck.
|
>
|
|
•
|
Count the number of multicombinations. We can do this in a number of ways; one is simply to invoke the Number method.
|
•
|
For this case, it is also practical to count the iterations one by one.
|
•
|
A final method is the following. If there were five of each ranked card, rather than four, we would obtain exactly 13 more distinct ranked hands then the actual deck (the 13 five of a kind). This is expressed by the following formula:
|
>
|
|
•
|
The total number of distinct ranks of poker hands is
|
|