diff options
| author | Noud Aldenhoven <noud.aldenhoven@gmail.com> | 2020-06-26 14:08:22 +0200 |
|---|---|---|
| committer | Noud Aldenhoven <noud.aldenhoven@gmail.com> | 2020-06-26 14:08:22 +0200 |
| commit | 27e0e5f1842def5ef2e198cbd0ba4f80a8d4e2e5 (patch) | |
| tree | b5e85da35cf720fc0a2235f604e67581b5e014fe /challenge-066/noud | |
| parent | 29760afc99fdd18bc3b44f073b6a4ef5ab720d11 (diff) | |
| download | perlweeklychallenge-club-27e0e5f1842def5ef2e198cbd0ba4f80a8d4e2e5.tar.gz perlweeklychallenge-club-27e0e5f1842def5ef2e198cbd0ba4f80a8d4e2e5.tar.bz2 perlweeklychallenge-club-27e0e5f1842def5ef2e198cbd0ba4f80a8d4e2e5.zip | |
Solution to challenge 066 task 1 and 2 in Raku by Noud
Diffstat (limited to 'challenge-066/noud')
| -rw-r--r-- | challenge-066/noud/raku/ch-1.p6 | 53 | ||||
| -rw-r--r-- | challenge-066/noud/raku/ch-2.p6 | 38 |
2 files changed, 91 insertions, 0 deletions
diff --git a/challenge-066/noud/raku/ch-1.p6 b/challenge-066/noud/raku/ch-1.p6 new file mode 100644 index 0000000000..e95f6d500f --- /dev/null +++ b/challenge-066/noud/raku/ch-1.p6 @@ -0,0 +1,53 @@ +# 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: -3 +# +# Example 3: +# +# Input: $M = -5, $N = -2 +# Output: 2 + +# multi tedious-div($m, $n) { +# if ($m > 0 && $n > 0) { +# return ($n, {$_ + $n} ... ^ * > $m); +# } elsif ($m < 0 && $n < 0) { +# return tedious-div(-$m, -$n); +# } elsif ($m > 0 && $n < 0) { +# return (-$n, {$_ - $n} ... ^ * > $m); +# } else { +# return tedious-div(-$m, -$n); +# } +# } + +sub tedious-div($m, $n) { + if (abs($m) < abs($n)) { + if ($m >= 0) { + return 0; + } else { + return -1; + } + } elsif ($m > 0 && $n > 0) { + return 1 + tedious-div($m - $n, $n); + } elsif ($m < 0 && $n > 0) { + return -1 + tedious-div($m + $n, $n); + } else { + return tedious-div(-$m, -$n); + } +} + +tedious-div(5, 2).say; +tedious-div(-5, 2).say; +tedious-div(-5, -2).say; +tedious-div(5, -2).say; diff --git a/challenge-066/noud/raku/ch-2.p6 b/challenge-066/noud/raku/ch-2.p6 new file mode 100644 index 0000000000..e603d55019 --- /dev/null +++ b/challenge-066/noud/raku/ch-2.p6 @@ -0,0 +1,38 @@ +# 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. +# +# Please make sure m > 1 and n > 1. +# +# BONUS: If there are more than one ways to express the given number then print +# all possible solutions. +# +# Example 1: +# +# For given $N = 9, it should print 32 or 3^2. +# Example 2: +# +# For given $N = 45, it should print 0. +# Example 3: +# +# For given $N = 64, it should print all or one of 8^2 or 2^6 or 4^3. + +sub power-int($n) { + my @r = ((2..$n**.5) X (2..$n**.5)).grep({ $_[0] ** $_[1] eq $n}); + + if (@r.elems == 0) { + say 0; + } else { + say "$_[0]^$_[1]" for @r; + } +} + +say "Example 1, N=9:"; +power-int(9); + +say "Example 2, N=45:"; +power-int(45); + +say "Example 3, N=64:"; +power-int(64); |
