Skip to content

Commit 1f3aee3

Browse files
committed
add perl solution for wk-081 ch-1
1 parent 2e7fb5e commit 1f3aee3

File tree

1 file changed

+53
-0
lines changed
  • challenge-082/alexander-pankoff/perl

1 file changed

+53
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#!/usr/bin/env perl
2+
use v5.20;
3+
use utf8;
4+
use strict;
5+
use warnings;
6+
use autodie;
7+
use feature qw(say signatures);
8+
no warnings 'experimental::signatures';
9+
10+
use Pod::Usage;
11+
12+
use List::Util qw(min all any);
13+
use Scalar::Util qw(looks_like_number);
14+
15+
=pod
16+
17+
=head1 SYNOPSIS
18+
19+
This Script will print a list of common factors from M and N
20+
21+
=head1 USAGE
22+
23+
ch-1.pl <M> <N>
24+
25+
=cut
26+
27+
pod2usage(
28+
-message => "$0: Expects 2 postive numbers",
29+
-exitval => 1,
30+
-verbose => 99,
31+
-sections => "USAGE|SYNOPSIS",
32+
)
33+
if @ARGV != 2
34+
or any { !looks_like_number($_) || $_ < 1 } @ARGV;
35+
36+
my ( $M, $N ) = @ARGV;
37+
say format_list( common_factors( $M, $N ) );
38+
39+
sub common_factors ( $m, $n ) {
40+
grep {
41+
my $check_factor = $_;
42+
all { is_factor( $check_factor, $_ ) } ( $m, $n );
43+
} 1 .. min( $m, $n );
44+
}
45+
46+
sub is_factor ( $divisor, $value ) {
47+
return $value % $divisor == 0;
48+
}
49+
50+
sub format_list(@list) {
51+
return '(' . join( ', ', @list ) . ')';
52+
}
53+

0 commit comments

Comments
 (0)