#!/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