Dear Monks
I found an interesting puzzle and here is my attempt to solve it. Let say we have NxN matrix.
[
[1, 2]
[4, 8]
]
By Joining the columns and rows individually, I get the following four formed-numbers.
12,14,28,48 There Common Divisor is '2'. I like to find the matrix with the highest common divisor. Some condtions are..
- No formed-number should start with 0.
- No two formed-numbers be same.
I can do it easily for 2x2 matrix and found highest divisor as 7.
and the formed-numbers : 28,21,14,84.
[[2,8]
[1,4]]
How I can do with higher dimentions more efficiently?
Even for 3X3 with it has to test around billion numbers with my method.
$| += 1;
foreach my $divider (2..9){
print "Divider $divider\n";
N: foreach $n (1111..9999){
next if $n =~ /0\d+/;
@n = split //,$n;
my $count = 0;
my @pairs;
my %pairs;
foreach (qw(01 02 13 23)){
my($p,$q) = split //;
my $num = $n[$p].$n[$q];
push @pairs, $num;
next N if defined $pairs{$num};
$pairs{$num} = 1;
$count++ if $num % $divider == 0;
}
print join " " => @pairs,"\n" if $count == 4;
}
}
Thanks
artist
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|