aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcorvettes13 <86648326+corvettes13@users.noreply.github.com>2021-12-02 20:37:06 -0600
committerGitHub <noreply@github.com>2021-12-02 20:37:06 -0600
commit65d0924db079387e5b623da559aab651f0c73124 (patch)
tree01a0a0e49e86cb47fc82125da55aae9dc813c1f4
parentb6307d9d53d84d5342bdab2a6a39cf6285ff97bf (diff)
downloadperlweeklychallenge-club-65d0924db079387e5b623da559aab651f0c73124.tar.gz
perlweeklychallenge-club-65d0924db079387e5b623da559aab651f0c73124.tar.bz2
perlweeklychallenge-club-65d0924db079387e5b623da559aab651f0c73124.zip
Create ch-2.pl
-rw-r--r--challenge-140/paul-fajman/perl/ch-2.pl38
1 files changed, 38 insertions, 0 deletions
diff --git a/challenge-140/paul-fajman/perl/ch-2.pl b/challenge-140/paul-fajman/perl/ch-2.pl
new file mode 100644
index 0000000000..0cb95394bf
--- /dev/null
+++ b/challenge-140/paul-fajman/perl/ch-2.pl
@@ -0,0 +1,38 @@
+#!/usr/bin/perl
+
+# Weekly Challenge 140 Task 2
+#
+# You are given 3 positive integers, $i, $j and $k.
+#
+# Write a script to print the $kth element in the sorted multiplication table of $i and $j.
+
+use strict;
+use warnings;
+
+my ($i, $j, $k);
+
+if ($#ARGV eq 2) {
+ $i = $ARGV[0];
+ $j = $ARGV[1];
+ $k = $ARGV[2];
+}
+else {
+ print "Using test data. Provide 3 input numbers to test your own numbers.\n";
+ $i = 3;
+ $j = 3;
+ $k = 6;
+}
+
+my @mtable;
+
+for my $x (1..$i) {
+ for my $y (1..$j) {
+ push @mtable, $x*$y;
+ }
+}
+
+# Need the $a <=> $b comparison for numeric values
+my @stable = sort { $a <=> $b } @mtable;
+my $kth = $stable[$k-1];
+
+print "\n$kth is element number $k in multiplication table $i x $j\n";