Next | The Perl Hardware Store | DC.pm Version | 35 |
sub divide { my ($target, $treasures) = @_; return [] if $target == 0; return undef if $target < 0 || @$treasures == 0;
my @rest = @$treasures; my $first = shift @rest; my $solution; $solution = divide($target - $first, \@rest); return [$first, @$solution] if $solution; return divide($target , \@rest); }
To invoke:
divide(985, [112, 87, ..., 127]);
Returns array of treasure values
Undefined value if no solution is possible
Next | Copyright © 2003 M. J. Dominus |