aboutsummaryrefslogtreecommitdiff
path: root/challenge-066
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2020-06-28 01:47:25 +0100
committerGitHub <noreply@github.com>2020-06-28 01:47:25 +0100
commitc44aa86e7200b171d0608998a08bfad6ea3abe57 (patch)
treead4fe93e4a018f16f1dfc29713498d61f98278ef /challenge-066
parent66b696a384090ee69304c8f8e768704a08405a40 (diff)
parent00ed5b5a2ac61d3a593fdcc40f8246618f3b3611 (diff)
downloadperlweeklychallenge-club-c44aa86e7200b171d0608998a08bfad6ea3abe57.tar.gz
perlweeklychallenge-club-c44aa86e7200b171d0608998a08bfad6ea3abe57.tar.bz2
perlweeklychallenge-club-c44aa86e7200b171d0608998a08bfad6ea3abe57.zip
Merge pull request #1872 from choroba/ech066
Add solution to 066 by E. Choroba
Diffstat (limited to 'challenge-066')
-rwxr-xr-xchallenge-066/e-choroba/perl/ch-1.pl23
-rwxr-xr-xchallenge-066/e-choroba/perl/ch-2.pl22
2 files changed, 45 insertions, 0 deletions
diff --git a/challenge-066/e-choroba/perl/ch-1.pl b/challenge-066/e-choroba/perl/ch-1.pl
new file mode 100755
index 0000000000..dbb2ce6b60
--- /dev/null
+++ b/challenge-066/e-choroba/perl/ch-1.pl
@@ -0,0 +1,23 @@
+#! /usr/bin/perl
+use warnings;
+use strict;
+
+sub div {
+ my ($m, $n) = @_;
+ my $s = 1;
+ $s = -$s, $n = -$n if $n < 0;
+ $s = -$s, $n = -$n if $m < 0;
+ my $i = 0;
+ while (abs $m > abs $n) {
+ $m -= $n;
+ $i += $s;
+ }
+ --$i if $i < 0;
+ return $i
+}
+
+use Test::More tests => 4;
+is div( 5, 2), 2;
+is div(-5, 2), -3;
+is div( 5, -2), -3;
+is div(-5, -2), 2;
diff --git a/challenge-066/e-choroba/perl/ch-2.pl b/challenge-066/e-choroba/perl/ch-2.pl
new file mode 100755
index 0000000000..bd334c86c7
--- /dev/null
+++ b/challenge-066/e-choroba/perl/ch-2.pl
@@ -0,0 +1,22 @@
+#! /usr/bin/perl
+use warnings;
+use strict;
+
+#use Data::Dumper;
+
+sub power {
+ my ($N) = @_;
+ my @r;
+ my ($m, $n) = (2, 2);
+ while ($m < $N) {
+ push @r, "$m^$n" if $m ** $n == $N;
+ ++$m, $n = 2 if $m ** ++$n > $N;
+ }
+ return @r
+}
+
+use Test::More tests => 3;
+
+is_deeply [power(9)], ['3^2'];
+is power(45), 0;
+is_deeply [power(64)], [qw[ 2^6 4^3 8^2 ]];