Results 1 to 15 of 15

Thread: Programming Challange:

  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

  2. #2
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431
    With being able to use an element more than once, I used a random approach:

    VB Code:
    1. Dim i As Long
    2.     Dim RndVal1 As Long
    3.     Dim RndVal2 As Long
    4.     Dim RndVal3 As Long
    5.     Dim RndVal4 As Long
    6.     Dim RndVal5 As Long
    7.     Dim RndVal6 As Long
    8.  
    9.     Randomize
    10.     For i = 1 To 266981
    11.         RndVal1 = Int(Rnd * 60) + 1
    12.         RndVal2 = Int(Rnd * 60) + 1
    13.         RndVal3 = Int(Rnd * 60) + 1
    14.         RndVal4 = Int(Rnd * 60) + 1
    15.         RndVal5 = Int(Rnd * 60) + 1
    16.         RndVal6 = Int(Rnd * 60) + 1
    17.    
    18.         If RndVal1 + RndVal2 + RndVal3 + RndVal4 + RndVal5 + RndVal6 = 150 Then MyCounter = MyCounter + 1
    19.     Next i
    20.     MyCounter = MyCounter * (61 ^ 6) / (i - 1)

    I didn't make it generic, but it could be easily done. Oh, also, this gets more and more accurate the higher the upper bound of i is (at the sacrifice of speed).
    The time you enjoy wasting is not wasted time.
    Bertrand Russell

    <- Remember to rate posts you find helpful.

  3. #3
    Fanatic Member jian2587's Avatar
    Join Date
    Aug 2000
    Location
    I bet u need a fusion powered shuttle to reach my place...
    Posts
    963
    instead of using a matrix of numbers and going on sequentially to
    grab numbers that'll add up to 150, how about given a number,
    that is 150, and you try to search for number combinations that'll
    sum up to that number? That is, through intelligent algorithms and
    not sequentially?

    And yes, that's precisely what u need, an algorithm, I'll try and
    see if I can work out one.
    ASM,C,C++,BASIC,VB,JAVA,VBS,HTML,ASP,PHP,mySQL,VB.NET,MATLAB
    Programming is fun, but only if you're not on a tight deadline
    So I consider all those working engineers sad people

    VB FTP class
    3 page PHP crash course
    Crash Course on DX9 Managed with VB.NET covering basics till terrain creation

  4. #4

    Thread Starter
    pathfinder NotLKH's Avatar
    Join Date
    Apr 2001
    Posts
    2,397
    That sounds like a good strategy, jian2587.
    BTW, I've gotten mine down to about 4-5 seconds, but I've still got 1 tweak to do, and then I'm going to investigate rearranging a couple of steps that aren't needed until an individual combination has been made.


    And, jemidiah?

    It seems you've recently discovered the wonders of random numbers. They can certainly be usefull in many situations, but perhaps you should understand that, when I say "combination", I mean it in its purest mathematical sense, without repititions, and the order is not a consideration in the final output. So, basically, I'm looking for a conditional "All Combinations" Generator.


    -Lou

  5. #5
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    SQL Server could do it I'd say
    I'm interested in giving it a go, but I don't understand your question fully. Can you give a stepthrough example for a few numbers ?
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  6. #6

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

    Lets say I wanted to Generate every combination of 15 items taken 3 at a time, that add to 9.

    And, Lets say the actual Values of the 15 items range from
    1 to 15.

    Their are 3 combinations that meet this criteria:

    1, 2, 6
    1, 3, 5
    2, 3, 4

    So, the goal of the progie is:

    1) Return the count of all combinations of 15 items taken 3 at a time that add to 9, Generically, N items taken P at a time that add to S

    2) Have each such Combination Generated physically generated. This is NOT meant to be a function on a calculator, so the count is just a return of every valid combination as they are generated. Ultimately, once a combination has been generated, it could be used as the input into some as yet undefined process.

    3) All Such Combinations are not persistable. That is, It is not required to build an array of all such combinations. They only have to be in existance individually. For example, when all is said and done, the 3 combinations {Above} do not simultaneously exist. They Exist only Once, and destroyed to allow the next one to be generated. starts humming ending theme music to SOAP

    4) The Progie must be capable to process 61 items {Number Vals of 1 thru 61} taken 6 at a time that add to 150.

    There is a self imposed 5th goal, have the source numbers currently unused at the time the used ones are in a target aray be packed into the lower positions of the source array, but that'll be mentioned later.

    Hows That?

    BTW, I've localized my attempt into its own project, and it hums away at 4 to 5 seconds for the 6C61 = 150, meeting my 5th goal, and 2 seconds but not meeting my 5th goal

    Just doing some cleanup, and perhaps I'll be satisfied with it soon.

    BTW, if you Do have some way of confirming 369569 {See my Maths Question} , via a formula, perhaps you'd do so?


    -Thanks

    -Lou

  7. #7

  8. #8
    Frenzied Member dis1411's Avatar
    Join Date
    Mar 2001
    Posts
    1,048
    thats pretty cool that it only takes 4-5 sec on your comp
    look at this post and see if it makes it even faster

    the hard code takes about 1, i also get 369,569

    VB Code:
    1. Private Sub Command1_Click()
    2.  
    3. Dim a As Integer
    4. Dim b As Integer
    5. Dim c As Integer
    6. Dim d As Integer
    7. Dim e As Integer
    8. Dim f As Integer
    9. Dim l As Long
    10.  
    11. For a = 1 To 56
    12. For b = a + 1 To 57
    13. For c = b + 1 To 58
    14. For d = c + 1 To 59
    15. For e = d + 1 To 60
    16. For f = e + 1 To 61
    17.     If a + b + c + d + e + f = 150 Then
    18.        
    19.         l = l + 1
    20.     End If
    21. Next
    22. Next
    23. Next
    24. Next
    25. Next
    26. Next
    27.  
    28. MsgBox Format(l, "#,#")
    29.  
    30. End Sub

    i think that the fastest way that will allow u to enter how deep, the sum, and how many to try on the fly is to make a recursive function that in essence sets up nested for loops like the one above
    Last edited by dis1411; Oct 12th, 2003 at 01:35 PM.

  9. #9
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    Does it have to be vb?

  10. #10

    Thread Starter
    pathfinder NotLKH's Avatar
    Join Date
    Apr 2001
    Posts
    2,397
    da_silvy: Nope! Could be anything you want.

    dis1411: Excellent! I doubt it could be any faster!

    Now then. Your code specifically targets my test input, ie.. The numbers 1 thru 61, taken 6 at a time, adds to 150.

    Think you could make it a little more generic? Make it so that your source is N elements, starting at K, ending with K + N - 1? {Instead of 61 elements, starting at 1, ending with 1 + 61 - 1?}

    Also, allow it to take P at a time, instead of the fixed 6?

    And, have the sum condition be a variable S, instead of a fixed 150? {N, K, P, and S are just suggested nomenclature, just meant to illustrate general variability}

    And, One final thing? {Condition Number 5}

    At the point where your code represents:
    VB Code:
    1. If a + b + c + d + e + f = 150 Then
    2.        
    3.         l = l + 1
    4.     End If

    Can you have a structure where the numbers that aren't a,b,c,d,e or F are in the first N - P elements of the structure, in ascending order?

    With My code, although I'm sure its hardly decypherable, the In_Arr has all the used elements removed from their initial positions, and all the unused elements have shifted to the left to take up the positions originally held by the used elements.

    I'm leading up toward haveing it return several groups of elements, each haveing potentially different sums, where the next group {x} is the combination of the elements of the original source, minus those used by the previously extracted groups, taken Px at a time, adding to Sx.

    Have Fun!

  11. #11

  12. #12

    Thread Starter
    pathfinder NotLKH's Avatar
    Join Date
    Apr 2001
    Posts
    2,397
    Originally posted by NotLKH
    Thanks! I'll try this out tomorrow!


    -lou
    Yep!

    I checked off everything in the advanced area
    {Project->Project1 Properties->Compile->Advanced Optimizations}
    And my time went from 4.2ish to 3.8ish seconds!

    Very Nice!

    Thanks again, dis1411!

  13. #13
    Fanatic Member jian2587's Avatar
    Join Date
    Aug 2000
    Location
    I bet u need a fusion powered shuttle to reach my place...
    Posts
    963
    post up ur hardware and sys config so atleast i can know ur 3.8
    secs is produced by wut sort of machine speed and i can compare
    it with mine.

    mine is a slow P4 2.0A Ghz, bus speed 533Mhz, 256MB 266Mhz
    DDR SDRAM and WinXP Pro.

    i actually have a new idea in my mind earlier on how to fine tune
    this thin', but i haven't written any actual codes, still tweakin' it in
    my head.
    ASM,C,C++,BASIC,VB,JAVA,VBS,HTML,ASP,PHP,mySQL,VB.NET,MATLAB
    Programming is fun, but only if you're not on a tight deadline
    So I consider all those working engineers sad people

    VB FTP class
    3 page PHP crash course
    Crash Course on DX9 Managed with VB.NET covering basics till terrain creation

  14. #14
    Fanatic Member jian2587's Avatar
    Join Date
    Aug 2000
    Location
    I bet u need a fusion powered shuttle to reach my place...
    Posts
    963
    on my P4 2.0A Ghz system,
    ur prog gives me results as stated below:
    in ide:10.3 seconds
    compiled:1.578 seconds
    compiled with all Advance Optimizations checked: 1.371 seconds

    Pretty cool, huh?
    ASM,C,C++,BASIC,VB,JAVA,VBS,HTML,ASP,PHP,mySQL,VB.NET,MATLAB
    Programming is fun, but only if you're not on a tight deadline
    So I consider all those working engineers sad people

    VB FTP class
    3 page PHP crash course
    Crash Course on DX9 Managed with VB.NET covering basics till terrain creation

  15. #15

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