diff options
| author | Adam Russell <ac.russell@live.com> | 2020-10-18 00:58:16 -0400 |
|---|---|---|
| committer | Adam Russell <ac.russell@live.com> | 2020-10-18 00:58:16 -0400 |
| commit | b68a75a9e68e7e13b22ab5257f889a2bc025216b (patch) | |
| tree | e8496062f569cfb1856295ceadfdf3e3f9e5662a /challenge-082 | |
| parent | 2e7fb5ec844f60c121ef26d50e6b7f24b8849780 (diff) | |
| download | perlweeklychallenge-club-b68a75a9e68e7e13b22ab5257f889a2bc025216b.tar.gz perlweeklychallenge-club-b68a75a9e68e7e13b22ab5257f889a2bc025216b.tar.bz2 perlweeklychallenge-club-b68a75a9e68e7e13b22ab5257f889a2bc025216b.zip | |
perl solution for part 1 of challenge-082
Diffstat (limited to 'challenge-082')
| -rw-r--r-- | challenge-082/adam-russell/perl/ch-1.pl | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/challenge-082/adam-russell/perl/ch-1.pl b/challenge-082/adam-russell/perl/ch-1.pl new file mode 100644 index 0000000000..a9bdce7ab6 --- /dev/null +++ b/challenge-082/adam-russell/perl/ch-1.pl @@ -0,0 +1,30 @@ +use strict; +use warnings; +## +# You are given 2 positive numbers $M and $N. +# Write a script to list all common factors of the given numbers. +## +sub factor{ + my($n) = @_; + my @factors = (1); + foreach my $j (2..sqrt($n)){ + push @factors, $j if $n % $j == 0; + push @factors, ($n / $j) if $n % $j == 0 && $j ** 2 != $n; + } + return @factors; +} + +sub common_factors{ + my($m, $n) = @_; + my @common_factors = grep { my $f = $_; grep { $f == $_ } @{$n}} @{$m}; + return @common_factors; +} + + +MAIN:{ + my $M = 12; + my $N = 18; + my @m_factors = factor($M); + my @n_factors = factor($N); + print "(" . join(",", common_factors(\@m_factors, \@n_factors)) . ")\n"; +}
\ No newline at end of file |
