On Tue, 8 Oct 2002, Chris Devers wrote:

> On a different note, last weekend on NPR there was a puzzle that it seems
> to me could be solved pretty neatly by a Perl script, and I'm curious
> what solutions people would try for it. Consider the following string:
> 
>     1 2 3 4 5 6 7 8 9    =    2002
> 
> The problem is to add any number of addition & multiplication operations
> wherever you'd like on the left such that in the end you have a valid
> equation. So for example if it gets you to a solution you can have:
> 
>     12 * 345 + 6 ...
> 
> if that works as part of your solution [it's much too big: 4146].
> 
> Bearing in mind that multiplication takes higher precedence than addition,
> what is the solution? Supposedly there is only one correct answer, but I
> haven't yet figured it out. Anyone want to suggest a clever solution?
> Maybe a oneliner? Maybe a proof of multiple correct answers? Fire away :)

As for a proof for multiple correct answers wht about

[mike@ratdog tmp]$ perl -de 1

Loading DB routines from perl5db.pl version 1.19
Editor support available.

Enter h or `h h' for help, or `man perldebug' for more help.

main::(-e:1):   1
  DB<1> print 1*2+34*56+7+89
2002
  DB<2> print 1*23+45*6*7+89
2002

Mike

Unfortunately the code to do it looks like

g = Generator.new(%w(1 2 3 4 5 6 7 8 9), ['', '+', '*'])
g.each { |expr|
    puts expr if eval(expr) == 2002
}

or

G.new('123456789', '+*').find_all {|e| eval(e) == 2002}.each {|e| puts e}

so I'll leave a non-brute force perl one line implementation to the reader
;-)

-- 
[EMAIL PROTECTED]                    |           The "`Stok' disclaimers" apply.
http://www.stok.co.uk/~mike/       | GPG PGP Key      1024D/059913DA 
[EMAIL PROTECTED]                  | Fingerprint      0570 71CD 6790 7C28 3D60
http://www.exegenix.com/           |                  75D2 9EC4 C1C0 0599 13DA

_______________________________________________
Boston-pm mailing list
[EMAIL PROTECTED]
http://mail.pm.org/mailman/listinfo/boston-pm

Reply via email to