Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

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:

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

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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?

    What's my password?
    Create A New User
    [shmem]: karlgoethebier: np, got it. But doesn't either :-/
    [shmem]: just leaving it as is. After all, enough effort put into that. hrmpf.
    [karlgoethebier]: shmem: ah i see: no inline styles in the approved html :-{

    How do I use this? | Other CB clients
    Other Users?
    Others avoiding work at the Monastery: (15)
    As of 2017-07-24 16:51 GMT
    Find Nodes?
      Voting Booth?
      I came, I saw, I ...

      Results (356 votes). Check out past polls.