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.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.