After seeing the following puzzle floating around the net, I decided to automate it:
Take the digits 9, 8, 7, 6, 5, 4, 3, 2, 1 in that order. Insert one or more basic math operators (addition, subtraction, division, multiplication) so that the result makes 2006.
#!/usr/bin/perl use strict; use warnings; my @o = (" + ", " - ", " * ", " / ", ""); my @s = (" ", "-"); my @d = (0, 1 .. 9); my $year = @ARGV ? shift : 2006; for my $o8 (@o) { for my $o7 (@o) { for my $o6 (@o) { for my $o5 (@o) { for my $o4 (@o) { for my $o3 (@o) { for my $o2 (@o) { for my $o1 (@o) { for my $s (@s) { my $expr = "$s$d[9]$o8$d[8]$o7$d[7]$o6$d[6]$o5" . "$d[5]$o4$d[4]$o3$d[3]$o2$d[2]$o1$d[1]"; print "$expr == $year\n" if $year == eval $expr; }}}}}}}}} __END__ 2006 == -9 + 8 * 7 + 654 * 3 - 2 - 1 2006 == 9 + 8 * 7 + 654 * 3 - 21 2006 == 9 + 8 * 7 * 6 * 5 - 4 + 321 2006 == 9 * 8 - 7 + 654 * 3 - 21
I'm sure there are cleverer ways of writing the nested loop, using some kind of module. But cut-and-paste is fast, and this takes less programmer time.
Perl --((8:>*

In reply to Happy 2006 by Perl Mouse

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.