diff options
| -rwxr-xr-x | challenge-172/jo-37/perl/ch-1.pl | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/challenge-172/jo-37/perl/ch-1.pl b/challenge-172/jo-37/perl/ch-1.pl new file mode 100755 index 0000000000..328e1e36d5 --- /dev/null +++ b/challenge-172/jo-37/perl/ch-1.pl @@ -0,0 +1,62 @@ +#!/usr/bin/perl -s + +use v5.16; +use Test2::V0; +use Math::Prime::Util 'forpart'; +use List::Util 'uniqint'; +use experimental 'signatures'; + +our $examples; + +run_tests() if $examples; # does not return + +die <<EOS unless @ARGV == 2; +usage: $0 [-examples] [M N] + +-examples + run the examples from the challenge + +M N + Find all partitions of M into N prime parts having no duplicates. + +EOS + + +### Input and Output + +say "@$_" for prime_part(@ARGV); + + +### Implementation + +sub prime_part ($m, $n) { + my @part; + + # Find all prime partitions and filter out those containing + # duplicates. + forpart {push @part, [@_] if @_ == uniqint @_} $m, {n => $n, prime => 1}; + + wantarray ? @part : \@part; +} + + +### Examples and tests + +sub run_tests { + + is scalar(prime_part(18, 2)), + bag { + item bag {item 5; item 13; end}; + item bag {item 7; item 11; end}; + end; + }, 'example 1'; + + is scalar(prime_part(19, 3)), + bag { + item bag {item 3; item 5; item 11; end}; + end; + }, 'example 2'; + + done_testing; + exit; +} |
