diff options
| author | Matthew Neleigh <matthew.neleigh@gmail.com> | 2021-10-30 12:24:36 -0400 |
|---|---|---|
| committer | Matthew Neleigh <matthew.neleigh@gmail.com> | 2021-10-30 12:24:36 -0400 |
| commit | 9c34496daf88ee295dc4e14d8afb9569560d632e (patch) | |
| tree | faeead3089a1d1e57a795b28636e178a0e59812c | |
| parent | 85e041ce62ebb1025717e5ad04a8681d043c3f08 (diff) | |
| download | perlweeklychallenge-club-9c34496daf88ee295dc4e14d8afb9569560d632e.tar.gz perlweeklychallenge-club-9c34496daf88ee295dc4e14d8afb9569560d632e.tar.bz2 perlweeklychallenge-club-9c34496daf88ee295dc4e14d8afb9569560d632e.zip | |
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); + } + +} + + + |
