View Single Post
  #7   Report Post  
Posted to microsoft.public.excel.worksheet.functions
Dana DeLouis Dana DeLouis is offline
external usenet poster
 
Posts: 947
Default Lottery combinations

How do I calculate the number of combinations in a 49 number 6
ball lottery where every combinations adds up to say: 138


156004 combinations that add up to 138.
it took several minutes to run


Hi. If interested, there are non Brute-Force methods to calculate the total
combinations without actually generating each subset.
For example, it takes about 0.4 seconds to generate the totals of each
combination of 6.
We can quickly verify your total:

? SubsetSums(49,6,138)
156,004

Which checks with your count.

Just for fun, the largest count of subset (6) sums is 150.
n=49
s=6
?(1 + n)*s /2
150

Because it's already calculated, counting the number of subsets (6) that
total 150 takes 0 seconds.
?SubsetSums(49,6,150)
165,772

Just a programming idea.
This part:

For j = 1 To numbers
If j <= i Then GoTo 100


Is usually written something like this:

For i = 1 to 44
For j= i+1 to 45
For k = j+1 to 46
etc...


Again, an interesting subject. :)
--
Dana DeLouis
Windows XP & Excel 2007


"Mike H" wrote in message
...
Hi,

An interesting Sunday afternoon exercise. Firstly, you will need to be
quite
wealthy to cover all of the 156004 combinations that add up to 138. To
generate them use an empty worksheet and put the numbers 1 - 49 in column
A
starting in A1. Then right click the sheet tab, view code and paste this
in
and run it and then make a cup of tea because it took several minutes to
run
on my PC.

P.S. It would be unwise to press the print button unless you have lots of
paper!!

Sub thelottery()
Count = 1
col = 2
lastrow = Range("A65536").End(xlUp).Row
Set myRange = Range("A1:A" & lastrow)
For Each c In myRange
numbers = numbers + 1
Next
Dim n(49)
For p = 1 To numbers
n(p) = Cells(p, 1).Value
Next
For i = 1 To numbers
For j = 1 To numbers
If j <= i Then GoTo 100
For k = 1 To numbers
If k <= j Then GoTo 200
For l = 1 To numbers
If l <= k Then GoTo 300
For m = 1 To numbers
If m <= l Then GoTo 400
For o = 1 To numbers
If o <= m Then GoTo 500
For x = 1 To numbers
If i = x Then firstno =
n(x)
Next
For x = 1 To numbers
If j = x Then secondno =
n(x)
Next
For x = 1 To numbers
If k = x Then thirdno =
n(x)
Next
For x = 1 To numbers
If l = x Then fourthno =
n(x)
Next
For x = 1 To numbers
If m = x Then fifthno =
n(x)
Next
For x = 1 To numbers
If o = x Then sixthno =
n(x)
Next
If firstno + secondno + thirdno + fourthno + fifthno +
sixthno = 138 Then
Cells(Count, col).Value = firstno & "," & secondno
&
"," & thirdno & "," & fourthno & "," & fifthno & "," & sixthno
If Count = 65536 Then
Count = 1
col = col + 1
Else
Count = Count + 1
End If
End If
500 Next
400 Next
300 Next
200 Next
100 Next

Next
Cells(1, 8).Value = Count - 1
End Sub


Mike

"Kobus" wrote:

Question1: How do I calculate the number of combinations in a 49 number 6
ball lottery where every combinations adds up to say: 138. Sum total
279,
for instance, can only happen once.
Question2: How do I generate these results?