diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-10-31 21:55:14 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-10-31 21:55:14 +0000 |
| commit | 81f01dd5b47d7209c87eb6ffeae9aad2908c3e35 (patch) | |
| tree | 95d483f6b77b1959c06069f90506421afd364201 | |
| parent | 6392f855254b7190479138cb26f375e5605b2a44 (diff) | |
| parent | 9c34496daf88ee295dc4e14d8afb9569560d632e (diff) | |
| download | perlweeklychallenge-club-81f01dd5b47d7209c87eb6ffeae9aad2908c3e35.tar.gz perlweeklychallenge-club-81f01dd5b47d7209c87eb6ffeae9aad2908c3e35.tar.bz2 perlweeklychallenge-club-81f01dd5b47d7209c87eb6ffeae9aad2908c3e35.zip | |
Merge pull request #5129 from mattneleigh/pwc136
new file: challenge-136/mattneleigh/perl/ch-1.pl
| -rwxr-xr-x | challenge-136/mattneleigh/perl/ch-1.pl | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/challenge-136/mattneleigh/perl/ch-1.pl b/challenge-136/mattneleigh/perl/ch-1.pl new file mode 100755 index 0000000000..31a755832a --- /dev/null +++ b/challenge-136/mattneleigh/perl/ch-1.pl @@ -0,0 +1,84 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use English; + +################################################################################ +# Begin main execution +################################################################################ + +my @pairs = ( + [ 8, 24 ], + [ 26, 39 ], + [ 4, 10 ] +); +my $pair; + +foreach $pair (@pairs){ + print(" Input: \$m = $pair->[0], \$n = $pair->[1]\n"); + print(" Output: ", two_friendly(@{$pair}), "\n\n\n"); +} + +exit(0); +################################################################################ +# End main execution; subroutines follow +################################################################################ + + + +################################################################################ +# Determine of two integers are Two Friendly (their Greatest Common Divisor is +# a Power of Two) +# Takes two arguments: +# * An integer M +# * An integer N +# Returns: +# * 1 if M and N are Two Friendly +# * 0 if M and N are NOT Two Friendly +################################################################################ +sub two_friendly{ + my $m = int(shift()); + my $n = int(shift()); + + my $power_two; + + # Compute the power of two of the greatest + # common divisor + $power_two = log(gcd($m, $n)) / log(2); + + # If $power_two looks like an integer + # (accounting for round-off error...) then + # the GCD of $m and $n was a power of two + if(abs($power_two - int($power_two)) < 0.0000000001){ + return(1); + } else{ + return(0); + } + +} + + + +################################################################################ +# Compute the Greatest Common Denominator (GCD) of two integers +# Takes two arguments: +# * An integer A +# * An integer B +# Returns: +# * The GCD of A and B +################################################################################ +sub gcd{ + my $a = shift(); + my $b = shift(); + + if($b){ + return(gcd($b, $a % $b)); + } else{ + return($a); + } + +} + + + |
