CUFP
Rhose
This code "solves" [artist]'s puzzle posted in [Matrix Formation]. The problem presented was to find the largest divisor for an N x N matrix of digits such that the numbers formed by joining the digits in each row and numbers formed by joining the digits in each column all have a common divisor. In addition, the following restrictions were added:<ol><li>No formed-number should start with 0.</li><li>No two formed-numbers are allowed to be the same.</li></ol>For example, a 2 x 2 matrix is solved as:<p>
<tt>
2 1<p>
8 4<p>
</tt><p>
The numbers formed are: 21, 84, 28, and 14 which all have a divisor of 7.<p>
The code below has come up with the follwing solutions:<ul><li>2x2: Divisor 7, Rows 21, 84</li><li>3x3: Divisor 44, Rows 132, 792, 660</li><li>4x4: Divisor 416, Rows 2496, 9152, 1664, 2080</li></ul>
<readmore>
<code>
#!/usr/bin/perl
#
# NOTES: This code will not solve a 1 x 1 matrix as all numbers are "reused"
#
use strict;
use warnings;
use constant TRUE => 1;
use constant FALSE => 0;
use constant MATRIX_DIM => 3; # Set your matrix size here
use constant MAX_NUM => (10 ** MATRIX_DIM) - 1;
use constant DEBUG => 0; # 0=Nothing, 1=Summary, 2=Detail
use constant SEPARATOR => '-';
# Since you need to find 2 times the matrix width distinct numbers, you cannot
# have a divisor greater than the maximum number divided by that product.
#
# For example, a 3 x 3 matrix has a maximum number of 999. You need 6 distinct
# numbers to "solve" the matrix, so the maximum possible divisor would be 166.
# 166 would result in 166, 332, 498, 664, 830, and 996.
#
my $Divisor = int(MAX_NUM / (MATRIX_DIM * 2));
my @Num;
my @PermPtrs; # Saves the position of the pointers between GetNextPerm() calls
my @Sol=();
# GetNextPerm generates purmutations from the passed array (one at a time)
# and uses a global array of pointers (@PermPtrs). This method seems to be
# fast and uses less memory than computing all the permutations at once.
#
# NOTE: The first time this sub is called, @PermPtrs should be an empty array.
#
sub GetNextPerm
{
my $pNum=shift;
my $Perm='';
my @PtrStack;
if ($#PermPtrs > -1)
{
my $Ptr=MATRIX_DIM-1;
while (TRUE)
{
$PermPtrs[$Ptr]++;
if ($PermPtrs[$Ptr] > $#$pNum)
{
push @PtrStack,$Ptr;
$Ptr--;
return undef if ($Ptr == -1);
next;
}
else
{
my $OkInc=TRUE;
foreach (0..MATRIX_DIM-1)
{
next if ($_ == $Ptr);
$OkInc=FALSE if ($PermPtrs[$_] == $PermPtrs[$Ptr]);
}
next unless($OkInc)
}
last unless ($#PtrStack > -1);
$Ptr=pop(@PtrStack);
$PermPtrs[$Ptr]=-1;
}
}
else
{
@PermPtrs=(0..MATRIX_DIM-1);
}
foreach (@PermPtrs)
{
$Perm.=SEPARATOR if (length($Perm));
$Perm.=$$pNum[$_];
}
return $Perm;
}
sub CheckSolution
{
my $pNum=shift;
my $CheckNum;
my $PossSol;
my $Ptr;
my @Sol=();
my %Check;
my %Num;
#
# This code checks every permutation of possible numbers (from the passed
# numberarray) as rows and checks if there are MATRIX_DIM other numbers
# (also in the passed array) which can act as columns.
#
# For example, with a 3 x 3 matrix, this code should go through each
# permutation of three number (for rows) from the passed array and see if
# there are three other numbers which would work with the selected rows as
# their columns.
#
# WISH LIST: Verify members of each permutation for "fitness" in their
# assigned spot. "Fitness" means you do not put a number in the
# top row whose first digit is not the first digit of at least
# one other number... whose second digit is not the first digit
# of at least one other number... etc... This may speed up this
# section of the code.
#
# Benchmark the code which checks the columns (foreach $Ptr block)
# That code gets executed a lot, and *may* benefit from recoding.
#
@PermPtrs=();
while($PossSol=GetNextPerm($pNum))
{
print "\t",'Checking ',$PossSol,'... ' if (DEBUG >= 2);
%Num=map { $_ => TRUE } @$pNum;
@Sol=split(SEPARATOR,$PossSol);
delete $Num{$_} foreach (@Sol);
foreach $Ptr (0..length($Sol[0])-1)
{
$CheckNum='';
$CheckNum.=substr($_,$Ptr,1) foreach (@Sol);
if (defined($Num{$CheckNum}))
{
delete $Num{$CheckNum};
}
else
{
print 'nope',"\n" if (DEBUG >= 2);
@Sol=();
last;
}
}
last if ($#Sol > -1);
}
return @Sol;
}
while ($Divisor >= 1)
{
@Num=();
foreach (1..int(MAX_NUM / $Divisor))
{
next if (($Divisor * $_) < (MAX_NUM / 10)); # Numbers which begin with zero
push @Num, ($Divisor * $_);
}
print 'Checking solution for divisor ',$Divisor,': ',join('-',@Num),"\n"
if (DEBUG >= 1);
@Sol=CheckSolution(\@Num);
last if ($#Sol > -1);
$Divisor--;
}
print "\n";
if ($#Sol > -1)
{
print 'Maximum divisor is ',$Divisor,', solution is ',join('-',@Sol),"\n";
}
else
{
print 'No solution found...',"\n";
}
# End of Script
</code>
</readmore><p>
<b>Observation:</b> Unless I really missed something, I could not find a good CPAN module which would generate permutations of n distinct numbers taken r at a time. This problem led to the GetNextPerm() sub which uses a series or r pointers which are moved through the array of numbers generating one permutation at a time.