### Re: Re: Matrix Formation

by tall_man (Parson)
 on Jun 17, 2003 at 23:20 UTC ( #266681=note: print w/replies, xml ) Need Help??

in reply to Re: Matrix Formation
in thread Matrix Formation

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
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.

Create A New User
Node Status?
node history
Node Type: note [id://266681]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2020-11-27 03:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?

No recent polls found

Notices?