Results 1 to 13 of 13

Thread: methodology for making change

  1. #1

    Thread Starter
    Fanatic Member JPicasso's Avatar
    Join Date
    Aug 2001
    Location
    Kalamazoo, MI
    Posts
    843

    methodology for making change

    I need to program a methodology very similar to making change for a dollar.

    Say I have a value to obtain, 100 cents.

    I have 1 cent, 5 cent, 10 cent, 25 cent pieces to do it with.

    What would be the best methodology to figure out how many differnt ways I could make change?

    I need to program some thing like this and how it goes now takes forever.
    Actually, someone else on my team needs to do this and he is stuck, so I'd like
    to swoop in and rescue the whole thing, you know?

    anyways. anyone got any suggestions?
    Merry Christmas

  2. #2
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682
    Short of doing a brute force search, I can't think of a way of doing it.
    I don't live here any more.

  3. #3
    Fanatic Member alkatran's Avatar
    Join Date
    Apr 2002
    Location
    Canada
    Posts
    860
    Well, think of it this way:

    Make a few main groups. For example, make a "four quarters" dollar, etc. How many ways can you split a quarter? That number ^ 4. Do the same thing for dimes etc.

    Now, loop through that and take out any doubles. (Does order matter?)

    If order doesn't matter it gets a hell of a lot simpler.
    Don't pay attention to this signature, it's contradictory.

  4. #4
    Fanatic Member alkatran's Avatar
    Join Date
    Apr 2002
    Location
    Canada
    Posts
    860
    VB Code:
    1. for a = 0 to 4
    2. for b = 0 to 10
    3. for c = 0 to 20
    4. for d = 0 to 100
    5. if 25*a+b*10+c*5+d = 100 then
    6.    debug.print "Q:" & a & ", D:" & b & "N:" & c & "P:" & d
    7. end if
    8. next d
    9. next c
    10. next b
    11. next a

    Total loops: 4*10*20*100 = 40*2000 = 80 000

    Time to execute: Took like half a second
    Don't pay attention to this signature, it's contradictory.

  5. #5
    Fanatic Member alkatran's Avatar
    Join Date
    Apr 2002
    Location
    Canada
    Posts
    860
    There, EVERYTHING.
    VB Code:
    1. Private Sub Form_Load()
    2. Dim Ordered As Double
    3. Dim Disordered As Long
    4. Dim A As Long
    5. Dim B As Long
    6. Dim C As Long
    7. Dim D As Long
    8.  
    9.     For A = 0 To 4
    10.     For B = 0 To 10
    11.     For C = 0 To 20
    12.     For D = 0 To 100
    13.         If 25 * A + B * 10 + C * 5 + D = 100 Then
    14.             Disordered = Disordered + 1
    15.             Ordered = Ordered + Fact(A + B + C + D) / (Fact(A) * Fact(B) * Fact(C) * Fact(D))
    16.             Debug.Print "Q:" & A & ", D:" & B & ", N:" & C & ", P:" & D
    17.         End If
    18.     Next D
    19.     Next C
    20.     Next B
    21.     Next A
    22.     MsgBox "There are " & Disordered & " combinations and " & Ordered & " permutations.", vbOKOnly, "Results"
    23.     Unload Me
    24. End Sub
    25.  
    26. Private Function Fact(ByVal X As Long)
    27. Dim A As Long
    28.    
    29.     Fact = 1
    30.     For A = 2 To X
    31.         Fact = A * Fact
    32.     Next A
    33. End Function


    242 combinations, 8 577 828 731 901 permutations
    Don't pay attention to this signature, it's contradictory.

  6. #6
    Fanatic Member sql_lall's Avatar
    Join Date
    Jul 2002
    Location
    Up Above (i.e. AUS)
    Posts
    571

    Talking mmmm

    hehe...where's the sophisticated algorithmic development?

    I would recommend having a function that returns the number of ways to make X cents with the highest coin being the Yth in the series (25, 10, 5, 1) .

    You then call it recursively, caching results.

    For instance (in C++ pseudo-code for my convenience
    Code:
    int ways(int X, int Y)
    {
        if(Y == 3)
            return 1; // 1 way of making X cents with 1 cent pieces
    
        int ret = 0;
        for(int a = 0; a <= X; a+=coin[Y]) // each number of current coin
            ret += ways(X - a, Y + 1);
        
        return ret;
    }
    As I said before, you would do some caching (i.e. saving results to array) to make it a bit quicker. The array would be two-dimensional, indexed by the X and Y parameters.
    }
    sql_lall

  7. #7
    Fanatic Member alkatran's Avatar
    Join Date
    Apr 2002
    Location
    Canada
    Posts
    860
    My algorythm is fast enough to do the job in under a second (on my 1.2 .. or was it 1.8.. ghz system)... why would I want to make an overly complicated algo?
    Don't pay attention to this signature, it's contradictory.

  8. #8
    Fanatic Member alkatran's Avatar
    Join Date
    Apr 2002
    Location
    Canada
    Posts
    860
    Tell you what though, I'll optimize:

    VB Code:
    1. Private Sub Form_Load()
    2. Dim Ordered As Double
    3. Dim Disordered As Long
    4. Dim A As Long
    5. Dim B As Long
    6. Dim C As Long
    7. Dim D As Long
    8.  
    9.     For A = 0 To 4
    10.     For B = int(A*2.5) To 10
    11.     For C = A*5+B*2 To 20
    12.         If 25 * A + B * 10 + C * 5 < 100 Then
    13.             D = 100-25a-10b-5c
    14.             Disordered = Disordered + 1
    15.             Ordered = Ordered + Fact(A + B + C + D) / (Fact(A) * Fact(B) * Fact(C) * Fact(D))
    16.             Debug.Print "Q:" & A & ", D:" & B & ", N:" & C & ", P:" & D
    17.         End If
    18.     Next C
    19.     Next B
    20.     Next A
    21.     MsgBox "There are " & Disordered & " combinations and " & Ordered & " permutations.", vbOKOnly, "Results"
    22.     Unload Me
    23. End Sub
    24.  
    25. Private Function Fact(ByVal X As Long)
    26. Dim A As Long
    27.         Fact = 1
    28.     For A = 2 To X
    29.         Fact = A * Fact
    30.     Next A
    31. End Function

    That should shrink the run time a very large amount since it reduces the length to at LEAST 1% of what it was.
    Last edited by alkatran; Jun 30th, 2004 at 07:03 AM.
    Don't pay attention to this signature, it's contradictory.

  9. #9
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682
    OK does anyone have the algorithm to generate £50 notes?
    I don't live here any more.

  10. #10
    Fanatic Member alkatran's Avatar
    Join Date
    Apr 2002
    Location
    Canada
    Posts
    860
    Now I'm embarrased. That code was awful. But I couldn't check it where I was. So here's the fixed one.
    VB Code:
    1. Private Sub Form_Load()
    2. Dim Ordered As Double
    3. Dim Disordered As Long
    4. Dim A As Long
    5. Dim B As Long
    6. Dim C As Long
    7. Dim D As Long
    8.  
    9.     For A = 0 To 4
    10.     For B = 0 To 10 - Int(A * 2.5)
    11.     For C = 0 To 20 - 5 * A - 2 * B
    12.         If 25 * A + B * 10 + C * 5 <= 100 Then
    13.             D = 100 - 25 * A - B * 10 - C * 5
    14.             Disordered = Disordered + 1
    15.             Ordered = Ordered + Fact(A + B + C + D) / (Fact(A) * Fact(B) * Fact(C) * Fact(D))
    16.             Debug.Print "Q:" & A & ", D:" & B & ", N:" & C & ", P:" & D
    17.         End If
    18.     Next C
    19.     Next B
    20.     Next A
    21.     MsgBox "There are " & Disordered & " combinations and " & Ordered & " permutations.", vbOKOnly, "Results"
    22.     Unload Me
    23. End Sub
    24.  
    25. Private Function Fact(ByVal X As Long)
    26. Dim A As Long
    27.    
    28.     Fact = 1
    29.     For A = 2 To X
    30.         Fact = A * Fact
    31.     Next A
    32. End Function
    Don't pay attention to this signature, it's contradictory.

  11. #11
    Fanatic Member alkatran's Avatar
    Join Date
    Apr 2002
    Location
    Canada
    Posts
    860
    and by the way that algorythm takes 5 milliseconds to find the answer. I think that's acceptable.
    Don't pay attention to this signature, it's contradictory.

  12. #12
    Fanatic Member alkatran's Avatar
    Join Date
    Apr 2002
    Location
    Canada
    Posts
    860
    Haha, I changed the program so that it will generate the number for 50$ (including pennies, nickels.. all the way to twenties)

    It's been running for awhile now.

    It just did its first "10" iteration.


    I have two longs set up.. when one gets over a billion it adds 1 to the second.. I was worried about overflow.
    Don't pay attention to this signature, it's contradictory.

  13. #13
    Fanatic Member alkatran's Avatar
    Join Date
    Apr 2002
    Location
    Canada
    Posts
    860
    Today I learned there's almost 4 billion ways to make change for a 50$ bill.
    (3 9## ### ###)

    and it only took 37.5 minutes.
    Don't pay attention to this signature, it's contradictory.

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