Thread: Combin
View Single Post
  #13   Report Post  
Posted to microsoft.public.excel.worksheet.functions
Dana DeLouis Dana DeLouis is offline
external usenet poster
 
Posts: 947
Default Combin

Hi Max. Sure...Here is one way. I have it set up where the size of the set
is listed first. The size of the Subset is assumed from the remaining
numbers.
The output is:

1,405,869
0 Seconds

Sub TestIt()
Dim Start
Start = Timer
Debug.Print FormatNumber(RankKSubset(40, 3, 12, 17, 24, 32, 36), 0,
True)
Debug.Print Timer - Start & " Seconds"
End Sub

Function RankKSubset(Size, ParamArray v())
'//= = = = = = = = = = = = = = = = = =
'// By: Dana DeLouis
'//= = = = = = = = = = = = = = = = = =

Dim j As Long
Dim d As Double
Dim n As Double
Dim m As Double
Dim T As Double

With WorksheetFunction
m = Size
n = UBound(v) + 1
d = v(0)

T = T + .Combin(m, n) - .Combin(m - d + 1, n)
For j = 1 To UBound(v) - 1
m = m - d
n = n - 1
d = v(j) - v(j - 1)
T = T + .Combin(m, n) - .Combin(m - d + 1, n)
Next j
T = T + v(j) - v(j - 1)
End With
RankKSubset = T
End Function

If you are interested in this subject, one can also "UnRank" a number in a
similar fashion.
For example, the 3 Millionth combination in you large list is:

UnrankKSubset(3000000,6,40)

{9, 12, 22, 23, 27, 39}

--
HTH. :)
Dana DeLouis
Windows XP, Office 2003


"Max" wrote in message
...
"Dana DeLouis" wrote:
... The function name is usually called "RankKSubset."
The solution code is called recursively using Combin.
I get the same solution as you in 0 seconds.


Marvellous, Dana ! Thanks for dropping by, and confirming on the answer
<g.
Believe your solution is exactly what the OP was looking for here,
re OP's orig. post:
.. which combination the numbers 3,12,17,24,32,36 represent
without having to create all possible combinations
and then looking it up.


There are different ways to set it up...
Debug.Print RankKSubset(40, 3, 12, 17, 24, 32, 36)
1,405,869


Could you kindly post the function: RankKSubset ? Tried trawling google
for
it but got no hits other than your earlier post in this thread. Thanks.
--
Max
Singapore
http://savefile.com/projects/236895
xdemechanik
---