Combinatorial Index

Published:

Updated:

We posted this in the topic "Excel help need Combination FROM Lexicographix number" in the Mathematics Forum.

Adjust the Data Types on different programming platforms.

The functions may error out on very large Combinatorial Sets.

_______________________________________________________

Ok, Here It Is.

You can download the Excel file at ftp://www.jadexcode.com/excel

The file name is CombinatorialIndex.xls

The VB Macro Source Code is shown below

Function Fact(N As Integer) As Double
    If (N < 1) Then
        Fact = 1
    Else
        Fact = N * Fact(N - 1)
    End If
End Function

Function Perm(N As Integer, R As Integer) As Double
    If (N < R) Then
        Perm = 0
    Else
        Perm = Fact(N) / Fact(N - R)
    End If
End Function

Function Comb(N As Integer, R As Integer) As Long
    If (N < R) Then
        Comb = 0
    Else
        Comb = Perm(N, R) / Fact(R)
    End If
End Function

Function Cdist(N As Integer, R As Integer, C As Integer, Z As Integer) As Long
    If (Z < C) Or (Z > (N - R + C)) Or (Z > N) Or (C > R) Or (N < 1) Or (R < 1) Or (C < 1) Or (Z < 1) Then
        Cdist = 0
    Else
        Cdist = Comb(Z - 1, C - 1) * Comb(N - Z, R - C)
    End If
End Function

Function fColumnSum(N As Integer, R As Integer, Z As Integer) As Long
    Dim a As Integer
   
    If Z < 1 Then
        fColumnSum = 0
    ElseIf (Z >= 1) And (Z < N - R + 1) Then
        col_sum = 0
        For a = 1 To Z
            col_sum = col_sum + Cdist(N, R, 1, a)
        Next a
        fColumnSum = col_sum
    ElseIf Z >= N - R + 1 Then
        fColumnSum = Comb(N, R)
    End If
End Function

Function Index2Combin(N As Integer, R As Integer, I As Double) As String

    Dim Combination(), Z As Integer
    ReDim Combination(R)
    Dim tmpString As String
    Dim NumberFound As Boolean
   
    tmpString = ""
    I = I - 1
    Z = 0
   
    For a = 1 To R
   
        If I <= Comb(N, R) Then
            If a = 1 Then
                Combination(a) = 1
            Else
                Combination(a) = Combination(a - 1) + 1
            End If
       
            NumberFound = False
       
            Do
       
                Select Case (I - fColumnSum(N - Z, R - (a - 1), Combination(a) - Z - 1))
                    Case Is < 0
                        Combination(a) = Combination(a) - 1
                        NumberFound = True
                    Case Is = 0
                        NumberFound = True
                    Case Is > 0
                        Combination(a) = Combination(a) + 1
                End Select
       
            Loop Until NumberFound
       
            I = I - fColumnSum(N - Z, R - (a - 1), Combination(a) - Z - 1)
            Z = Combination(a)
           
        Else
       
            Combination(a) = 0
           
        End If
       
        tmpString = tmpString & Format(Combination(a), "00")
        If a < R Then tmpString = tmpString & " "
   
    Next a
   
    Index2Combin = tmpString

End Function

Function Combin2Index(N As Integer, R As Integer, theRange As Range) As Long
   
    Dim a As Integer
    Dim fSum As Double
    Dim NotInAscendingOrder, NotInPool As Boolean
    NotInAscendingOrder = False
    NotInPool = False
   
    If (theRange.Rows.Count <> 1) Or (theRange.Columns.Count <> R) Then
        Combin2Index = -1
        Exit Function
    End If
   
    For a = 1 To R
        If a < R Then
            If theRange.Cells(1, a) >= theRange.Cells(1, a + 1) Then
                NotInAscendingOrder = True
            End If
        End If
       
        If (theRange.Cells(1, a) < 1) Or (theRange.Cells(1, a) > N) Then
            NotInPool = True
        End If
    Next a
   
    If NotInAscendingOrder Or NotInPool Then
        Combin2Index = -1
        Exit Function
    End If
   
   
    fSum = fColumnSum(N, R, theRange.Cells(1, 1) - 1) + 1
    For a = 2 To R
        fSum = fSum + fColumnSum(N - theRange.Cells(1, a - 1), R - a + 1, theRange.Cells(1, a) - theRange.Cells(1, a - 1) - 1)
    Next a
    Combin2Index = fSum
   
End Function

Entry #1,066

Comments

This Blog entry currently has no comments.

Post a Comment

Please Log In

To use this feature you must be logged into your Lottery Post account.

Not a member yet?

If you don't yet have a Lottery Post account, it's simple and free to create one! Just tap the Register button and after a quick process you'll be part of our lottery community.

Register