There are 292 ways to break a dollar using half-dollars, quarters, dimes, nickels, and cents. The following snippet lists them all. Use
% perl make_change.pl 100 50 25 10 5 1
More generally, if you want to find out how many ways one can break n into chunks of size m1,...,mk, then the following command will print them out:

% perl make_change.pl n m1 ... mk

Update: Spurred by Roy Johnson's reply++ I tightened my version slightly to take care of edge cases in a more natural way, and explicitly list the 0 coefficients. Originally I thought I liked the looks of the "zero-less" output better, but on a second thought I realized that a version of the core function that returns all the coefficients is more generally useful; the output can be easily tweaked by the calling code. The current version lists all the coefficients, one for each coin.

Keyword: partitions.

use strict; use warnings; sub make_change { my $amount = shift; return $amount ? () : [] unless @_; my $coin = pop; my $n_coins = 0; my $base = 0; my @ways; while ( $base <= $amount ) { push @ways, map [ @$_, [ $n_coins, $coin ] ], make_change( $amount - $base, @_ ); $n_coins++; $base += $coin; } return @ways; } sub print_way { my $way = shift; print join ' + ', map "$_->[ 0 ] x $_->[ 1 ]", @$way; print $/; } print_way( $_ ) for make_change( @ARGV );

Replies are listed 'Best First'.
Re: How many ways to make change?
by Roy Johnson (Monsignor) on May 04, 2005 at 03:27 UTC
    Nice work on a mind-bending puzzle. I refactored it some to combine base cases and whatnot and came up with this probably-too-dense version:
    sub my_make_change { my $amount = shift; my $coin = shift || -1; return( map { my $n = $_; map [ [ $n, $coin ], @$_ ], ($n*$coin == $amount ? [] : my_make_change( $amount - $n*$coin, @_ )); } (0..$amount/$coin) ); }
    The output includes zeroes for any denominations on the left, which coincidentally makes the output tidier (or undesirably verbose, depending on your viewpoint).

    Caution: Contents may have been coded under pressure.
Re: How many ways to make change?
by blokhead (Monsignor) on May 04, 2005 at 04:52 UTC
    Thare are some other neat solutions to this problem at How to generate restricted partitions of an integer.

    Of course, if you'll indulge me to demonstrate a silly way to do it, I'd use regexes on unary strings as I did in my other change-making posts {1, 2}:

    my $amount = shift; my $regex = join "", map { "(1{$_})*" } @ARGV; my $incr_x = qr/(?{$x++})/; local $x; (1 x $amount) =~ /^$regex(?:$)$incr_x(?!)/; print "$x\n";

    blokhead