View Single Post
  #13   Report Post  
Posted to microsoft.public.excel.programming
John Coleman John Coleman is offline
external usenet poster
 
Posts: 274
Default Function for combinations

On Jan 20, 6:59*am, "Robert Crandal" wrote:
"GS" wrote in ....

Using an array index will always give you a unique ID. You can also use a
Collection object's 'Key', or the Scripting.Dictionary's 'Key' since both
of those will always be unique because they will raises an error if you
try to add dupes. (**Note** that VB's/VBA's Collection will process
considerably faster than using Scripting's Dictionary) In any case, you
need to increment a counter to create unique keys and so this brings us
back to using an array. Possibly, you'll want to use a 1 based array
rather than zero based. I use zero based because my wheeling combos are
stored in ranges where the first cell is the wheel config, which always
occupies array(0) so ticket numbering starts at array(1) on up to the
UBound. So an array with UBound of 42 contains 43 elements; 0 + 42
tickets.


Hmmmm, I'm looking for a solution that still doesn't involve the use
of array indexes.... *I'll try to rephrase my question again, but with a
different scenario. * (My bad for not being clear in the first place)

Suppose that my lottery contains the numbers 1 through 39, and only
5 numbers are drawn, which means that this lottery contains 575,757
unique combinations. * My program will NEVER attempt to generate
all possible 575,757 combos, therefore, using an array index is not an
option

All my program does at the moment is ask the user to input any 5-combo
they choose, in the form of a string or whatever. * If the user enters a
string
of "1 2 3 5 10", how can you calculate that this combo might occur at
position
#24 in the combo list (without using an array)? * How might you be able to
calculate that combo "1 2 3 4 5" occurs at position #1?? *It would be ideal
if I could calculate either the "postion number" of any combo, or simply
calculate any type of unique identifier for each combo.

Does that make more sense?

Somebody pointed me in the direction of the following website for a
solution: *http://en.wikipedia.org/wiki/Combina..._number_system
The problem is, I don't really understand much of the jargon or notations
on that webpage, but it seems to be related to the problem I have.

Sorry if I confused u again, Mr. GS.


Greetings:

I wrote the following function from that Wikipedia page. I tested it
on all 10 combinations of 3 numbers drawn from {1,2,3,4,5} and it
seems to work. The order it returns is not the same order that you
asked for in your original post but it succeeds in returning a unique
number in the range 1 to (n choose k) for all combinations of k
elements given as space-delimited strings where the numbers are listed
in increasing order:

Function CRank(combo As String) As Long
Dim r As Long, i As Long, j As Long, k As Long
Dim nums As Variant

nums = Split(combo)
k = UBound(nums)
r = 1
For i = 1 To k + 1
j = Int(nums(i - 1))
If j i Then
r = r + Application.WorksheetFunction.Combin(j - 1, i)
End If
Next i
CRank = r
End Function

Hope that helps