aboutsummaryrefslogtreecommitdiff
path: root/challenge-134
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2021-10-16 22:58:02 +0100
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2021-10-16 22:58:02 +0100
commitc5ed2c98762a4b56a628419317c14dfd8e276b74 (patch)
tree8bbf8f539a0f74c50f10d37487fd43006d707879 /challenge-134
parent13f99be2b57472c86b8cabe9a90c9dbacc5aefc6 (diff)
downloadperlweeklychallenge-club-c5ed2c98762a4b56a628419317c14dfd8e276b74.tar.gz
perlweeklychallenge-club-c5ed2c98762a4b56a628419317c14dfd8e276b74.tar.bz2
perlweeklychallenge-club-c5ed2c98762a4b56a628419317c14dfd8e276b74.zip
- Added solutions by Pete Houston.
Diffstat (limited to 'challenge-134')
-rwxr-xr-xchallenge-134/pete-houston/perl/ch-1.pl45
-rwxr-xr-xchallenge-134/pete-houston/perl/ch-2.pl42
2 files changed, 87 insertions, 0 deletions
diff --git a/challenge-134/pete-houston/perl/ch-1.pl b/challenge-134/pete-houston/perl/ch-1.pl
new file mode 100755
index 0000000000..a897760ce4
--- /dev/null
+++ b/challenge-134/pete-houston/perl/ch-1.pl
@@ -0,0 +1,45 @@
+#!/usr/bin/env perl
+#===============================================================================
+#
+# FILE: 13401.pl
+#
+# USAGE: ./13401.pl [ N ]
+#
+# DESCRIPTION: Print first 5 (or N) pandigital decimal numbers
+#
+# OPTIONS: If N is not provided, defaults to 5.
+# REQUIREMENTS: Math::Combinatorics for permutations
+# NOTES: This will fail for sufficiently high N (>10^6 or so)
+# AUTHOR: Pete Houston (pete), cpan@openstrike.co.uk
+# ORGANIZATION: Openstrike
+# VERSION: 1.0
+# CREATED: 11/10/21
+#===============================================================================
+
+use strict;
+use warnings;
+use Math::Combinatorics;
+
+# General case
+my $n = $ARGV[0] // 5;
+my $i = 1;
+my $fac = 1;
+$i++ while (($fac *= $i) < $n);
+
+# Permute the $i highest digits
+my $smallest = "1023456789";
+my $post = substr $smallest, 10 - $i, $i, '';
+my @swaps = split (//, $post);
+
+my $combo = Math::Combinatorics->new (count => $i, data => [@swaps]);
+my @pans;
+
+while (my @perm = $combo->next_permutation) {
+ push @pans, join '', $smallest, @perm;
+}
+
+# Sort these, print the first $n
+for my $pan (sort @pans) {
+ print "$pan\n";
+ last unless --$n;
+}
diff --git a/challenge-134/pete-houston/perl/ch-2.pl b/challenge-134/pete-houston/perl/ch-2.pl
new file mode 100755
index 0000000000..562fa1ddfb
--- /dev/null
+++ b/challenge-134/pete-houston/perl/ch-2.pl
@@ -0,0 +1,42 @@
+#!/usr/bin/env perl
+#===============================================================================
+#
+# FILE: 13402.pl
+#
+# USAGE: ./13402.pl ROWS COLUMNS
+#
+# DESCRIPTION: Multiplication table and count of distinct products
+#
+# AUTHOR: Pete Houston (pete), cpan@openstrike.co.uk
+# ORGANIZATION: Openstrike
+# VERSION: 1.0
+# CREATED: 11/10/21
+#===============================================================================
+
+use strict;
+use warnings;
+
+my ($r, $c) = @ARGV;
+
+my $maxlenp = length ($r * $c) + 1;
+my $maxlenr = length ($r);
+
+my $ifmt = "%${maxlenp}i";
+my $rfmt = "%${maxlenr}i";
+
+my $row;
+$row .= sprintf $ifmt, $_ for 1 .. $c;
+printf "%${maxlenr}s | %s\n", 'x', $row;
+
+print (('-' x ${maxlenr}) . '-+-' . ('-' x ($maxlenp * $c)) . "\n");
+
+my %seen;
+for my $i (1 .. $r) {
+ my @rval = map { $_ *= $i } 1 .. $c;
+ @seen{@rval} = (1) x $r;
+ $row = '';
+ $row .= sprintf $ifmt, $_ for @rval;
+ printf "$rfmt | %s\n", $i, $row;
+}
+print "\nDistinct Terms: " . join (', ', sort { $a <=> $b } keys %seen) . "\n";
+print "Count: " . keys (%seen) . "\n";