aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJörg Sommrey <28217714+jo-37@users.noreply.github.com>2022-07-06 19:10:22 +0200
committerJörg Sommrey <28217714+jo-37@users.noreply.github.com>2022-07-08 16:17:51 +0200
commitb92d69d7abf58f56575c389ddb8e4fe684af0e89 (patch)
tree6735db0cd1671a923496dfd2c9f03fc6e239a1ce
parent9096175e24a25bdf3fb98ae3508949ad9c5aa4b9 (diff)
downloadperlweeklychallenge-club-b92d69d7abf58f56575c389ddb8e4fe684af0e89.tar.gz
perlweeklychallenge-club-b92d69d7abf58f56575c389ddb8e4fe684af0e89.tar.bz2
perlweeklychallenge-club-b92d69d7abf58f56575c389ddb8e4fe684af0e89.zip
Solution to task 1
-rwxr-xr-xchallenge-172/jo-37/perl/ch-1.pl62
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;
+}