RabbitFarm
2021-10-17
A Couple of Brute Force Computations
The examples used here are from The Weekly Challenge problem statement and demonstrate the working solution.
Part 1
Write a script to generate first 5 Pandigital Numbers in base 10.
Solution
use strict;
use warnings;
##
# Write a script to generate first 5 Pandigital Numbers in base 10.
##
use boolean;
sub first_n_pandigitals {
my ($n) = @_;
my $found = false;
my $pandigitals = [];
my $x = 1_000_000_000;
do {
my $test = $x;
push @{$pandigitals}, $x
if ( $test =~ tr/0//d ) > 0
&& ( $test =~ tr/1//d ) > 0
&& ( $test =~ tr/2//d ) > 0
&& ( $test =~ tr/3//d ) > 0
&& ( $test =~ tr/4//d ) > 0
&& ( $test =~ tr/5//d ) > 0
&& ( $test =~ tr/6//d ) > 0
&& ( $test =~ tr/7//d ) > 0
&& ( $test =~ tr/8//d ) > 0
&& ( $test =~ tr/9//d ) > 0;
$found = ( @{$pandigitals} == $n );
$x++;
} while ( !$found );
return $pandigitals;
}
sub first_5_pandigitals {
return first_n_pandigitals(5);
}
MAIN: {
my $pandigitals = first_5_pandigitals;
for my $x ( @{$pandigitals} ) {
print "$x\n";
}
}
Sample Run
$ perl perl/ch-1.pl
1023456789
1023456798
1023456879
1023456897
1023456978
Notes
From the definition we know that we will need at least 10 digits and, intuitively, the
first five pandigital numbers will start with 1
. So then, we start with 1_000_000_000
and iterate upwards testing each candidate until we find the first five. The test used
here is to determine if tr
finds all the required digits.
Part 2
You are given 2 positive numbers, $m and $n. Write a script to generate multiplication table and display count of distinct terms.
Solution
use strict;
use warnings;
##
# You are given 2 positive numbers, $m and $n.
# Write a script to generate multiplcation table and display count of distinct terms.
##
sub compute_print {
my ( $m, $n ) = @_;
my $distinct = {};
print " x | " . join( " ", ( 1 .. $n ) ) . "\n";
print "---+-" . "-" x ( $n * 2 - 1 ) . "\n";
for my $i ( 1 .. $m ) {
print " $i | " . join( " ", map { $i * $_ } ( 1 .. $n ) ) . "\n";
for my $j ( 1 .. $n ) {
$distinct->{ $i * $j } = undef;
}
}
return $distinct;
}
MAIN: {
my $distinct = compute_print( 3, 3 );
print "Distinct Terms: "
. join( ", ", sort { $a <=> $b } keys %{$distinct} ) . "\n";
print "Count: " . keys( %{$distinct} ) . "\n";
print "\n\n";
$distinct = compute_print( 3, 5 );
print "Distinct Terms: "
. join( ", ", sort { $a <=> $b } keys %{$distinct} ) . "\n";
print "Count: " . keys( %{$distinct} ) . "\n";
}
Sample Run
$ perl perl/ch-2.pl
x | 1 2 3
---+------
1 | 1 2 3
2 | 2 4 6
3 | 3 6 9
Distinct Terms: 1, 2, 3, 4, 6, 9
Count: 6
x | 1 2 3 4 5
---+----------
1 | 1 2 3 4 5
2 | 2 4 6 8 10
3 | 3 6 9 12 15
Distinct Terms: 1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15
Count: 11
Notes
This is a perfectly Perl shaped problem. The computations can be handled in a
straightforward way, especially with map
. Getting rid of duplicates is done using
the idiomatic method with hash keys. Finally, formatting the output cleanly is done
without much undo stress. Compare what we do here to format the table with what was
necessary to represent the
same table in Prolog.
References
posted at: 13:03 by: Adam Russell | path: /perl | permanent link to this entry