Results 1 to 15 of 15

Thread: Programming Challange:

Threaded View

  1. #1

    Thread Starter
    pathfinder NotLKH's Avatar
    Join Date
    Apr 2001
    Posts
    2,397

    Programming Challange:

    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:
    1. Private Sub cmdAddsTo2_Click()
    2.     Dim MyStartTick As Long
    3.     Dim MyEndTick As Long
    4.     Dim MyI As Integer
    5.     Dim MyCounter As Long
    6. 'Lets do 61 elements
    7.     Dim MySourceArr() As Integer
    8.     ReDim MySourceArr(60)
    9.     For MyI = 0 To 60
    10.         MySourceArr(MyI) = MyI + 1
    11.     Next MyI
    12. 'taken 6 at a time
    13.     Dim My4_arr() As Integer
    14.     ReDim My4_arr(5)
    15. 'that adds to 150
    16.     Dim ItAddsTo As Long
    17.     ItAddsTo = 150
    18.     MyCounter = 0
    19.     MyStartTick = GetTickCount
    20.     Call YOUR_SUB_OR_FUNCTION_HERE(MySourceArr,  My4_arr, MyCounter, _
    21.         ItAddsTo, ...[i]anything else you need[/i])
    22.     MyEndTick = GetTickCount
    23.     MsgBox MyCounter & " Took " & (MyEndTick - MyStartTick) / 1000
    24. End Sub

    for example, an outline view of my sub looks like the folowing:
    VB Code:
    1. Private Sub NUM_SUM_COMBS_2(ByRef IN_Arr() As Integer, _
    2.         ByRef HowDeep As Integer, ByVal MyLev As Integer, _
    3.         ByRef MyOut() As Integer, ByRef MyCount As Long, _
    4.         ByRef MyLastI As Integer, ByVal mAddsTo As Integer, _
    5.         ByRef mBackOut As Boolean)
    6.  
    7. 'mylev starts at 0, and so does mycount
    8.  
    9. If MyLev > HowDeep Then
    10.     If mAddsTo = 0 Then
    11.         MyCount = MyCount + 1     'At this point, MyOut() contains a combination
    12.     End If                            'of elements that add to the desired sum
    13. Else
    14.         'does something
    15.     Dim MyI As Integer
    16.     For MyI = (MyLastI) To (UBound(IN_Arr) - MyLev)
    17.         'does something else
    18.             Call NUM_SUM_COMBS_2(IN_Arr, HowDeep, MyLev + 1, MyOut, MyCount, _
    19.                    MyI, mAddsTo - MyOut(MyLev), mBackOut)
    20.             'does some more stuff
    21.     Next MyI
    22. End If
    23. 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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width