From 65d0924db079387e5b623da559aab651f0c73124 Mon Sep 17 00:00:00 2001 From: corvettes13 <86648326+corvettes13@users.noreply.github.com> Date: Thu, 2 Dec 2021 20:37:06 -0600 Subject: Create ch-2.pl --- challenge-140/paul-fajman/perl/ch-2.pl | 38 ++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 challenge-140/paul-fajman/perl/ch-2.pl 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"; -- cgit