aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJörg Sommrey <28217714+jo-37@users.noreply.github.com>2022-06-24 17:59:10 +0200
committerJörg Sommrey <28217714+jo-37@users.noreply.github.com>2022-06-24 17:59:10 +0200
commitfc5b1768f1f26f6e5400791bd6428685cfcd35f9 (patch)
tree89211003d7150313490d41d6dbfae198ee673714
parent247e5a8ccbf2ca49929f435a6efc5e7f9c6e4e36 (diff)
parentf41734a94c092dd850bbac5efcbbcc95d48dc902 (diff)
downloadperlweeklychallenge-club-fc5b1768f1f26f6e5400791bd6428685cfcd35f9.tar.gz
perlweeklychallenge-club-fc5b1768f1f26f6e5400791bd6428685cfcd35f9.tar.bz2
perlweeklychallenge-club-fc5b1768f1f26f6e5400791bd6428685cfcd35f9.zip
Solutions to challenge 170
-rwxr-xr-xchallenge-170/jo-37/perl/ch-1.pl24
-rwxr-xr-xchallenge-170/jo-37/perl/ch-2.pl70
2 files changed, 94 insertions, 0 deletions
diff --git a/challenge-170/jo-37/perl/ch-1.pl b/challenge-170/jo-37/perl/ch-1.pl
new file mode 100755
index 0000000000..db3b1bbd91
--- /dev/null
+++ b/challenge-170/jo-37/perl/ch-1.pl
@@ -0,0 +1,24 @@
+#!/usr/bin/perl -s
+
+use v5.16;
+use warnings;
+use Math::Prime::Util 'prime_iterator';
+use List::Util 'reductions';
+
+die <<EOS unless @ARGV;
+usage: $0 N
+
+N
+ Print the first N primorials.
+
+EOS
+
+
+# Math::Prime::Util has 'primorial', but as the *sequence* of the first
+# N primorials is requested in this task, another approach seems to be
+# more useful: Build the sequence of the first N-1 primes' product.
+
+main: {
+ my $pi = prime_iterator();
+ say for reductions {$a * $b} 1, map $pi->(), 2 .. shift;
+}
diff --git a/challenge-170/jo-37/perl/ch-2.pl b/challenge-170/jo-37/perl/ch-2.pl
new file mode 100755
index 0000000000..9c4e1b135f
--- /dev/null
+++ b/challenge-170/jo-37/perl/ch-2.pl
@@ -0,0 +1,70 @@
+#!/usr/bin/perl -s
+
+use v5.16;
+use warnings;
+use PDL;
+use experimental 'signatures';
+
+our ($examples, $verbose);
+
+run_tests() if $examples; # does not return
+
+die <<EOS unless @ARGV;
+usage: $0 [-examples] [-verbose] [--] [A B]
+
+-examples
+ run some examples
+
+-verbose
+ Print intermediate piddles.
+
+A B
+ Two matrices given in any format accepted by the PDL constructor, e.g.
+ '[1 2] [3 4]' or '1,2;3,4'
+
+EOS
+
+
+### Input and Output
+
+say kronecker(pdl($ARGV[0]), pdl($ARGV[1]), $verbose);
+
+
+### Implementation
+
+# PDL::Matrix has 'kroneckerproduct' but its usage would not qualify as
+# 'implementing Kronecker product'.
+# Therefore taking a different approach:
+# - replicate every element of A in the shape of B
+# - replicate the whole matrix B in the shape of A.
+# - the element-wise product of both replicated matrices results in a
+# "true" 4-dimensional Kronecker product.
+# - "flattening" two dimensions gives the usual matrix result.
+# The potential one-liner is split into four steps to be able to show
+# the intermediate piddles.
+
+sub kronecker ($a, $b, $verbose) {
+ say "a: $a" if $verbose;
+ my $ar = $a->dummy(0, $b->dim(0))->dummy(1, $b->dim(1));
+ say "ar: $ar" if $verbose;
+
+ say "b: $b" if $verbose;
+ my $br = $b->dummy(2, $a->dim(0))->dummy(3, $a->dim(1));
+ say "br: $br" if $verbose;
+
+ my $k = $ar * $br;
+ say "k: $k" if $verbose;
+
+ $k->clump(0, 2)->clump(1, 2);
+}
+
+
+### Examples and tests
+
+sub run_tests {
+
+ say kronecker(1 + sequence(long, 2, 2), 5 + sequence(long, 2, 2), 1);
+ say kronecker(1 + sequence(long, 2, 3), 7 + sequence(long, 3, 5), 1);
+
+ exit;
+}