Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Here's a little ditty to put your birthday in the corners of a 4 x 4 magic square. In a magic square, the sum of all columns, all rows, and the two major diagonals is the same.

The squares made with this algorithm also have same values in the corner 2 x 2 squares, and the center 2 x 2 square.

The square has the maximum number of unique values allowed by the algorithm. I didn't devise a clever way to disallow negative numbers so folks with low birthmonth or birthday might see negative values. There is a random factor, so running the program again might generate an all positive square.

SquareFoo

#!/usr/bin/perl use strict; if (@ARGV < 1) { print STDERR "\nUsage: $0 mmddyyyy\n\n"; exit; } my ($mm, $dd, $cc, $yy) = ($ARGV[0] =~ m{(..)}g); my $ms = []; $ms->[0] = [$mm, $cc, $yy, $dd]; $ms->[1] = [$yy, $dd, $mm, $cc]; $ms->[2] = [$dd, $yy, $cc, $mm]; $ms->[3] = [$cc, $mm, $dd, $yy]; my $most = 12 + uniq($ms); if ($mm == $dd) { $most--; } if ($mm == $cc) { $most--; } if ($yy == $dd) { $most--; } if ($yy == $cc) { $most--; } while (uniq($ms) < $most) { if (int(rand(2))) { if (int(rand(2))) { rot ($ms, 1, 0, 1); rot ($ms, 1, 2, -1); } else { rot ($ms, 1, 0, -1); rot ($ms, 1, 2, 1); } } else { if (int(rand(2))) { rot ($ms, 0, 1, 1); rot ($ms, 2, 1, -1); } else { rot ($ms, 0, 1, -1); rot ($ms, 2, 1, 1); } } } show ($ms); #----------------------------------------------------------- sub uniq { my ($ms) = @_; my (%used); for my $row (0..3) { for my $col (0..3) { $used{$ms->[$row][$col]}++; } } return scalar(keys(%used)); } #----------------------------------------------------------- sub rot { my ($ms, $row, $col, $val) = @_; $ms->[$row] [$col] += $val; $ms->[$row+1][$col+1] += $val; $ms->[$row] [$col+1] -= $val; $ms->[$row+1][$col] -= $val; } #----------------------------------------------------------- sub show { my ($ms) = @_; for my $row (0..3) { for my $col (0..3) { printf(STDOUT "%2d ", $ms->[$row][$col]); } print STDOUT "\n"; } }

In reply to Magic Birthday Square by YuckFoo

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, 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, 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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others avoiding work at the Monastery: (6)
    As of 2019-11-19 03:23 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      Strict and warnings: which comes first?



      Results (93 votes). Check out past polls.

      Notices?