Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
You're looking for the Greedy Algorithm. It's basically the reverse from your approach: you start out with the highest denomination, then decrease that and spread the difference among the lower denominations.

Here's one link: http://www.personal.kent.edu/~rmuhamma/Algorithms/MyAlgorithms/Greedy/greedyIntro.htm

I dug up my solution to problem 31 on Project Euler.

#!/usr/bin/perl use List::Util qw/ sum /; @w = ( 1, 2, 5, 10, 20, 50, 100 ); $S = 100; $found = 0; @c = ( 0, 0, 0, 0, 0, 0, 1 ); partition( $S, @c ); print "$/Total found: $found$/"; sub partition { my ( $s, @c ) = @_; while(1) { # exact change $found++ if $s - amount( \@c, \@w ) == 0; # last match return if $c[0] == $s; # decrement first non-zero my $i = 0; $i++ while $c[$i] == 0; $i == 0 ? $c[0] = 0 : $c[$i]--; @c[ 0 .. $i - 1 ] = (0) x $i; # and redistribute difference for my $j ( reverse 0 .. $i - 1 ) { my $Dj = $s - amount( \@c, \@w ); $c[$j] = int( $Dj / $w[$j] ); } } } sub amount { my ( $x, $y ) = @_; return sum map { $x->[$_] * $y->[$_] } 0 .. $#$x; }

It could use some refactoring, but I was in a hurry and I didn't write it for public consumption ;)

Update: Oh right, yes, output... I get Total found: 4563.


In reply to Re: puzzle: how many ways to make $100 by rhesa
in thread puzzle: how many ways to make $100 by davidj

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others drinking their drinks and smoking their pipes about the Monastery: (19)
    As of 2014-07-24 16:17 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My favorite superfluous repetitious redundant duplicative phrase is:









      Results (162 votes), past polls