aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexander Pankoff <ccntrq@screenri.de>2020-10-12 11:50:09 +0200
committerAlexander Pankoff <ccntrq@screenri.de>2020-10-12 11:50:09 +0200
commit1f3aee3539964c567010c11f189ddba4cbb873cc (patch)
treebae76e01b0404d3e12ec1d0f1dd4c4b6b4c9058a
parent2e7fb5ec844f60c121ef26d50e6b7f24b8849780 (diff)
downloadperlweeklychallenge-club-1f3aee3539964c567010c11f189ddba4cbb873cc.tar.gz
perlweeklychallenge-club-1f3aee3539964c567010c11f189ddba4cbb873cc.tar.bz2
perlweeklychallenge-club-1f3aee3539964c567010c11f189ddba4cbb873cc.zip
add perl solution for wk-081 ch-1
-rwxr-xr-xchallenge-082/alexander-pankoff/perl/ch-1.pl53
1 files changed, 53 insertions, 0 deletions
diff --git a/challenge-082/alexander-pankoff/perl/ch-1.pl b/challenge-082/alexander-pankoff/perl/ch-1.pl
new file mode 100755
index 0000000000..ef11c95c46
--- /dev/null
+++ b/challenge-082/alexander-pankoff/perl/ch-1.pl
@@ -0,0 +1,53 @@
+#!/usr/bin/env perl
+use v5.20;
+use utf8;
+use strict;
+use warnings;
+use autodie;
+use feature qw(say signatures);
+no warnings 'experimental::signatures';
+
+use Pod::Usage;
+
+use List::Util qw(min all any);
+use Scalar::Util qw(looks_like_number);
+
+=pod
+
+=head1 SYNOPSIS
+
+This Script will print a list of common factors from M and N
+
+=head1 USAGE
+
+ch-1.pl <M> <N>
+
+=cut
+
+pod2usage(
+ -message => "$0: Expects 2 postive numbers",
+ -exitval => 1,
+ -verbose => 99,
+ -sections => "USAGE|SYNOPSIS",
+ )
+ if @ARGV != 2
+ or any { !looks_like_number($_) || $_ < 1 } @ARGV;
+
+my ( $M, $N ) = @ARGV;
+say format_list( common_factors( $M, $N ) );
+
+sub common_factors ( $m, $n ) {
+ grep {
+ my $check_factor = $_;
+ all { is_factor( $check_factor, $_ ) } ( $m, $n );
+ } 1 .. min( $m, $n );
+}
+
+sub is_factor ( $divisor, $value ) {
+ return $value % $divisor == 0;
+}
+
+sub format_list(@list) {
+ return '(' . join( ', ', @list ) . ')';
+}
+