diff options
| -rwxr-xr-x | challenge-170/jo-37/perl/ch-1.pl | 24 | ||||
| -rwxr-xr-x | challenge-170/jo-37/perl/ch-2.pl | 70 |
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; +} |
