aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-134/andrezgz/perl/ch-1.pl37
-rw-r--r--challenge-134/andrezgz/perl/ch-2.pl72
2 files changed, 109 insertions, 0 deletions
diff --git a/challenge-134/andrezgz/perl/ch-1.pl b/challenge-134/andrezgz/perl/ch-1.pl
new file mode 100644
index 0000000000..d0d31b0c48
--- /dev/null
+++ b/challenge-134/andrezgz/perl/ch-1.pl
@@ -0,0 +1,37 @@
+#!/usr/bin/perl
+
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-134/
+# TASK #1
+#
+# Write a script to generate first 5 Pandigital Numbers in base 10.
+#
+# As per the wikipedia (https://en.wikipedia.org/wiki/Pandigital_number), it says:
+#
+# A pandigital number is an integer that in a given base has among its significant digits each digit used in the base at least once.
+
+use strict;
+use warnings;
+use feature 'say';
+
+# start from the first obvious pandigital number in base 10
+my $n = 1023456789;
+
+my @pandigitals;
+while (@pandigitals < 5) {
+ my %unique;
+ $unique{$_}++ for split //, $n;
+ push @pandigitals, $n if '1' x 10 eq join '', values %unique;
+ $n++;
+}
+
+say join "\n", @pandigitals;
+exit 0;
+
+__END__
+
+$ ./ch-1.pl
+1023456789
+1023456798
+1023456879
+1023456897
+1023456978
diff --git a/challenge-134/andrezgz/perl/ch-2.pl b/challenge-134/andrezgz/perl/ch-2.pl
new file mode 100644
index 0000000000..297822db34
--- /dev/null
+++ b/challenge-134/andrezgz/perl/ch-2.pl
@@ -0,0 +1,72 @@
+#!/usr/bin/perl
+
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-134/
+# Task #2
+#
+# You are given 2 positive numbers, $m and $n.
+#
+# Write a script to generate multiplcation table and display count of distinct terms.
+#
+# Example 1
+# Input: $m = 3, $n = 3
+# Output:
+#
+# 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
+# Example 2
+# Input: $m = 3, $n = 5
+# Output:
+#
+# 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
+
+use strict;
+use warnings;
+use feature 'say';
+
+my $m = shift // 3;
+my $n = shift // 3;
+
+say sprintf ' x |' . '%3d' x $n, 1..$n;
+say '---|' . '---' x $n;
+my %unique;
+for my $row (1..$m) {
+ my @mult = map { $row * $_ } 1..$n;
+ @unique{@mult} = (undef) x @mult;
+ say sprintf '%2d |' . '%3d' x @mult, $row, @mult;
+}
+
+say 'Distinct Terms: ' . join ',', sort {$a <=> $b} keys %unique;
+say 'Count: ' . scalar keys %unique;
+
+__END__
+
+$ ./ch-2.pl 3 3
+ 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
+
+$ ./ch-2.pl 3 5
+ 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