Combination from Lexicographic number.

serge

Well-known Member
Joined
Oct 8, 2008
Messages
1,402
Office Version
  1. 2007
Platform
  1. Windows
I'm looking for a formula that will be able to return a combination set of 5 numbers from it's lexicographic number.


For a game of 5/39 meaning 5 balls are draw from a pool of 39 balls.
This game have a total of 575757 combinations and each combination of 5 numbers are numbered from 1 to 575757 which is the " lexicographic number " meaning the rank of the combination in the whole possible combinations for this game ( I hope I make sense ? )


So the lexico # : 1 would have the 1st combination : 1 2 3 4 5


the lexico # : 2 would have the 2nd combination : 1 2 3 4 6


etc.. up to the last lexico # 575757 would have the last combination : 35 36 37 38 39


My question is, What formula would be able to return the 5 numbers combination from the Lexico number ?


Assuming my lexico is in cell A1 what would be the numbers from C1:G1 ?


Thank you.
Serge.
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
I also have this excel file but there is an error somewhere when I type the lexico number : 501456 it should return 13 17 19 27 39, but it doesn't and return instead : 13 17 19 28 28 which is wrong.

here's the link if someone want to look at it.

https://app.boxcn.net/s/jdhdvljlzhfi20nfsuma02s2hc9zxd9o

Thank you.
Serge.
 
Upvote 0
Your COMBIN function is occasionally producing results with very tiny rounding differences to the correct integer values, and hence your VLOOKUPs will occasionally get the wrong value.

Try rounding your lookup table to zero places, and I think your formulae will always work?

I'd also simplify your formulae like this:

A1: =N+1-MATCH(NoCombinations-NoReqd,M1:M40,1)
B1: =N+1-MATCH(NoCombinations-NoReqd-INDEX(M1:M40,N+1-A1),N1:N40,1)
C1: =N+1-MATCH(NoCombinations-NoReqd-INDEX(M1:M40,N+1-A1)-INDEX(N1:N40,N+1-B1),O1:O40,1)
D1: =N+1-MATCH(NoCombinations-NoReqd-INDEX(M1:M40,N+1-A1)-INDEX(N1:N40,N+1-B1)-INDEX(O1:O40,N+1-C1),P1:P40,1)
E1: =N+1-MATCH(NoCombinations-NoReqd-INDEX(M1:M40,N+1-A1)-INDEX(N1:N40,N+1-B1)-INDEX(O1:O40,N+1-C1)-INDEX(P1:P40,N+1-D1),Q1:Q40,1)


Excel 2010
ABCDEFGHIJKLMNOPQ
11317192739501,456KNc(n,k)00000
2Change Lexi Here539575,75700001
300012
400133
501464
61510105
761520156
8213535217
9567056288
1012612684369
112522101204510
124623301655511
137924952206612
1412877152867813
15200210013649114
163003136545510515
174368182056012016
186188238068013617
198568306081615318
2011628387696917119
21155044845114019020
Lexi to Combination
 
Upvote 0
And I know you said formula, but if you're also open to VBA then:

B1:F1 {=GetNthCombination(39,5,A1)} array-entered


Excel 2010
ABCDEF
1501,4561317192739
Sheet1


Code:
Function GetNthCombination(N As Long, r As Long, ByVal cCombinationNo As Currency) As Long()

    Dim lCombination() As Long, i As Long, j As Long
    Dim cTemp As Currency
    ReDim lCombination(1 To r)

    For i = 1 To r
        Do
            j = j + 1
            cTemp = Round(Application.Combin(N - j, r - i), 0)
            If cCombinationNo <= cTemp Then
                lCombination(i) = j
                Exit Do
            End If
            cCombinationNo = cCombinationNo - cTemp
        Loop
    Next i

    GetNthCombination = lCombination

End Function
 
Upvote 0
I don't know if this Sub helps!

Code:
Option Explicit
Option Base 1

Const MinA As Integer = 1
Const MaxF As Integer = 39
 
Sub Lex2Combination()
    Dim A As Long, B As Long, C As Long, D As Long, E As Long
    Dim nVal As Double, nLex As Double
    With Application
        .ScreenUpdating = False: .Calculation = xlCalculationManual: .DisplayAlerts = False
    End With
    Cells(1, 1).Select
    nVal = Range("G1").Value
    nLex = 0
    For A = MinA To MaxF - 4
        For B = A + 1 To MaxF - 3
            For C = B + 1 To MaxF - 2
                For D = C + 1 To MaxF - 1
                    For E = D + 1 To MaxF
                        nLex = nLex + 1
                        If nLex = nVal Then
                            Range("A1").Value = A
                            Range("B1").Value = B
                            Range("C1").Value = C
                            Range("D1").Value = D
                            Range("E1").Value = E
                            Exit Sub
                        End If
                    Next E
                Next D
            Next C
        Next B
    Next A
    With Application
        .DisplayAlerts = True: .Calculation = xlCalculationAutomatic: .ScreenUpdating = True
    End With
End Sub
 
Upvote 0
Hi Stephen,

Thank you for the reply, and your explanations but this is Chinese to me, I didn't create that table someone else did.

What would be the actual formulas to put in those cells ?

I don't mind using code, but rather formulas, it's easier for me, since I don't know how to use code.

See, I know exactly what I need for what I do, but I have no clue how to built a formula or macro.
 
Upvote 0
Thanks SHADO, for the code but like I said above, don't know how to use it.
 
Upvote 0
Insert the code into a standard module.
From the Developer tab click on the insert tab and click the top left button under Form Controls and then click in the Worksheet and click the Macro name.
Then you can change the lexicographic number in the Worksheet and then click the button to run the combination associated with the lexicographic number.
I hope this helps!
 
Upvote 0
What would be the actual formulas to put in those cells ?

The formulae were in the workbook that you posted! The only problem with this workbook is that the COMBIN function occasionally produces very tiny rounding differences to the correct integer values, which affects your VLOOKUPs.

You can fix this by rounding to zero decimal places, i.e.

M1: <i$2,0,round(combin($r1,$i$2-0),0))
=IF(R1+0< I$2,0,ROUND(COMBIN($R1,$I$2-0),0))
N1: <i$2,0,round(combin($r1,$i$2-1),0))
=IF(R1+1< I$2,0,ROUND(COMBIN($R1,$I$2-1),0))
O1: <i$2,0,round(combin($r1,$i$2-2),0))
=IF(R1+2< I$2,0,ROUND(COMBIN($R1,$I$2-2),0))
P1: <i$2,0,round(combin($r1,$i$2-3),0))
=IF(R1+3< I$2,0,ROUND(COMBIN($R1,$I$2-3),0))
Q1: <i$2,0,round(combin($r1,$i$2-4),0))
<i$2,0,round(combin($r1,$i$2-4),0))
=IF(R1+4< I$2,0,ROUND(COMBIN($R1,$I$2-4),0))

and copy down columns M:Q.

If you make this simple change, your original workbook will work just fine.

Alternatively I've proposed some simpler formulae, e.g.

Your E1: =IF(G1=0,"",$J$2-($K$2-G1-(VLOOKUP($K$2-G1,$M$1:$R$56,1))-VLOOKUP($K$2-G1-(VLOOKUP($K$2-G1,$M$1:$R$56,1)),$N$1:$R$56,1)-VLOOKUP($K$2-G1-(VLOOKUP($K$2-G1,$M$1:$R$56,1))-VLOOKUP($K$2-G1-(VLOOKUP($K$2-G1,$M$1:$R$56,1)),$N$1:$R$56,1),$O$1:$R$56,1)-VLOOKUP($K$2-G1-(VLOOKUP($K$2-G1,$M$1:$R$56,1))-VLOOKUP($K$2-G1-(VLOOKUP($K$2-G1,$M$1:$R$56,1)),$N$1:$R$56,1)-VLOOKUP($K$2-G1-(VLOOKUP($K$2-G1,$M$1:$R$56,1))-VLOOKUP($K$2-G1-(VLOOKUP($K$2-G1,$M$1:$R$56,1)),$N$1:$R$56,1),$O$1:$R$56,1),$P$1:$R$56,1)))

My E1: =N+1-MATCH(NoCombinations-NoReqd-INDEX(M1:M40,N+1-A1)-INDEX(N1:N40,N+1-B1)-INDEX(O1:O40,N+1-C1)-INDEX(P1:P40,N+1-D1),Q1:Q40,1)

I have also changed the lookup table formula slightly:

M1: =IFERROR(ROUND(COMBIN($Q1,COLUMN($Q1)+1-COLUMN()),0),0)
(Copy down and across to column P)</i$2,0,round(combin($r1,$i$2-4),0))
</i$2,0,round(combin($r1,$i$2-4),0))
</i$2,0,round(combin($r1,$i$2-3),0))
</i$2,0,round(combin($r1,$i$2-2),0))
</i$2,0,round(combin($r1,$i$2-1),0))
</i$2,0,round(combin($r1,$i$2-0),0))
 
Upvote 0
Thanks to both of you.

You guys are incredible and quick, it's about 5 am here in Los Angeles, and just finished working, so I'm going to bed and will use your formulas and macro tomorrow.

Thank you for you precious help, I really appreciate it.
serge.
 
Upvote 0

Forum statistics

Threads
1,214,520
Messages
6,120,008
Members
448,935
Latest member
ijat

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top