A little arithmetic puzzle

Jasvir Nagra <[email protected]>
Newsgroups gmane.comp.lang.perl.fun
Message-ID <[email protected]>
The list has been fairly quiet of late so I thought I would share the
following simple little puzzle I heard recently.

Using the numbers 1,5,6,7 exactly once and using any of addition,
subtraction, division and/or multiplication zero or more times form an
expression that evaluates to 21.  You may use brackets.

So  1 + 5 * (7 - 6) = 6  fr'instance
and 1 * 5 + 6 * 7   = 47

but the puzzle needs 21.

Yes, you must use every number at least once.
No, you may not repeat any number.
No, you may not use powers just +-/*.
No, you may not concatenate numbers together.

The puzzle is sufficiently simple that it can be solved with a pencil
and paper (although I think its not quite trivial and took me a couple
of hours).  The Fun offcourse is in getting perl to solve it.

To get things started, here is one solution - without a real quantum
computer or at least a real grunty network of machines, its not going
to terminate any time soon.  But its clear and simple.

#!/usr/bin/perl -lw

use Quantum::Superpositions;
use strict;

my $tree = my $num = any(1,5,6,7);
my $op = any qw!+ - / *!;

$tree =  any eigenstates "($num $op $tree)",
             eigenstates "($tree $op $num)"
  for 1..3;

print grep { 21 == eval and not /(\d).*\1/ } eigenstates $tree;
__END__


Your quest is to find more efficient or elegant solutions.

-- 
Jasvir Nagra
http://www.cs.auckland.ac.nz/~jas
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.