File tree 1 file changed +30
-16
lines changed
challenge-082/alexander-pankoff/perl
1 file changed +30
-16
lines changed Original file line number Diff line number Diff line change 12
12
use List::Util qw( min all any) ;
13
13
use Scalar::Util qw( looks_like_number) ;
14
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
15
pod2usage(
28
- -message => " $0 : Expects 2 postive numbers" ,
29
- -exitval => 1,
30
- -verbose => 99,
31
- -sections => " USAGE|SYNOPSIS" ,
16
+ -message => " $0 : Expects 2 natural numbers" ,
17
+ -exitval => 1,
32
18
)
33
19
if @ARGV != 2
34
20
or any { !looks_like_number($_ ) || $_ < 1 } @ARGV ;
35
21
36
22
my ( $M , $N ) = @ARGV ;
23
+
37
24
say format_list( common_factors( $M , $N ) );
38
25
39
26
sub common_factors ( $m , $n ) {
27
+
28
+ # we grep for numbers from 1 to min($m,$n) that are factors of both $m and
29
+ # $n. since all numbers larger than min($m,$n) can't be a factor of that
30
+ # minimum we don't have to check them
40
31
grep {
41
32
my $check_factor = $_ ;
42
33
all { is_factor( $check_factor , $_ ) } ( $m , $n );
@@ -51,3 +42,26 @@ (@list)
51
42
return ' (' . join ( ' , ' , @list ) . ' )' ;
52
43
}
53
44
45
+ =pod
46
+
47
+ =head1 NAME
48
+
49
+ wk-082 ch-1 - Common Factors
50
+
51
+ =head1 SYNOPSIS
52
+
53
+ Prints the common factors of two given natural numbers M and N
54
+
55
+ ch-1.pl <M> <N>
56
+
57
+ =head1 ARGUMENTS
58
+
59
+ =over 8
60
+
61
+ =item B<N > The first natural number
62
+
63
+ =item B<M > The second natural number
64
+
65
+ =back
66
+
67
+ =cut
You can’t perform that action at this time.
0 commit comments