Next The Perl Hardware Store DC.pm Version 35

Treasure Division

        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);
        }          
        divide(985, [112, 87, ..., 127]);


Next Copyright © 2003 M. J. Dominus