Alright! I'd like to see all of you do your best in the following:
Given a Matrix of the numbers 1 thru 61
and, given a target array of 6 elements:
Build an app that generates every combination of 61 elements from 1 thru 61, taken 6 at a time that add to 150, and counts how many total it generates. When its done, it msgbox'es the count.
It must be generic enough so that we can vary the target sum, the number of elements in the source array, and the number of elements in the target array.
Any Questions?
VB Code:
Private Sub cmdAddsTo2_Click() Dim MyStartTick As Long Dim MyEndTick As Long Dim MyI As Integer Dim MyCounter As Long 'Lets do 61 elements Dim MySourceArr() As Integer ReDim MySourceArr(60) For MyI = 0 To 60 MySourceArr(MyI) = MyI + 1 Next MyI 'taken 6 at a time Dim My4_arr() As Integer ReDim My4_arr(5) 'that adds to 150 Dim ItAddsTo As Long ItAddsTo = 150 MyCounter = 0 MyStartTick = GetTickCount Call YOUR_SUB_OR_FUNCTION_HERE(MySourceArr, My4_arr, MyCounter, _ ItAddsTo, ...[i]anything else you need[/i]) MyEndTick = GetTickCount MsgBox MyCounter & " Took " & (MyEndTick - MyStartTick) / 1000 End Sub
for example, an outline view of my sub looks like the folowing:
VB Code:
Private Sub NUM_SUM_COMBS_2(ByRef IN_Arr() As Integer, _ ByRef HowDeep As Integer, ByVal MyLev As Integer, _ ByRef MyOut() As Integer, ByRef MyCount As Long, _ ByRef MyLastI As Integer, ByVal mAddsTo As Integer, _ ByRef mBackOut As Boolean) 'mylev starts at 0, and so does mycount If MyLev > HowDeep Then If mAddsTo = 0 Then MyCount = MyCount + 1 'At this point, MyOut() contains a combination End If 'of elements that add to the desired sum Else 'does something Dim MyI As Integer For MyI = (MyLastI) To (UBound(IN_Arr) - MyLev) 'does something else Call NUM_SUM_COMBS_2(IN_Arr, HowDeep, MyLev + 1, MyOut, MyCount, _ MyI, mAddsTo - MyOut(MyLev), mBackOut) 'does some more stuff Next MyI End If End Sub
Lets see how creative YOU are!
so far, compiled, mine produces 369569 combinations in about 4-5 seconds on my machine. but I'm still tweaking!![]()
![]()
-Lou




Reply With Quote