aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorwanderdoc <wanderdoc@googlemail.com>2020-06-23 19:46:47 +0200
committerwanderdoc <wanderdoc@googlemail.com>2020-06-23 19:46:47 +0200
commit8484ee7816bc3a601868e73b163c2b0331de3bf1 (patch)
treec1bc7cffaebe7c4902465410a167ef7bfc528541
parentf68b9cf5d79b8bd60690fd12d7bc12f48e5d176f (diff)
downloadperlweeklychallenge-club-8484ee7816bc3a601868e73b163c2b0331de3bf1.tar.gz
perlweeklychallenge-club-8484ee7816bc3a601868e73b163c2b0331de3bf1.tar.bz2
perlweeklychallenge-club-8484ee7816bc3a601868e73b163c2b0331de3bf1.zip
Solutions challenge-066.
-rw-r--r--challenge-066/wanderdoc/perl/ch-1.pl59
-rw-r--r--challenge-066/wanderdoc/perl/ch-2.pl52
2 files changed, 111 insertions, 0 deletions
diff --git a/challenge-066/wanderdoc/perl/ch-1.pl b/challenge-066/wanderdoc/perl/ch-1.pl
new file mode 100644
index 0000000000..9661aa929f
--- /dev/null
+++ b/challenge-066/wanderdoc/perl/ch-1.pl
@@ -0,0 +1,59 @@
+#!perl
+use strict;
+use warnings FATAL => qw(all);
+
+=prompt
+You are given two integers $M and $N.
+
+Write a script to divide the given two integers i.e. $M / $N without using multiplication, division and mod operator and return the floor of the result of the division.
+Example 1:
+
+Input: $M = 5, $N = 2
+Output: 2
+
+Example 2:
+
+Input: $M = -5, $N = 2
+Output: -2
+
+Example 3:
+
+Input: $M = -5, $N = -2
+Output: 2
+
+=cut
+use Test::More;
+
+sub floor_division
+{
+ my ($m, $n) = @_;
+ die "Division by 0$/" if 0 == $n;
+ return 0 if 0 == $m;
+
+ my $sign = 1;
+ if ( $m < 0 ) { $sign = 0 - $sign; }
+ if ( $n < 0 ) { $sign = 0 - $sign; }
+
+ $m = abs($m);
+ $n = abs($n);
+
+
+ my $counter = 0;
+ while ( $m > $n )
+ {
+
+ $m -= $n;
+ $counter++;
+ }
+ return 1 == $sign ? $counter : 0 - $counter;
+
+}
+
+is(floor_division( 5, 2), 2, ' 5 / 2');
+is(floor_division(-5, 2), -2, '-5 / 2');
+is(floor_division(-5, -2), 2, '-5 / -2');
+is(floor_division(10, 3), 3, '10 / 3');
+is(floor_division(17, -2), -8, '17 / -2');
+
+
+done_testing(); \ No newline at end of file
diff --git a/challenge-066/wanderdoc/perl/ch-2.pl b/challenge-066/wanderdoc/perl/ch-2.pl
new file mode 100644
index 0000000000..ace1ad5afa
--- /dev/null
+++ b/challenge-066/wanderdoc/perl/ch-2.pl
@@ -0,0 +1,52 @@
+#!perl
+use strict;
+use warnings FATAL => qw(all);
+
+=prompt
+You are given an integer $N.
+
+Write a script to check if the given number can be expressed as mn where m and n are positive integers. Otherwise print 0.
+Example 1:
+
+For given $N = 9, it should print 3^2.
+Example 2:
+
+For given $N = 45, it should print 0.
+=cut
+
+
+
+use Test::More;
+
+is(check_power(9), '3^2', '9 can');
+is(check_power(45), 0, '45 cannot');
+is(check_power(387420489), '3^18', '387420489 can');
+is(check_power(322687697779), '19^9', '322687697779 can');
+is(check_power(982451653), 0, '982451653 cannot');
+
+is(check_power(312079600999), '199^5', '312079600999 can');
+is(check_power(100000004987), 0, '100000004987 cannot');
+is(check_power(584318301411328), '22^11', '584318301411328 can');
+
+done_testing();
+sub check_power
+{
+ my $num = $_[0];
+ for my $i ( 2 .. sqrt($num) )
+ {
+
+ my $result = $i;
+
+
+ my $counter = 1;
+ while ( $result < $num )
+ {
+
+ $result *= $i;
+ $counter++;
+ return return $i . '^' . $counter if $result == $num;
+ }
+
+ }
+ return 0;
+} \ No newline at end of file