Lexicographic Index Number To Combination For C(n,6)

PAB

Member
Hi everyone,

I was looking through the archives today and saw a request for a formula or Macro to calculate the Combination from a Lex number.
Unfortunately I couldn't find it again to paste the link.
Anyway, here is a Macro that will calculate this.
Change the nMaxF value as needed.
Put the Lex number in cell A1 and run the Macro below.

Option Explicit
Option Base 1

Const nMinA As Integer = 1
Const nMaxF As Integer = 49

Sub Lex2Combination()
Dim A As Integer, B As Integer, C As Integer, D As Integer, E As Integer, F As Integer
Dim nVal As Double, nLex As Double
With Application
.ScreenUpdating = False: .Calculation = xlCalculationManual: .DisplayAlerts = False
End With
Range("A1").Select
nVal = Range("A1").Value
nLex = 0
For A = nMinA To nMaxF - 5
For B = A + 1 To nMaxF - 4
For C = B + 1 To nMaxF - 3
For D = C + 1 To nMaxF - 2
For E = D + 1 To nMaxF - 1
For F = E + 1 To nMaxF
nLex = nLex + 1
If nLex = nVal Then
Range("B1").Value = A: Range("C1").Value = B
Range("D1").Value = C: Range("E1").Value = D
Range("F1").Value = E: Range("G1").Value = F
Exit Sub
End If
Next F: Next E: Next D: Next C: Next B: Next A
With Application
.DisplayAlerts = True: .Calculation = xlCalculationAutomatic: .ScreenUpdating = True
End With
End Sub

Have FUN!

I do HATE the fact that we can't post using the Code tags which would correctly indent the code.

Regards,
PAB
:wavey:

-∏-∏-∏-∏-∏-∏-∏-∏-∏-∏-∏-∏-∏-∏-∏-∏-∏-∏-∏-∏-∏-∏-∏-∏-∏-∏-∏-∏-∏-∏-∏-∏-∏-∏-∏-∏-∏-∏-∏-∏-∏-∏-∏-∏-
12:45, restate my assumptions.
Mathematics is the language of nature.
Everything around us can be represented and understood through numbers.
If you graph the numbers of any system, patterns emerge. Therefore, there are patterns everywhere in nature.
 

PAB

Member
For anyone who is interested in an Excel formula to convert a 6 number combination into a CSN ( Combination Sequence Number ) here is the Formula.
Enter the six numbers of the combination to be converted into cells A1:F1 and in cell H1 enter the Formula:

=COMBIN(49,6)-IF(49-5-A1>0,COMBIN(49-A1,6),0)-IF(49-4-B1>0,COMBIN(49-B1,5),0)-IF(49-3-C1>0,COMBIN(49-C1,4),0)-IF(49-2-D1>0,COMBIN(49-D1,3),0)-IF(49-1-E1>0,COMBIN(49-E1,2),0)-IF(49-F1>0,COMBIN(49-F1,1),0)

Have fun!

Regards,
PAB
:wavey:

-∏-∏-∏-∏-∏-∏-∏-∏-∏-∏-∏-∏-∏-∏-∏-∏-∏-∏-∏-∏-∏-∏-∏-∏-∏-∏-∏-∏-∏-∏-∏-∏-∏-∏-∏-∏-∏-∏-∏-∏-∏-∏-∏-∏-
12:45, restate my assumptions.
Mathematics is the language of nature.
Everything around us can be represented and understood through numbers.
If you graph the numbers of any system, patterns emerge. Therefore, there are patterns everywhere in nature.
 

Sidebar

Top