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

Hi Max. There are a couple of ways to do it.
Given the op's initial question in reverse...

We are given 1,405,869 out of a possible 3,838,380.

We are first looking for x1 in {x1,_,_,_,_,_}.

We need to first sum the Combin's that don't exceed our number. We could
use code that works with the number (3838380 - 1405869), but we'll use
1405869 here.

Unfortunately, there is no Analytical solution to a Binomial Sum (Sum of
Excel's Combin function ) that allows a 1-liner for this particular problem.
(AFAIK!).
Therefore, we have to resort to a Loop. (Ahh!)

Here's a demo of the first number...

Sub Demo()
Dim n, k, m, j, t
Dim Rt ' Running Total
Dim Remem ' Remember

With WorksheetFunction
n = 1405869
k = 6
m = 40

Rt = 0
j = 1
Do
Remem = Rt
t = .Combin(m - j, k - 1)
Rt = Rt + t
j = j + 1
Loop While Rt < n
j = j - 1
MsgBox "First #: " & j
End With

End Sub

For out next loop, we adjust some values such as:
n = n - Remem 'Last total that didn't exceed #
m = m - j

Eventually, the count becomes the array.
3, 9, 5, 7, 8, 4
To convert it to our Subset, we take a running total again. (ie second
number is 3+9=12)
Therefore, our KSubset is (3,12,17,24,32,36).
Again, there are a few ways to do it.
Hope this general discussion helps.

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


"Max" wrote in message
...
"Dana DeLouis" wrote:
.. Sure... Here is one way. I have it set up ...


Many thanks, Dana!
Works great

.. 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}


Yes, v.interested. Could you post a similar set-up for UnrankKSubset ?
Thanks.
--
Max
Singapore
http://savefile.com/projects/236895
xdemechanik
---