Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Dear Monks

I found an interesting puzzle and here is my attempt to solve it. Let say we have NxN matrix.

[ [1, 2] [4, 8] ]
By Joining the columns and rows individually, I get the following four formed-numbers. 12,14,28,48 There Common Divisor is '2'. I like to find the matrix with the highest common divisor. Some condtions are..
  • No formed-number should start with 0.
  • No two formed-numbers be same.
I can do it easily for 2x2 matrix and found highest divisor as 7. and the formed-numbers : 28,21,14,84.
[[2,8] [1,4]]
How I can do with higher dimentions more efficiently? Even for 3X3 with it has to test around billion numbers with my method.

$| += 1; foreach my $divider (2..9){ print "Divider $divider\n"; N: foreach $n (1111..9999){ next if $n =~ /0\d+/; @n = split //,$n; my $count = 0; my @pairs; my %pairs; foreach (qw(01 02 13 23)){ my($p,$q) = split //; my $num = $n[$p].$n[$q]; push @pairs, $num; next N if defined $pairs{$num}; $pairs{$num} = 1; $count++ if $num % $divider == 0; } print join " " => @pairs,"\n" if $count == 4; } }

In reply to Matrix Formation by artist

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?

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

    How do I use this? | Other CB clients
    Other Users?
    Others chanting in the Monastery: (9)
    As of 2020-11-30 20:50 GMT
    Find Nodes?
      Voting Booth?

      No recent polls found