aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2020-10-18 23:04:18 +0100
committerGitHub <noreply@github.com>2020-10-18 23:04:18 +0100
commit587fa065372b773b7f386e8f852912bea89ca371 (patch)
treeddc66e02fa2ae3ee82e7aed4b232b8b202ef2d35
parent7e13e7b7166b03ad65876881e3ea5cf64bbb1708 (diff)
parente5f2f218f4c755b1f411fe9fbc9e1aa11131f549 (diff)
downloadperlweeklychallenge-club-587fa065372b773b7f386e8f852912bea89ca371.tar.gz
perlweeklychallenge-club-587fa065372b773b7f386e8f852912bea89ca371.tar.bz2
perlweeklychallenge-club-587fa065372b773b7f386e8f852912bea89ca371.zip
Merge pull request #2555 from wanderdoc/master
Solution to task #1 challenge-082.
-rw-r--r--challenge-082/wanderdoc/perl/ch-1.pl48
1 files changed, 48 insertions, 0 deletions
diff --git a/challenge-082/wanderdoc/perl/ch-1.pl b/challenge-082/wanderdoc/perl/ch-1.pl
new file mode 100644
index 0000000000..91c75271bf
--- /dev/null
+++ b/challenge-082/wanderdoc/perl/ch-1.pl
@@ -0,0 +1,48 @@
+#!perl
+use strict;
+use warnings FATAL => qw(all);
+
+=prompt
+You are given 2 positive numbers $M and $N. Write a script to list all common factors of the given numbers.
+Example 1: Input: $M = 12 $N = 18 Output: (1, 2, 3, 6)
+Example 2: Input: $M = 18 $N = 23 Output: (1)
+=cut
+
+
+
+
+
+
+
+print join(', ', common_factors(12, 18)), $/;
+print join(', ', common_factors(18, 23)), $/;
+print join(', ', common_factors(1810, 2020)), $/;
+sub common_factors
+{
+ my ($num_1, $num_2) = @_;
+ my $gcd = gcd($num_1, $num_2);
+ my @factors;
+
+
+ for my $i ( 1 .. $gcd )
+ {
+ push @factors, $i unless $gcd % $i;
+ }
+ return @factors;
+}
+
+
+sub gcd
+{
+
+ my ($num_1, $num_2) = @_;
+
+
+ while ( $num_1 != $num_2 )
+ {
+ ($num_1, $num_2) = ($num_2, $num_1) if ($num_2 > $num_1);
+
+ ($num_1, $num_2) = ($num_1 - $num_2, $num_2);
+ }
+ return $num_2;
+} \ No newline at end of file