6/49 combination

Nick Koutras

Member
A faster sub to display combinations
is the subroutine below.
It is a recursive one and can handle *all*
the Lotto Games

Sub combinations(Start As Long, N As Long, K As Long, maxk As Long)
Dim i As Long

If K > maxk Then
Combo_Index = Combo_Index + 1
'/ add code below to do what you wish
'/ save combination...
'/ test combination...

Else
For i = Start To N
Row(K) = CByte(i)
'recursively generate combinations from i+1...n
Call combinations(i + 1, N, K + 1, maxk)
Next
End If

End Sub

If anybody wants more info for it's implementation just post yor request.

6/49 Combination

Hi Nick,

Regards
PAB

Nick Koutras

Member
Re: 6/49 Combination

PAB said:
Hi Nick,

Regards
PAB

Give me an example you want to implement the subroutine on
and I'll provide you with a complete

Nick

PAB

Member
6/49 Combination

Hi Nick,

What I really wanted to try was to find the Combinations with 3 Odd and 3 Even numbers, the total of the Combinations to be 149 to 151 with each of the Combinations having 3 numbers less than 25 and 3 numbers greater than 24.

If this is possible it will be great.

Regards
PAB

GillesD

Member
6/49 combinations

I thought that with your conditions (3 odd/3 even - sum between 149 and 151 - and 3 low/3 high), there would not be that many combinations.

The total is 141,890 combinations starting with 01-02-03-46-48-49 and ending with 22-23-24-26-27-29. So slightly more than 1.0% meet your requirements.

GillesD

Member
To Nick K

1 - What parameters must be passed for a 6/49 lottery when I call your Combinations subroutine . Basically what value should I assign to Start, N, K and maxk initially? What do these variables represent?

2 - I had to declare Combo_Index; I choose a Long variable for it. Is that the best choice? The value keeps increasing but what is its purpose and its ultimate value?

3 - I get: "Undefined Sub or Function" for ROW(K). It is likely I get that because I use the French version of Excel. I could possibly fix that, once I get the program to run.

4 - After the program runs, where do I get the combinations? Are they listed one combination per row or what?

Thanks

Nick Koutras

Member
Hello,

This applies to PAB & GillesD

Below is the complete code
for the requirements you specified.

-Copy the code to a module.
-Create a button to a sheet and associate the macro "BruteForce" to it
-Click the button to run the code

The answer will appear as 173,304 combos
that satisfy these filters

The code is very flexible
Alter the V parameter to 32
The answer shall be 478 such combos
Alter the M parameter to 7
for a 7 number game and so on....

Nick

========== Code ====

Option Explicit

' used when generating combinations
Private Row() As Byte
Private RowIndex As Long
Private cHor&, xCell&

Public Combo_Index&, Counter&
Public V&, M&

' BruteForce method **************************************************

Public Sub BruteForce()
Dim RetVals

Cells.Clear

V = 49
M = 6
cHor = 1

Application.ScreenUpdating = False
'// Initialize public counters
Counter = 0
Combo_Index = 0

ReDim Row(1 To M) ' one row (one combination)
RowIndex = 0 ' reset current row index

' generate And test all combinations
Call combinations(1, V, 1, M)

RetVals = MsgBox("The combinations that satisfy " & vbNewLine & "your filters are:" & vbNewLine & "" & vbNewLine & Counter, 64, "Total Combinations...")
Select Case RetVals
Case 1: 'OK
End Select

Application.ScreenUpdating = True
End Sub

Sub combinations(Start As Long, N As Long, K As Long, maxk As Long)
Dim i As Long

If K > maxk Then
Combo_Index = Combo_Index + 1
'/ add code below to do what you wish
'/ save combination...
'/ test combination...

Call TestCombo

Else
For i = Start To N
Row(K) = CByte(i)
'recursively generate combinations from i+1...n
Call combinations(i + 1, N, K + 1, maxk)
Next
End If

End Sub

Sub TestCombo()
Dim H%
Dim score%

'//1. to test if this Combo Satisifies your requirements
'//a. if sum between 149 & 151
score = Application.WorksheetFunction.Sum(Row)
If score < 149 Or score > 151 Then Exit Sub

'//b. if 3 small numbers
If Row(3) > 24 Then Exit Sub

'//c. if 3 even numbers
score = 0
For H = 1 To M
If Row(H) Mod 2 = 0 Then score = score + 1
Next
If score <> 3 Then Exit Sub

'//if we are here the combo is ok
Counter = Counter + 1

RowIndex = RowIndex + 1
If RowIndex > 65535 Then
RowIndex = 1
cHor = cHor + M + 2
End If

'//2. Display Combo_Index
Cells(RowIndex, cHor) = Combo_Index

'//3. Display Combo
For H = 1 To M
Cells(RowIndex, H + cHor) = Row(H)
Next

End Sub

Nick Koutras

Member
What I forgot was the explanation about

parameters "Start" & Combo_Index

Start can be altered to specify the first
Combo_Index is what is called as Lexicographic ID of a combination

It is used in sorting procedures

Good hunting...

GillesD

Member
To Nick K.

Your program works fine but a few things bug me.

1 - It seems my program using the good old method with 6 For ... Next loops works faster than your recursive program.

2 - More important: I get 141,980 combinations while you get 173,304 combinations.

To resolve this, I compared the first two combinations appearing in your set and not in mine. They are:

Combination #473,295 with numbers 01-04-23-24-48-49
Combination #606,446 with numbers 01-05-22-24-48-49

In both cases, it seems these combinations do not meet the condition "with each of the Combinations having 3 numbers less than 25 and 3 numbers greater than 24" set by PAB.

Nick Koutras

Member
Replace with the code below:

'//b. if 3 small numbers
If Row(3) > 24 Or Row(4) < 25 Then Exit Sub

Now the answer is as you indicated.

PAB

Member
Thanks Nick & GillesD for your help.

I have ordered John Walkenbach's VB and Excel Formulas Books, which will be in next week.
I imagine there is a lot of hard learning ahead.

One thing though Nick, you said that the code you supplied was complete. What I don't understand is the section of code below :-

If K > maxk Then
Combo_Index = Combo_Index + 1

'/ add code below to do what you wish
'/ save combination...
'/ test combination...

Call TestCombo

It implies that there is more code to be inserted.

Anyway, thankyou both for your time and effort.

All the best and good luck
PAB

Nick Koutras

Member
If anybody is interested in learning Excel
I have the following e-books (pdf) format
Just e-mail me.

Excel 2002 Formulas
Excel 2002 VBA Programming
Excel 2000 Intermediate Manual

That could save you some bucks.

PAB

Member
649 Combination

Hi GillesD,

Last Month the UK introduced a 727 Lotto.
I have tried to create ( adapting the VB code that you posted ) an Excel Spreadsheet that contains ALL 888,030 Combinations. Ideally I would like each Combination to be in 6 seperate cells so that I could perform Statistical Analysis, i.e. whether they are all Odd, Even, 2 Consecutive, 3 Consecutive etc. Unfortunately I can't seem to get it to work. Would you be so kind as to just cast your eyes over the code below and point out the error.

Option Explicit
Dim A As Integer
Dim B As Integer
Dim C As Integer
Dim D As Integer
Dim E As Integer
Dim F As Integer
Dim N As Long
Sub List_Combinations_27()
Range("A1").Select
Application.ScreenUpdating = False
N = 0
For A = 1 To 22
For B = A + 1 To 23
For C = B + 1 To 24
For D = C + 1 To 25
For E = D + 1 To 26
For F = E + 1 To 27
N = N + 1
If N = 65001 Then
N = 1
ActiveCell.Offset(-65000, 1).Select
Application.ScreenUpdating = True
Application.ScreenUpdating = False
End If
ActiveCell.Offset(1, 0).Select
Next F
Next E
Next D
Next C
Next B
Next A
Application.ScreenUpdating = True
End Sub

Thanks very much for your help.
PAB

NmbrsDude

Member
Nick Koutras said:
If anybody is interested in learning Excel
I have the following e-books (pdf) format
Just e-mail me.

Excel 2002 Formulas
Excel 2002 VBA Programming
Excel 2000 Intermediate Manual

That could save you some bucks.

Nick,

For some reason, I missed this post orignially but I am very much intersted in the 2002 Formulas and 2000 Advanced Manuals. Probably have to go through LT to get the transaction done though. Much appreciated!

Martin,
aka ND

GillesD

Member
Combinations for 7/27

The following macro will enter in an Excel sheet the 888,030 combinations for a 7/27 lottery (from1-2-3-4-5-6-7 to 21-22-23-24-25-26-27). The 7 numbers are listed individually in a cell and a blank column separates each group of 65,000 rows with data.

It might be best if you reduce the width of all columns to 4 or 5 for easier reading. It will use 65,000 rows and up to column DG. Afterward, you may want to insert a blank row at the top and place titles (like N1 to N7) for each column. This time, the macro will not update the screen after each set of 7 columns. Enjoy yourself.

Option Explicit
Dim A As Integer, B As Integer, C As Integer, D As Integer
Dim E As Integer, F As Integer, G As Integer, N As Long
Sub List_Combinations_27()
Range("A1").Select
Application.ScreenUpdating = False
N = 0
For A = 1 To 21
For B = A + 1 To 22
For C = B + 1 To 23
For D = C + 1 To 24
For E = D + 1 To 25
For F = E + 1 To 26
For G = F + 1 To 27
N = N + 1
If N = 65001 Then
N = 1
ActiveCell.Offset(-65000, 8).Select
End If
ActiveCell.Offset(0, 0).Value = A
ActiveCell.Offset(0, 1).Value = B
ActiveCell.Offset(0, 2).Value = C
ActiveCell.Offset(0, 3).Value = D
ActiveCell.Offset(0, 4).Value = E
ActiveCell.Offset(0, 5).Value = F
ActiveCell.Offset(0, 6).Value = G
ActiveCell.Offset(1, 0).Select
Next G
Next F
Next E
Next D
Next C
Next B
Next A
Application.ScreenUpdating = True
End Sub

Sheba

Member
Nick Koutras said:
If anybody is interested in learning Excel
I have the following e-books (pdf) format
Just e-mail me.

Excel 2002 Formulas
Excel 2002 VBA Programming
Excel 2000 Intermediate Manual

That could save you some bucks.
I would also like them as well...

Thanks

SHeba--

You coud ask LT to do the transaction....

Beaker

Member
email Nick directly

No need for any 'transactions'

Irvin

Member
Nick Just sent you an email

PAB

Member
727 Combinations

Hi GillesD,

Thanks very much for correcting my VB code.
I will run it at some time over the weekend, have a good one.

All the best.
PAB

PAB

Member
Hi GillesD,

How difficult is it to get the program to ignore if ALL 7 Numbers for each Combination are "ODD", "EVEN" or the Total is Greater Than or Less Than 98.