in reply to How many ways to make change?
The output includes zeroes for any denominations on the left, which coincidentally makes the output tidier (or undesirably verbose, depending on your viewpoint).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) ); }
|
|---|