[Home] [Puzzles & Projects] [Delphi Techniques] [Math topics] [Library] [Utilities]
|
|
|
In general for selecting R things from N there are N x (N-1) x (N-2) ... (N-R+1) permutations. This is often written using factorial notation. N factorial, written N!, is the product of consecutive integers from 1 to N. The number of permutations can be expressed as N! / (N-R)! In our example, 2 of 18 can be written 18! / 16! or 18 x 17. |
The number of possible arrangements of holes for each a, b, c set is a little more complicated. Assume that we need to select all possible arrangements of sets of two par 3 holes. My first thought was that we have 18 choices for the first selection and 17 choices for the second, or 18 X 17 = 306 ways total. This is the number of permutations of 2 things selected from 18 .
But ... notice that the 306 permutations includes the pairs (hole1, hole2) and (hole2, hole1) for any two holes. These shouldn't count twice since the order in which we selected the par 3 holes doesn't matter to the player (he'll play from 1 through 18 regardless of how we assigned the par values to the holes). The phrase "order doesn't matter" is our clue that we actually need the number of combinations, the unique ways we can select 2 things from 18. The number of combinations is the number of permutations with the duplicates removed. How many duplicates are there? For each pair (x, y) selected in our 306 choices there is a corresponding pair (y, x), so we need to divide the number of permutations by 2. If we were selecting 3 holes, there would be 6 duplicates of each triplet selected (x, y ,z), (x, z, y), (y, x, z), (y, z, x), (z, x, y), (z, y, x) so we would have to divide the permutation count by 6. In general when selecting R things there will be R! duplicates. Since the number of permutations is N! / (N-R)!, the number of combinations is sometimes formulated as (N!) / ((N-R)! x R!).
In the Permutes and Combinations functions in this program that we won't use the factorial formulas - instead we'll just multiply or divide by the index of a loop as necessary to compute the result.
So far we've only discussed selecting the number of unique arrangements for one of the hole sizes. Say we need a par 3's, the number is given by Combinations(a,18). To complete the calculation we need to select say b par 4's from the remaining 18-a holes. This is given by Combinations(b,18-a). The c par 5's can only be selected in one way, since c is 18-a-b by definition and there are only 18-a-b holes remaining. The total ways we can select the par 3's and 4's is the product of the two combination values. Answers should be the same of course, no matter which two hole sizes are used for these calculations.
![]() | Browse source extract |
![]() | Download source |
![]() | Download executable |
![]() |
You could add
code to check for yourself that it doesn't matter which pair of hole sizes
are used in the calculation, or in which order. In other words if
the number of holes of par 3, 4, and 5 is given by a, b, c; it
should be the case that:
Combinations(a,18)*Combinations(b,18-a) = Combinations(b,18)*Combinations(a,18-b) = Combinations(a,18)*Combinations(c,18-a) = Combinations(c,18)*Combinations(a,18-c) = Combinations(b,18)*Combinations(c,18-b) = Combinations(c,18)*combinations(b,18-c)
|
![]() |
The Permutes and Combinations routines both need some error checking to ensure that the first parameter does not exceed the second. |
Modified: May 15, 2018
[Feedback] [Newsletters (subscribe/view)] [About me]Copyright © 2000-2018, Gary Darby All rights reserved. |