diff options
| -rwxr-xr-x | challenge-082/alexander-pankoff/perl/ch-1.pl | 46 |
1 files changed, 30 insertions, 16 deletions
diff --git a/challenge-082/alexander-pankoff/perl/ch-1.pl b/challenge-082/alexander-pankoff/perl/ch-1.pl index ef11c95c46..1b9916d7b5 100755 --- a/challenge-082/alexander-pankoff/perl/ch-1.pl +++ b/challenge-082/alexander-pankoff/perl/ch-1.pl @@ -12,31 +12,22 @@ 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", + -message => "$0: Expects 2 natural numbers", + -exitval => 1, ) 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 ) { + + # we grep for numbers from 1 to min($m,$n) that are factors of both $m and + # $n. since all numbers larger than min($m,$n) can't be a factor of that + # minimum we don't have to check them grep { my $check_factor = $_; all { is_factor( $check_factor, $_ ) } ( $m, $n ); @@ -51,3 +42,26 @@ sub format_list(@list) { return '(' . join( ', ', @list ) . ')'; } +=pod + +=head1 NAME + +wk-082 ch-1 - Common Factors + +=head1 SYNOPSIS + +Prints the common factors of two given natural numbers M and N + +ch-1.pl <M> <N> + +=head1 ARGUMENTS + +=over 8 + +=item B<N> The first natural number + +=item B<M> The second natural number + +=back + +=cut |
