diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-06-28 19:35:04 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-06-28 19:35:04 +0100 |
| commit | 4f9aff95da68549e0011f52d4c37eac2db848d22 (patch) | |
| tree | 41f897dfb64eeee553905fcbde2d4908fc6448af /challenge-066 | |
| parent | 4197e4b360a47932840f6569f5d3f36773cbbc52 (diff) | |
| parent | e46864ec14861a972fc114f70a9f15ced9f5731d (diff) | |
| download | perlweeklychallenge-club-4f9aff95da68549e0011f52d4c37eac2db848d22.tar.gz perlweeklychallenge-club-4f9aff95da68549e0011f52d4c37eac2db848d22.tar.bz2 perlweeklychallenge-club-4f9aff95da68549e0011f52d4c37eac2db848d22.zip | |
Merge pull request #1875 from waltman/branch-for-challenge-066
Branch for challenge 066
Diffstat (limited to 'challenge-066')
| -rw-r--r-- | challenge-066/walt-mankowski/perl/ch-1.pl | 55 | ||||
| -rw-r--r-- | challenge-066/walt-mankowski/perl/ch-2.pl | 50 |
2 files changed, 105 insertions, 0 deletions
diff --git a/challenge-066/walt-mankowski/perl/ch-1.pl b/challenge-066/walt-mankowski/perl/ch-1.pl new file mode 100644 index 0000000000..2bb4f028ce --- /dev/null +++ b/challenge-066/walt-mankowski/perl/ch-1.pl @@ -0,0 +1,55 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use feature qw(:5.32); +use experimental qw(signatures); + +# TASK #1 › Divide Integers +# Submitted by: Mohammad S Anwar +# +# 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 + +my ($m, $n) = @ARGV; + +say divide($m, $n); + +sub divide($m, $n) { + # handle 0 values first + return "undefined" if $n == 0; + return 0 if $m == 0; + + # sign of the result + my $sign = ($m < 0 && $n > 0) || ($m > 0 && $n < 0) ? -1 : 1; + ($m, $n) = map {abs} ($m, $n); + my $cnt = 0; + while ($m >= $n) { + $m -= $n; + $cnt++; + } + + if ($sign == -1 && $m > 0) { + # floor is 1 below cnt when negative and there's a remainder + return -($cnt+1); + } else { + return $cnt * $sign; + } +} diff --git a/challenge-066/walt-mankowski/perl/ch-2.pl b/challenge-066/walt-mankowski/perl/ch-2.pl new file mode 100644 index 0000000000..07e46a478a --- /dev/null +++ b/challenge-066/walt-mankowski/perl/ch-2.pl @@ -0,0 +1,50 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use feature qw(:5.32); +use experimental qw(signatures); + +# TASK #2 › Power Integers +# Submitted by: Mohammad S Anwar +# +# You are given an integer $N. +# +# Write a script to check if the given number can be expressed as m^n +# 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 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. + +# My algorithm is this: +# Loop over all the m's from 2 to sqrt(N) +# If m can be raised to an integer power to equal N, then log_m N is an integer + +my $N = shift @ARGV; +my $logn = log($N); +my $found = 0; +for my $m (2..sqrt($N)) { + my $n = $logn / log($m); + if (is_int($n)) { + say "$m^$n"; + $found = 1; + } +} + +say 0 unless $found; + +sub is_int($n) { + my $EPS = 1e-10; + my $round = sprintf "%.0f", $n; + return abs($round - $n) < $EPS; +} |
