|
-
May 30th, 2009, 09:38 AM
#1
Thread Starter
New Member
VB 6 - Combinatorics - Lottery Problem
Hi, I am new to Visual Basic6 and I am interested in writing a little application for combining 6 numbers from the range of 1....7, or even more, in all possible combinations.
I like to get an result -- Example :
1-2-3-4-5-6
2-3-4-5-6-7
1---3-4-5-6-7
1-2---4-5-6-7
1-2-3---5-6-7
1-2-3-4---6-7
1-2-3-4-5---7
The order of the numbers is not important.
On the internet, I have found two probably useful algorithm - called "Johnson-Trotter Algorithm" or the "Odometer Style Algorithm", useful because I also like to calculate larger combinations (like combinations of 6 numbers out from the range of 1....15). ---
Found at : http://www.xtremevbtalk.com/showthread.php?p=904198
Could anyone help to solve my problem, becaue I am very new to VB (I only wrote a few small/easy applications, whitout the complexity of my current project.
Here, the "Odometer" algorithm:
Code:
Option Explicit
'
' Odometer-style Combination generating functions
' by MathImagics (Dr Memory) 2004
'
' SetCombination
' NextCombination
' ThisCombination
'
Dim Cwheel() As Long
Public Sub SetCombination(ByVal N As Long, ByVal K As Long, ByVal Combn As String)
ReDim token(K) As String
token = Split(Combn, ",")
If UBound(token) <> K - 1 Then Exit Sub
Dim W As Long
ReDim Cwheel(0 To K)
For W = 1 To K
Cwheel(W) = Val(token(W - 1))
If Cwheel(W) <= Cwheel(W - 1) Then Exit Sub ' invalid combn
If Cwheel(W) > N Then Exit Sub ' ditto
Next
End Sub
Public Function ThisCombination() As String
'
' Current Combination readout
'
Dim i As Long, Comb As String
Comb = Cwheel(1)
For i = 2 To UBound(Cwheel)
Comb = Comb & ", " & Cwheel(i)
Next
ThisCombination = Comb
End Function
Public Sub NextCombination(ByVal N As Long, ByVal K As Long)
' "Combination Odometer"
'
' By MathImagics: the array Cwheel contains the current
' K items combined, in increasing order.
' Each call to this sub will adjust Cwheel
' so it contains
' the NEXT combination in lex order
'
Dim i As Long
Dim j As Long
i = K
While Cwheel(i) >= N - K + i
i = i - 1 ' find rightmost wheel that allows an increment
If i = 0 Then
' wraps around (natch!)
i = 1
Cwheel(1) = 0
End If
Wend
Cwheel(i) = Cwheel(i) + 1
For j = i + 1 To K
Cwheel(j) = Cwheel(i) + j - i
Next
End Sub
I have red this code carefully , several times, but I can´t use it in my project.
I would need your help, for putting that algorithm into a small VB6 program like: 1Command button, -> press it, the algorithm calculates the possible combinations and writes it into "Label1.caption".
That´s all. I would be very grateful for your help (and example code), because sometimes it doesn´t goes on.....
Many thanks in advance, best wishes from Gerry!!!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|