Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Matrix Formation

by Solo (Deacon)
on Jun 17, 2003 at 19:28 UTC ( #266612=note: print w/replies, xml ) Need Help??


in reply to Matrix Formation

The greatest common divisor of a set of integers is the product of the primes common to each factored integer (there's a better way to say that, but it's been 10+ years since Number Theory :)

So, your problem is reduced to factoring each of the N^N integers and somehow intersecting the resulting sets of factors.

Quantum::Superpositions claims to make factoring trivial (and possibly the rest of the program, as well).

Update: Hey, Look! Here's a new Q&A node that finds the intersection of sets. I wonder if it handles repeated items, since factorings may look like 8 = (2, 2, 2) or 45 = (3,3,5) for examples...

--Solo

--
You said you wanted to be around when I made a mistake; well, this could be it, sweetheart.

Replies are listed 'Best First'.
Re: Re: Matrix Formation
by tall_man (Parson) on Jun 17, 2003 at 23:20 UTC
    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.

Re: Re: Matrix Formation
by CountZero (Bishop) on Jun 17, 2003 at 19:57 UTC
    Quantum::Superpositions claims to make factoring trivial (and possibly the rest of the program, as well).

    Trivial perhaps, but slow for sure.

    CountZero

    "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://266612]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (9)
As of 2020-11-26 21:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?