Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
It's actually easier to find the greatest common divisor of two numbers than to factor them. There's an O(log N) algorithm for it. There's one in the snippets: greatest common factor and also one in Math::Pari.
use Math::Pari qw(gcd); my $x = gcd(232,300); print "gcd is $x\n";
Update: A nested loop search is a good way to eliminate cases quickly:
use strict; my @nineset = (9, 8, 7, 6, 5, 4, 3, 2, 1); my @tenset = (9, 8, 7, 6, 5, 4, 3, 2, 1, 0); # Array is arranged: # a b c # d e f # g h i sub gcf { my ($x, $y) = @_; ($x, $y) = ($y, $x % $y) while $y; return $x; } sub multigcf { my $x = shift; $x = gcf($x, shift) while @_; return $x; } my ($a, $b, $c, $d, $e, $f, $g, $h, $i); my ($abc, $def, $ghi, $adg, $beh, $cfi); my ($gcf1, $gcf2, $gcf3, $gcf4); my $maxgcf = 1; my @maxsolution = (); foreach $a (@nineset) { foreach $b (@nineset) { foreach $c (@nineset) { foreach $d (@nineset) { foreach $g (@nineset) { $abc = $a . $b . $c; $adg = $a . $d . $g; next if ($abc eq $adg); $gcf1 = gcf($abc, $adg); next if ($gcf1 <= $maxgcf || $gcf1 == 1); foreach $e (@tenset) { foreach $f (@tenset) { $def = $d . $e . $f; next if ($abc eq $def || $adg eq $def); $gcf2 = multigcf($def, $abc, $adg); next if ($gcf2 <= $maxgcf || $gcf2 == 1); foreach $h (@tenset) { $beh = $b . $e . $h; next if ($abc eq $beh || $adg eq $beh || $def +eq $beh); $gcf3 = multigcf($beh, $def, $abc, $adg); next if ($gcf3 <= $maxgcf || $gcf3 == 1); foreach $i (@tenset) { $cfi = $c . $f . $i; next if ($abc eq $cfi || $adg eq $cfi || $d +ef eq $cfi || $beh eq $cfi); $ghi = $g . $h . $i; next if ($abc eq $ghi || $adg eq $ghi || $d +ef eq $ghi || $beh eq $ghi || $cfi eq $ghi); $gcf4 = multigcf($cfi, $ghi, $beh, $def, $a +bc, $adg); next if ($gcf4 <= $maxgcf || $gcf4 == 1); # Found a good one so far. $maxgcf = $gcf4; @maxsolution = ($a, $b, $c, $d, $e, $f, $g, + $h, $i); } # i } #h } #f } #e } #g } # d } #c } #b } #a ($a, $b, $c, $d, $e, $f, $g, $h, $i) = @maxsolution; print "$a $b $c\n"; print "$d $e $f\n"; print "$g $h $i\n"; print "Common divisor is: $maxgcf\n";
Result I get is:
8 3 2 9 2 8 6 0 8 Common divisor is: 32
Update 2: I fixed a bug in the above program: I had $e . $f . $i where I should have had $c. $f . $i. Now I get the following:
1 7 6 3 9 6 2 2 0 Common divisor is: 44
That's the correct maximum, according to artist. Total time to run was less than two seconds.

Update 3: Another way to do it which would be more practical for larger NxN: I once wrote a program to find answers to crossword puzzles where the word list is given. Starting from the highest number that produces enough N-digit results, make a list of all of its N-digit multiples and fit them to the crossing constraints. The first one that works is the answer.

Update 4: That's basically the solution Rhose has in mind, I think. The starting number he chose, 166, is too big because you need at least two numbers with the same first digit, two others with the same last digit, etc. I'm thinking hashes of digits by position might help.


In reply to Re: Re: Matrix Formation by tall_man
in thread Matrix Formation by artist

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 scrutinizing the Monastery: (3)
    As of 2020-11-27 02:44 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found

      Notices?