Tricky combinations generate using following distribution

Kishan

Well-known Member
Joined
Mar 15, 2011
Messages
1,648
Office Version
  1. 2010
Platform
  1. Windows
Using Excel 2000

Hi,

Formula column M shows the TT: Cobinationes could be made with each distribution as per C:K columns


Book1
CDEFGHIJKLMN
51|11|XX|11|22|1X|22|XX|X2|2SumTT:Combinations
62130100007420
73030010007140
81330000007140
9000304000735
10005020000721
11005100001742
1201221100071.260
130123010007420
Sheet1
Cell Formulas
RangeFormula
M6=SUM(C6:K6)
M7=SUM(C7:K7)
M8=SUM(C8:K8)
M9=SUM(C9:K9)
M10=SUM(C10:K10)
M11=SUM(C11:K11)
M12=SUM(C12:K12)
M13=SUM(C13:K13)
N6{=FACT(SUM(C6:K6))/PRODUCT(FACT(C6:K6))}
Press CTRL+SHIFT+ENTER to enter array formulas.


I need VBA, which can generate 420 unique combinations if I use following distribution in cells K6:S6 distribution length is 9 and sum will be always = 7

Does it is possible to get combinations with length 7 in columns C:I, only unique = 420 the example below shown just few dummy combinations in the columns C:I

Example…


Book1
CDEFGHIJKLMNOPQRSTU
1
2
3
4
5P1P2P3P4P5P6P71|11|XX|11|22|1X|22|XX|X2|2TT:Combinations
61|11|11|X2|1X|1X|1X|1213010000420
71|11|11|XX|12|1X|1X|1
81|11|11|XX|1X|12|1X|1
91|11|11|XX|1X|1X|12|1
101|11|12|1X|1X|1X|11|X
111|11|12|11|XX|1X|1X|1
121|11|12|1X|1X|11|XX|1
131|11|12|1X|11|XX|1X|1
141|11|1X|1X|12|1X|11|X
151|11|1X|11|XX|12|1X|1
161|11|1X|1X|1X|11|X2|1
171|11|1X|12|11|XX|1X|1
181|11|1X|1X|12|11|XX|1
191|11|1X|1X|1X|12|11|X
201|11|1X|12|1X|1X|11|X
211|11|1X|12|1X|11|XX|1
221|11|1X|11|XX|1X|12|1
231|11|1X|1X|11|XX|12|1
241|11|1X|11|X2|1X|1X|1
251|11|1X|1X|11|X2|1X|1
261|11|X1|1X|12|1X|1X|1
271|11|X1|1X|1X|1X|12|1
281|11|X1|12|1X|1X|1X|1
291|11|X1|1X|1X|12|1X|1
301|11|X2|1X|11|1X|1X|1
311|11|X2|1X|1X|11|1X|1
321|11|X2|1X|1X|1X|11|1
331|11|X2|11|1X|1X|1X|1
Sheet2


Thank you in advance

Regards,
Kishan
 
Hi,

Ok It come in my mind what if I change post#1 layout like follow does it is variable?

Instead putting numbers of time to be used each pattern in row K6:S6. Put them in columns A:I times to repeated each pattern

2-Pattern 1|1 in column A
1-Pattern 1|X in column B
3-Pattern X|1 in column C
1-Pattern 2|1 in column D

And create 420 sets out of total 7 Patterns in 7 columns as shown in the example below

Example shows sample output total to be generated 420 combinations or may be it called permutations


Book1
ABCDEFGHIJKLMNOPQ
1Set 1Set 2Set 3Set 4Set 5Set 6Set 7Set 8Set 9P1P2P3P4P5P6P7
21|11|XX|12|11|11|11|X2|1X|1X|1X|1
31|1X|11|11|11|XX|12|1X|1X|1
4X|11|11|11|XX|1X|12|1X|1
51|11|11|XX|1X|1X|12|1
61|11|12|1X|1X|1X|11|X
71|11|12|11|XX|1X|1X|1
81|11|12|1X|1X|11|XX|1
91|11|12|1X|11|XX|1X|1
101|11|1X|1X|12|1X|11|X
111|11|1X|11|XX|12|1X|1
121|11|1X|1X|1X|11|X2|1
131|11|1X|12|11|XX|1X|1
141|11|1X|1X|12|11|XX|1
151|11|1X|1X|1X|12|11|X
161|11|1X|12|1X|1X|11|X
171|11|1X|12|1X|11|XX|1
181|11|1X|11|XX|1X|12|1
191|11|1X|1X|11|XX|12|1
201|11|1X|11|X2|1X|1X|1
211|11|1X|1X|11|X2|1X|1
221|11|X1|1X|12|1X|1X|1
231|11|X1|1X|1X|1X|12|1
241|11|X1|12|1X|1X|1X|1
251|11|X1|1X|1X|12|1X|1
261|11|X2|1X|11|1X|1X|1
271|11|X2|1X|1X|11|1X|1
281|11|X2|1X|1X|1X|11|1
291|11|X2|11|1X|1X|1X|1
301|11|XX|1X|1X|12|11|1
311|11|XX|12|1X|1X|11|1
321|11|XX|1X|11|12|1X|1
331|11|XX|12|1X|11|1X|1
341|11|XX|11|1X|12|1X|1
351|11|XX|1X|1X|11|12|1
361|11|XX|12|11|1X|1X|1
371|11|XX|11|12|1X|1X|1
381|11|XX|1X|12|1X|11|1
391|11|XX|1X|11|1X|12|1
401|11|XX|1X|12|11|1X|1
411|11|XX|11|1X|1X|12|1
421|12|11|1X|1X|11|XX|1
431|12|11|11|XX|1X|1X|1
441|12|11|1X|11|XX|1X|1
451|12|11|1X|1X|1X|11|X
461|12|11|XX|1X|1X|11|1
471|12|11|X1|1X|1X|1X|1
481|12|11|XX|11|1X|1X|1
491|12|11|XX|1X|11|1X|1
501|12|1X|11|11|XX|1X|1
511|12|1X|1X|11|1X|11|X
521|12|1X|1X|1X|11|X1|1
531|12|1X|1X|11|11|XX|1
541|12|1X|11|XX|1X|11|1
551|12|1X|11|X1|1X|1X|1
561|12|1X|11|1X|1X|11|X
571|12|1X|11|XX|11|1X|1
581|12|1X|11|1X|11|XX|1
591|12|1X|1X|11|XX|11|1
601|12|1X|1X|11|X1|1X|1
611|12|1X|1X|1X|11|11|X
621|1X|11|12|1X|1X|11|X
631|1X|11|11|XX|1X|12|1
641|1X|11|12|11|XX|1X|1
651|1X|11|1X|11|XX|12|1
661|1X|11|1X|12|11|XX|1
671|1X|11|1X|12|1X|11|X
681|1X|11|1X|11|X2|1X|1
691|1X|11|12|1X|11|XX|1
701|1X|11|11|X2|1X|1X|1
711|1X|11|11|XX|12|1X|1
721|1X|11|1X|1X|11|X2|1
731|1X|11|1X|1X|12|11|X
741|1X|11|X2|1X|11|1X|1
751|1X|11|X2|1X|1X|11|1
761|1X|11|X1|1X|12|1X|1
771|1X|11|XX|1X|12|11|1
781|1X|11|XX|11|12|1X|1
791|1X|11|XX|1X|11|12|1
801|1X|11|X2|11|1X|1X|1
811|1X|11|X1|1X|1X|12|1
821|1X|11|X1|12|1X|1X|1
831|1X|11|XX|12|11|1X|1
841|1X|11|XX|12|1X|11|1
851|1X|11|XX|11|1X|12|1
861|1X|12|11|1X|1X|11|X
871|1X|12|11|XX|11|1X|1
881|1X|12|11|XX|1X|11|1
891|1X|12|1X|11|11|XX|1
901|1X|12|11|11|XX|1X|1
911|1X|12|1X|11|XX|11|1
921|1X|12|11|X1|1X|1X|1
931|1X|12|11|1X|11|XX|1
941|1X|12|1X|11|X1|1X|1
951|1X|12|1X|11|1X|11|X
961|1X|12|1X|1X|11|11|X
971|1X|12|1X|1X|11|X1|1
981|1X|1X|11|1X|12|11|X
991|1X|1X|1X|12|11|11|X
1001|1X|1X|11|X2|11|1X|1
Sheet3


Thank you

Regards,
Kishan

 
Last edited:
Upvote 0

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Hi Kishan,
this is your most challenging post. Looks simple but permutations with repetitions are not so easy to get as they seem.
Use this layout:
ABCDEFGHIJKLMNOPQ
11|11|XX|11|22|1X|22|XX|X2|2P1
P2P3P4P5P6P7
22311

<tbody>
</tbody>
Foglio7
In row 2 enter presences of couples in a game ticket under the obvious constrain they sum to 7 and put in a module this code:
Code:
Option Explicit
Sub PlaySlip_Peremutations()
Dim mystring As String
Dim idx2 As Long
Dim idx4 As Long
Dim Count As Long
Dim Count2 As Long
Dim Count3 As Long
Dim PermutationsArr()
Dim StartP As Long
Dim EndP As Long
Dim idxArr As Long
Dim idxLowPer As Long
Dim MyChar As String

mystring = "!!!!!!!"
For idx2 = 1 To 9
    If Cells(2, idx2).Value > 0 Then
        mystring = Replace(mystring, "!", Cells(2, idx2).Column, 1, Cells(2, idx2).Value)
    End If
Next idx2

Cells(1, 10).Activate
ReDim PermutationsArr(1 To Fact(Len(mystring)))
For Count = 1 To UBound(PermutationsArr)
    PermutationsArr(Count) = mystring
Next Count

For StartP = 1 To Len(mystring)
    idxArr = 0
    Do While idxArr < UBound(PermutationsArr)
        For EndP = StartP To Len(mystring)
            For Count = 1 To Fact(Len(mystring) - StartP + 1) / (Len(mystring) - StartP + 1)
                idxArr = idxArr + 1
                MyChar = Mid(PermutationsArr(idxArr), StartP, 1)
                Mid(PermutationsArr(idxArr), StartP) = Mid(PermutationsArr(idxArr), EndP, 1)
                Mid(PermutationsArr(idxArr), EndP) = MyChar
            Next Count
        Next EndP
    Loop
Next StartP

For idxArr = 1 To UBound(PermutationsArr)
    idxLowPer = idxArr
    For Count = idxArr To UBound(PermutationsArr)
        If PermutationsArr(Count) < PermutationsArr(idxLowPer) Then
            idxLowPer = Count
        End If
    Next Count
    MyChar = PermutationsArr(idxArr)
    PermutationsArr(idxArr) = PermutationsArr(idxLowPer)
    PermutationsArr(idxLowPer) = MyChar
Next idxArr

MyChar = "": Count2 = 0: Count3 = 1
For Count = 1 To UBound(PermutationsArr)
    If PermutationsArr(Count) <> MyChar Then
        Count2 = Count2 + 1: Count3 = Count3 + 1: MyChar = PermutationsArr(Count)
        For idx4 = 1 To 7
        Cells(Count3, 10 + idx4).Value = Cells(1, CLng(Mid(PermutationsArr(Count), idx4, 1))).Value
        Next idx4
    End If
Next Count

End Sub
Public Function Fact(i As Integer) As Long
Dim k As Integer
Fact = 1
For k = i To 2 Step -1
    Fact = Fact * k
Next k
End Function
At the moment I am playing lotto games, the one you like in Italy is named Totocalcio and was my favourite many years ago so I must think a bit to remember all the tricks. I was wondering why you use couples, once when totocalcio was the most popular game and set on 13 games (that's the reason why in my country 13 is a lucky number!), the most common result was six 1, five X, and two 2.

Have a nice weekend.
 
Last edited:
Upvote 0
Hi Kishan,
this is your most challenging post. Looks simple but permutations with repetitions are not so easy to get as they seem.
Hi, B__P, first of all I want to thank you very much for giving a great solution. Really I needed it most

Challenging yes it was because this were not normal, it were extraordinary query after I lounge the question I did 9 times bump in 10th time I change the view of question 11th time did bump and get the answer from you after the page has visited 340+ times this say much about the complicity of the question.

A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
1
1|1
1|X
X|1
1|2
2|1
X|2
2|X
X|X
2|2
P1

P2
P3
P4
P5
P6
P7
2
2
3
1
1

<tbody>
</tbody>
Foglio7
In row 2 enter presences of couples in a game ticket under the obvious constrain they sum to 7 .
I tested the code combining much combination which sum reach to 7 and find out possible combinations could be formed by the sequence using your Formula you provide me in other post also were difficult too. This says you accept the challenges. Proof is this is done (y)

Code:
Option Explicit
Sub PlaySlip_Peremutations()
Dim mystring As String
Dim idx2 As Long
Dim idx4 As Long
Dim Count As Long
Dim Count2 As Long
Dim Count3 As Long
Dim PermutationsArr()
Dim StartP As Long
Dim EndP As Long
Dim idxArr As Long
Dim idxLowPer As Long
Dim MyChar As String

mystring = "!!!!!!!"
For idx2 = 1 To 9
    If Cells(2, idx2).Value > 0 Then
        mystring = Replace(mystring, "!", Cells(2, idx2).Column, 1, Cells(2, idx2).Value)
    End If
Next idx2

Cells(1, 10).Activate
ReDim PermutationsArr(1 To Fact(Len(mystring)))
For Count = 1 To UBound(PermutationsArr)
    PermutationsArr(Count) = mystring
Next Count

For StartP = 1 To Len(mystring)
    idxArr = 0
    Do While idxArr < UBound(PermutationsArr)
        For EndP = StartP To Len(mystring)
            For Count = 1 To Fact(Len(mystring) - StartP + 1) / (Len(mystring) - StartP + 1)
                idxArr = idxArr + 1
                MyChar = Mid(PermutationsArr(idxArr), StartP, 1)
                Mid(PermutationsArr(idxArr), StartP) = Mid(PermutationsArr(idxArr), EndP, 1)
                Mid(PermutationsArr(idxArr), EndP) = MyChar
            Next Count
        Next EndP
    Loop
Next StartP

For idxArr = 1 To UBound(PermutationsArr)
    idxLowPer = idxArr
    For Count = idxArr To UBound(PermutationsArr)
        If PermutationsArr(Count) < PermutationsArr(idxLowPer) Then
            idxLowPer = Count
        End If
    Next Count
    MyChar = PermutationsArr(idxArr)
    PermutationsArr(idxArr) = PermutationsArr(idxLowPer)
    PermutationsArr(idxLowPer) = MyChar
Next idxArr

MyChar = "": Count2 = 0: Count3 = 1
For Count = 1 To UBound(PermutationsArr)
    If PermutationsArr(Count) <> MyChar Then
        Count2 = Count2 + 1: Count3 = Count3 + 1: MyChar = PermutationsArr(Count)
        For idx4 = 1 To 7
        Cells(Count3, 10 + idx4).Value = Cells(1, CLng(Mid(PermutationsArr(Count), idx4, 1))).Value
        Next idx4
    End If
Next Count

End Sub
Public Function Fact(i As Integer) As Long
Dim k As Integer
Fact = 1
For k = i To 2 Step -1
    Fact = Fact * k
Next k
End Function
At the moment I am playing lotto games, the one you like in Italy is named Totocalcio and was my favourite many years ago so I must think a bit to remember all the tricks. I was wondering why you use couples, once when totocalcio was the most popular game and set on 13 games (that's the reason why in my country 13 is a lucky number!), the most common result was six 1, five X, and two 2.

Have a nice weekend.
Does the Totocalcio is football game. Is still continuing there. I am playing football and I find will be more effective to analyse the couple’s instead individual sign I hope I will achieve with your help out with some interesting results. Let see.

I'm grateful for your help! I love your coding it is fine and precise

You have a nice weekend too.

Kind Regards,
Kishan :)
 
Upvote 0

Forum statistics

Threads
1,215,479
Messages
6,125,041
Members
449,206
Latest member
Healthydogs

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