Lottery 1st & 2nd Digits Calculations

ststern45

Well-known Member
Joined
Sep 17, 2005
Messages
961
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
Need help with creating 2 lists:
Example: 5/39 Lottery Game
List #1 - 1st digits/left digits
Pad all numbers with a zero in numbers less than 10 (01-02-03-04-05)
Create a list of all 1st or left digit sets
01-02-03-04-05 = 0,0,0,0,0
35-36-37-38-39 = 3,3,3,3,3
Count how many sets from the entire list of 575757 have the 1st digits 0-0-0-0-0 through 3-3-3-3-3
Remove any duplicate 1st digit sets from the list

List #2 - 2nd digits/right digits
Pad all numbers with a zero in numbers less than 10 (01-02-03-04-05)
Create a list of all 2nd or right d1,2,3,4,5
35-36-37-38-39 = 5,6,7,8,9
Count how many sets from the entire list of 575757 have the 2nd digits 1,2,3,4,5 through 5,6,7,8,9
Remove any duplicate 2nd digit sets from the list.

Thank you in advance.
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
List # 1:

ABCDEF
1Total575,757
2
3CombinsNumber
400000126
5000011,260
6000021,260
7000031,260
8000113,780
9000128,400
10000138,400
11000223,780
12000238,400
13000333,780
14001114,320
150011216,200
160011316,200
170012216,200
180012336,000
190013316,200
20002224,320
210022316,200
220023316,200
23003334,320
24011111,890
250111210,800
260111310,800
270112218,225
280112340,500
290113318,225
300122210,800
310122340,500
320123340,500
330133310,800
34022221,890
350222310,800
360223318,225
370233310,800
38033331,890
3911111252
40111122,100
41111132,100
42111225,400
431112312,000
44111335,400
45112225,400
461122320,250
471123320,250
48113335,400
49122222,100
501222312,000
511223320,250
521233312,000
53133332,100
5422222252
55222232,100
56222335,400
57223335,400
58233332,100
5933333252
List1
Cell Formulas
RangeFormula
F1F1=SUM(F4:F60)
F4:F59F4=PRODUCT(COMBIN({9,10,10,10},COUNTIF(A4:E4,{0,1,2,3})))
Press CTRL+SHIFT+ENTER to enter array formulas.

List #2 is derived similarly, but attached here as it's too big to post: Combins Lists.xlsx
 
  • Like
Reactions: shg
Upvote 0
Excellent work Stephen.
Thank you for the Excel Combins Lists.xlsx
May I ask how you created the strings starting in A4:E4 down to A59 through E59 in List1?
Thank you for the work.
Very impressive!!
 
Upvote 0
My guess is that you created 2 lists of all possible strings for 1st and 2nd digts (left & right) for the 5/39 game then removed any of the strings where the total sets = 0?

For example,
5/39 game
List1
1st Digit Strings
0-0-0-0-0 (01-02-03-04-05)
3-3-3-3-3 (35-36-37-38-39)
Position 1 - Low # = 0, High # = 3
Position 2 - Low # = 0, High # = 3
Position 3 - Low # = 0, High # = 3
Position 4 - Low # = 0, High # = 3
Position 5 - Low # = 0, High # = 3

List2
2nd Digit Strings
01-02-03-04-05
35-36-37-38-39

Position 1 - Low # = 0, High # = 9
Position 2 - Low # = 0, High # = 9
Position 3 - Low # = 0, High # = 9
Position 4 - Low # = 0, High # = 9
Position 5 - Low # = 0, High # = 9
 
Upvote 0
So list1 (1st digit sets) would consist of a total of 1024 possible strings
list 2 (2nd digits sets) would consist of a total of 100,000 possible strings but would need to be reduced since the highest possible number for positon 4 = 38, and position 5 = 39
I could be wrong but wanted to give it a try.
 
Upvote 0
Question:
Could you explain the part of the formula below and the calculations
COMBIN({9,10,10,10}
Is the 9,10,10,10 based off of the 39 total numbers in the game?
Suppose the game was a 5/30 with 30 total numbers or 5/43 with 43 total numbers?
Thanks in advance
 
Upvote 0
>> Is the 9,10,10,10 based off of the 39 total numbers in the game?

1 to 9 is 9 numbers; 10 to 19, 20 to 29, and 30 to 39 are 10 numbers each.

>> Suppose the game was a 5/30 with 30 total numbers ...

1 to 9, 10 to 19, 20 to 29, 30: COMBIN(9, 10, 10, 1)

>> or 5/43 with 43 total numbers?

1 to 9, 10 to 19, 20 to 29, 30 to 39, 40 to 43: COMBIN(9, 10, 10, 10, 4)
 
Upvote 0
Solution
Thank you for the reply. The calculations you provided work great.
I understand how this works.
Very much appreciated.
 
Upvote 0
My next project would be create the list of all possible strings for the 1st Digits
This is the vba code below I used to test the calculations.
Is there a faster way yo accomplish this?
Below is the code for a 5/39 game
The issue arrises when creating a list from a larger game matrix such as a 5/69 oo 5/69 game
Once the calculations reach the last cell which is 1,048,576 I receive an error.
Is there code that can be added that will contine to the next column?

Big thanks to Jack at Lottoforums for the code.

1st Digit Code

Sub first_digits539()
Dim a, b, c, d, e As Integer
Dim a1, b1, c1, d1, e1 As Integer
Dim count As Long

Columns("A:A").Select

Selection.NumberFormat = "@"
Range("a1").Select

count = 0

For a = 1 To 35
a1 = Int(a / 10)
For b = a + 1 To 36
b1 = Int(b / 10)
For c = b + 1 To 37
c1 = Int(c / 10)
For d = c + 1 To 38
d1 = Int(d / 10)
For e = d + 1 To 39
e1 = Int(e / 10)

count = count + 1

ActiveCell.Offset(count, 0) = CStr(a1 & b1 & c1 & d1 & e1)

Next
Next
Next
Next
Next

Columns("A:A").Select
ActiveSheet.Range("$A$1:$A$575757").RemoveDuplicates Columns:=1, Header:= _
xlNo
Range("A1").Select
End Sub



Second Digit Code

Sub second_digits539()
Dim a, b, c, d, e As Integer
Dim a1, b1, c1, d1, e1 As Integer
Dim count As Long

Columns("A:A").Select

Selection.NumberFormat = "@"
Range("a1").Select

count = 0

For a = 1 To 35
a1 = a Mod 10
For b = a + 1 To 36
b1 = b Mod 10
For c = b + 1 To 37
c1 = c Mod 10
For d = c + 1 To 38
d1 = d Mod 10
For e = d + 1 To 39
e1 = e Mod 10

count = count + 1

ActiveCell.Offset(count, 0) = CStr(a1 & b1 & c1 & d1 & e1)

Next
Next
Next
Next
Next

Columns("A:A").Select
ActiveSheet.Range("$A$1:$A$575757").RemoveDuplicates Columns:=1, Header:= _
xlNo
Range("A1").Select
End Sub
 
Upvote 0
May I ask how you created the strings starting in A4:E4 down to A59 through E59 in List1?
VBA, based on pgc01's code here: Combination, Repeats (AAB,BBC) allowed, Repeats (AAC,ACA,CAA) only considered as One
which can be used to generate combinations/permutations with/without repeats.

For List #2, this generates a few impossible combinations, e.g.
0|0|0|0|1 (there are only three 0's available), and
2|2|2|2|2 (only four 2's available).

For these combinations, the COMBIN() formula will produce an error, so I just filtered the errors out of List #2.

Have a play with the workbook attached: Combination Lists.xlsm

ABCDEFGHIJK
1Lottery(n,p)695
2MethodFirst
30123456789
4No9101010101010000
5
6CountDigits7Total:11,238,513
7Check:11,238,513
8
9Combins
1000000126
11000011,260
12000021,260
13000031,260
14000041,260
15000051,260
16000061,260
17000113,780
18000128,400
19000138,400
20000148,400
21000158,400
22000168,400
Combins
Cell Formulas
RangeFormula
B4B4=IF(Method="First",MIN(9,n),INT(n/10))
C4:K4C4=IF(Method="First",MIN(10,n-SUM($B4:B4)),INT(n/10)+(MOD(n,10)>=C3))
B6B6=MATCH(2,1/(NoDigits>0))
E6E6=AGGREGATE(9,6,INDEX(MyResults,,p+1))
E7E7=COMBIN(n,p)
F10:F22F10=PRODUCT(COMBIN(NoDigits,COUNTIF(A10:E10,Digits)))
Press CTRL+SHIFT+ENTER to enter array formulas.
Named Ranges
NameRefers ToCells
Digits=Combins!$B$3:$K$3F10:F22
Method=Combins!$B$2B4:K4
MyResults=Combins!$A$10:$F$471E6, F10
n=Combins!$B$1E7, B4:K4
NoDigits=Combins!$B$4:$K$4B6, F10:F22, C4:K4
p=Combins!$C$1E6:E7
ResultsHere=Combins!$A$10E6, F10
Cells with Data Validation
CellAllowCriteria
B2ListFirst,Last

VBA Code:
Option Explicit
'Sheet Module
Private Sub Worksheet_Change(ByVal Target As Range)

    If Not Application.Intersect(Target, Union(Range("n"), Range("p"), Range("Method"))) Is Nothing Then _
        Call CombPerm

End Sub

'Code Module

' PGC Set 2007
' Calculates and writes the Combinations / Permutations with/without repetition
' vElements - Array with the set elements (1 to n)
' p - number of elements in 1 combination/permutation
' bComb - True: Combinations, False: Permutations
' bRepet - True: with repetition, False: without repetition
' vResult - Array to hold 1 permutation/combination (1 to p)
' lRow - row number. the next combination/permutation is written in lRow+1
' vResultAll - Array to hold all the permutations/combinations (1 to Total, 1 to p)
' iElement - order of the element to process in case of combination
' iIndex - position of the next element in the combination/permutation
' Sub CombPerm() deals with the input / output
' Sub CombPermNP() generates the combinations / permutations
Sub CombPerm()

    Dim p As Long, i As Long, N As Long
    Dim vElements As Variant, vResult As Variant, vResultAll As Variant, lTotal As Long
    Dim lRow As Long, bComb As Boolean, bRepet As Boolean
    
    p = Range("p").Value
    N = Range("CountDigits").Value
    bComb = True
    bRepet = True
              
    ReDim vElements(1 To N)
    For i = 1 To N
        vElements(i) = i - 1
    Next i
    
    On Error Resume Next
    Range("MyResults").ClearContents
    On Error GoTo 0
    With Application.WorksheetFunction
        If bComb = True Then
                lTotal = .Combin(N + IIf(bRepet, p - 1, 0), p)
        Else
            If bRepet = False Then lTotal = .Permut(N, p) Else lTotal = N ^ p
        End If
    End With
    ReDim vResult(1 To p)
    ReDim vResultAll(1 To lTotal, 1 To p)
    
    ' Calculate the Combinations / Permutations
    Call CombPermNP(vElements, p, bComb, bRepet, vResult, lRow, vResultAll, 1, 1)
    With Range("ResultsHere").Resize(lTotal, p)
        .Value = vResultAll
        With .Columns(p + 1)
            .FormulaR1C1 = "=PRODUCT(COMBIN(NoDigits,COUNTIF(RC[-" & p & "]:RC[-1],Digits)))"
            .FormulaArray = .FormulaR1C1
        End With
        .Resize(, p + 1).Name = "MyResults"
    End With
    
End Sub
Sub CombPermNP(ByVal vElements As Variant, ByVal p As Integer, ByVal bComb As Boolean, ByVal bRepet As Boolean, _
                             ByVal vResult As Variant, ByRef lRow As Long, ByRef vResultAll As Variant, ByVal iElement As Integer, ByVal iIndex As Integer)
    Dim i As Long, j As Long, bSkip As Boolean
    
    For i = IIf(bComb, iElement, 1) To UBound(vElements)
        bSkip = False
        ' in case of permutation without repetition makes sure the element is not yet used
        If (Not bComb) And Not bRepet Then
            For j = 1 To p
                If vElements(i) = vResult(j) And Not IsEmpty(vResult(j)) Then
                    bSkip = True
                    Exit For
                End If
            Next
        End If
    
        If Not bSkip Then
            vResult(iIndex) = vElements(i)
            If iIndex = p Then
                lRow = lRow + 1
                For j = 1 To p
                    vResultAll(lRow, j) = vResult(j)
                Next j
            Else
                Call CombPermNP(vElements, p, bComb, bRepet, vResult, lRow, vResultAll, i + IIf(bComb And bRepet, 0, 1), iIndex + 1)
            End If
        End If
    Next i
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,069
Messages
6,122,951
Members
449,095
Latest member
nmaske

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