user who wrote:
> 4 numbers (all from 1-9) are given, make a formula(include +-*/()) for
> result 24.  ex:
> 
> 4, 2, 7, 5
> (4-2)*(7+5) = 24
> 
> shortest/quickest solution?

a solution:

$ make-24 4 2 7 5
solution: (4)/2*(7+5) == 24
$ make-24 4 2 7 5
solution: 4/2*(7+5) == 24
$ make-24 4 2 7 5
solution: 4/2*(7+5) == 24
$ ./make-24 4 2 7 5
solution: 4/2*(7+5) == 24
$ ./make-24 4 2 7 5
solution: (4/2)*(7+5) == 24
$ ./make-24 4 2 7 5
solution: (4/2)*(7+5) == 24

__BEGIN__
#!/usr/bin/perl -w
# make-24 <N> <N> <N> <N>
use strict;

my @OP = qw(+ - * /);

{
  my $expr = do {
    local @ARGV = @ARGV;
    my $term;
    {
      $term .= '(' if int rand 2;
      $term .= shift;
      $term .= ')' if int rand 2;
      last if !@ARGV;
      $term .= $OP[rand @OP];
      redo;
    }
    "$term == 24"
  };

  eval $expr and do { print "solution: $expr\n"; last };
  
  redo;
}
__END__
--
Steve Lane <[EMAIL PROTECTED]>

Reply via email to