aboutsummaryrefslogtreecommitdiff
path: root/challenge-066/craig/perl
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2020-06-24 06:08:23 +0100
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2020-06-24 06:08:23 +0100
commit36e98b2a6eaea2411c45c6259c92dc737c651f42 (patch)
tree56b074a4a8c226f47a50e7ade2dc6efe4bcfd990 /challenge-066/craig/perl
parent7bcc4a63695846396ff27f0d658167243e959fc4 (diff)
downloadperlweeklychallenge-club-36e98b2a6eaea2411c45c6259c92dc737c651f42.tar.gz
perlweeklychallenge-club-36e98b2a6eaea2411c45c6259c92dc737c651f42.tar.bz2
perlweeklychallenge-club-36e98b2a6eaea2411c45c6259c92dc737c651f42.zip
- Added solutions by Craig.
Diffstat (limited to 'challenge-066/craig/perl')
-rw-r--r--challenge-066/craig/perl/ch-1.pl20
-rw-r--r--challenge-066/craig/perl/ch-2.pl27
2 files changed, 47 insertions, 0 deletions
diff --git a/challenge-066/craig/perl/ch-1.pl b/challenge-066/craig/perl/ch-1.pl
new file mode 100644
index 0000000000..d90c25040a
--- /dev/null
+++ b/challenge-066/craig/perl/ch-1.pl
@@ -0,0 +1,20 @@
+use strict;
+use v5.16;
+
+my $m = shift;
+my $n = shift;
+
+die "\nDivide by zero not allowed.\n" if $n == 0;
+
+my $count = 0;
+my $multiplier = ($m < 0 xor $n < 0) ? -1 : 1;
+
+($m, $n) = (abs($m), abs($n));
+
+while ($m >= $n) {
+ $m -= $n;
+ $count += $multiplier;
+}
+
+# Floor negative result if division was not perfect
+say $count + ($m != 0 && $multiplier == -1 ? -1 : 0);
diff --git a/challenge-066/craig/perl/ch-2.pl b/challenge-066/craig/perl/ch-2.pl
new file mode 100644
index 0000000000..a847a8d201
--- /dev/null
+++ b/challenge-066/craig/perl/ch-2.pl
@@ -0,0 +1,27 @@
+use strict;
+use v5.16;
+
+my $n = shift;
+die "\nMust provide positive integer.\n" if !$n || $n < 0;
+my $exp = 1;
+my $result;
+
+# Work up to $n in powers of 2
+do {
+ $result = 2 ** $exp;
+ $exp++;
+} while ($result < $n);
+
+# Find nth root up to $exp
+my $i = 2;
+while ($i <= $exp) {
+ $result = $n ** (1/$i);
+ if ($result !~ /\D/) {
+ say "$result^$i";
+ exit;
+ } else {
+ $i++;
+ }
+}
+
+say "0";