aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Campbell Smith <pj.campbell.smith@gmail.com>2022-06-21 10:50:41 +0100
committerPeter Campbell Smith <pj.campbell.smith@gmail.com>2022-06-21 10:50:41 +0100
commit69a6caba38097779d51e4b9ef5545e331d232300 (patch)
tree5a68004e6041f05ce9f7e4f23c745158b15c2484
parent81cfb6211946c15f45696f3102a57d3aede7a4c4 (diff)
downloadperlweeklychallenge-club-69a6caba38097779d51e4b9ef5545e331d232300.tar.gz
perlweeklychallenge-club-69a6caba38097779d51e4b9ef5545e331d232300.tar.bz2
perlweeklychallenge-club-69a6caba38097779d51e4b9ef5545e331d232300.zip
Week 170 submission
-rw-r--r--challenge-170/peter-campbell-smith/blog.txt1
-rwxr-xr-xchallenge-170/peter-campbell-smith/perl/ch-1.pl34
-rwxr-xr-xchallenge-170/peter-campbell-smith/perl/ch-2.pl112
3 files changed, 147 insertions, 0 deletions
diff --git a/challenge-170/peter-campbell-smith/blog.txt b/challenge-170/peter-campbell-smith/blog.txt
new file mode 100644
index 0000000000..05a72311e8
--- /dev/null
+++ b/challenge-170/peter-campbell-smith/blog.txt
@@ -0,0 +1 @@
+https://pjcs-pwc.blogspot.com/2022/06/primorials-and-kronecker-products.html
diff --git a/challenge-170/peter-campbell-smith/perl/ch-1.pl b/challenge-170/peter-campbell-smith/perl/ch-1.pl
new file mode 100755
index 0000000000..b9f51e8195
--- /dev/null
+++ b/challenge-170/peter-campbell-smith/perl/ch-1.pl
@@ -0,0 +1,34 @@
+#!/usr/bin/perl
+
+# Peter Campbell Smith - 2022-06-20
+# PWC 170 task 1
+
+use v5.28;
+use strict;
+use warnings;
+use utf8;
+
+# Write a script to generate first 10 Primorial Numbers.
+# Primorial numbers are those formed by multiplying successive prime numbers.
+
+# Blog: https://pjcs-pwc.blogspot.com/2022/06/primorials-and-kronecker-products.html
+
+my (@primes, $j, $p, @pm, $k);
+
+@primes = ();
+$p = 1;
+$pm[0] = 1;
+say qq[P(0) = 1];
+
+OUTER: for $j (2 .. 99999) {
+ for $k (@primes) {
+ next OUTER if $j % $k == 0; # $j is not prime if divisible by a smaller prime
+ }
+
+ # found a prime, create next primorial
+ push @primes, $j;
+ $pm[$p] = $pm[$p - 1] * $j;
+ say qq[P($p) = $pm[$p]];
+ $p ++;
+ exit if $p == 10;
+}
diff --git a/challenge-170/peter-campbell-smith/perl/ch-2.pl b/challenge-170/peter-campbell-smith/perl/ch-2.pl
new file mode 100755
index 0000000000..61f9824e83
--- /dev/null
+++ b/challenge-170/peter-campbell-smith/perl/ch-2.pl
@@ -0,0 +1,112 @@
+#!/usr/bin/perl
+
+# Peter Campbell Smith - 2022-06-20
+# PWC 170 task 2
+
+use v5.28;
+use strict;
+use warnings;
+use utf8;
+
+# You are given 2 matrices.
+# Write a script to generate the Kronecker Product of the given 2 matrices.
+
+# Blog: https://pjcs-pwc.blogspot.com/2022/06/primorials-and-kronecker-products.html
+
+my ($test, $A, $B, $K, $m, $n, $p, $q, $i, $j, $i_quo_p, $i_rem_p, $j_quo_q, $j_rem_q);
+
+for $test (1, 2) {
+
+ if ($test == 1) { # Mohammad's example
+
+ $A = [[ 1, 2 ],
+ [ 3, 4 ]];
+
+ $B = [[ 5, 6 ],
+ [ 7, 8 ]];
+
+ } else { # Wikipedia's 2nd example
+
+ $A = [[ 1, -4, 7],
+ [-2, 3, 3]];
+
+ $B = [[8, -9, -6, 5],
+ [1, -3, -4, 7],
+ [2, 8, -8, -3],
+ [1, 2, -5, -1]];
+ }
+
+ # display input
+ show('A', $A);
+ show('B', $B);
+
+ $m = scalar(@$A); # no of rows in A (0 to $m - 1)
+ $n = scalar(@{$A->[0]}); # no of columns in A
+ $p = scalar(@$B); # no of rows in B
+ $q = scalar(@{$B->[0]}); # no of columns in B
+
+ # loop over the rows and cols of product matrix $K ($m * $p rows, $n * $q cols)
+ for $i (0 .. $m * $p - 1) {
+ for $j (0 .. $n * $q - 1) {
+
+ # get the quotient and remainder on dividing i by p and j by q
+ ($i_quo_p, $i_rem_p) = quorem($i, $p);
+ ($j_quo_q, $j_rem_q) = quorem($j, $q);
+
+ # Wikipedia formula for element i, j of K
+ $K->[$i]->[$j] = $A->[$i_quo_p]->[$j_quo_q] * $B->[$i_rem_p]->[$j_rem_q];
+ }
+ }
+
+ # display the result
+ show('A x B', $K);
+ print qq[-----\n\n];
+}
+
+sub quorem {
+
+ my ($dividend, $divisor, $quotient, $remainder);
+
+ # returns the integer quotient and remainder from dividend / divisor (both integers > 0)
+ $dividend = shift;
+ $divisor = shift;
+
+ $quotient = int($dividend / $divisor);
+ $remainder = $dividend - $quotient * $divisor;
+
+ return ($quotient, $remainder);
+}
+
+sub show {
+
+ my ($caption, $M, $margin, $i, $j, $this_width, $cell_width);
+
+ # pretty print matrix
+ ($caption, $M) = @_;
+
+ $caption .= ' = ';
+ $margin = length($caption);
+
+ # first pass to get width of cells
+ $cell_width = 0;
+ for $i (0 .. scalar(@$M) - 1) {
+ for $j (0 .. scalar(@{$M->[0]}) - 1) {
+ $this_width = length($M->[$i]->[$j]);
+ $cell_width = $this_width if $this_width > $cell_width;
+ }
+ }
+ $cell_width += 1;
+
+ # second pass to print out - loop over rows
+ for $i (0 .. scalar(@$M) - 1) {
+ print qq{$caption\[}; # first line
+ $caption = ' ' x $margin; # subsequent lines
+
+ # and over columns, padding each value to $cell_width
+ for $j (0 .. scalar(@{$M->[0]}) - 1) {
+ printf("%${cell_width}s", $M->[$i]->[$j]);
+ }
+ print qq[ \]\n];
+ }
+ print qq[\n];
+}