#!/usr/bin/perl use strict; use warnings; use constant MATRIX_DIM => 3; # Set your matrix size here use constant MAX_NUM => (10 ** MATRIX_DIM) - 1; # 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 @Sol=(); sub CheckSolution { my \$pNum=shift; my @Sol=(); # INCOMPLETE CODE # # Place code here which checks every combination of MATRIX_DIM numbers as rows # and sees if there are MATRIX_DIM other numbers which can act as columns. # # For example, for a 3 x 3 matrix, this code should go through each combination # 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 columns. This should # be a workable computation for all smaller matrix sizes. # return @Sol; } while (\$Divisor >= 1) { @Num=(); foreach (1..int(MAX_NUM / \$Divisor)) { next if ((\$Divisor * \$_) < (MAX_NUM / 10)); #Skip numbers which begin with zero push @Num, (\$Divisor * \$_); } print 'Checking solution for divisor ',\$Divisor,': ',join('-',@Num),"\n"; @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"; }