aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStephen Lynn <bizlsg@localhost.localdomain>2022-06-07 22:08:17 +0800
committerStephen Lynn <bizlsg@localhost.localdomain>2022-06-07 22:08:17 +0800
commit22fc5dcff65cb0cc13d002172f710c7246e0de4a (patch)
tree1eed6d4f2c43f1e3f8f259a61eac213868e0737d
parentad6edd6a47450455cf81a94854ce74eabee2501a (diff)
downloadperlweeklychallenge-club-22fc5dcff65cb0cc13d002172f710c7246e0de4a.tar.gz
perlweeklychallenge-club-22fc5dcff65cb0cc13d002172f710c7246e0de4a.tar.bz2
perlweeklychallenge-club-22fc5dcff65cb0cc13d002172f710c7246e0de4a.zip
challenge 1
-rw-r--r--challenge-168/steve-g-lynn/README1
-rwxr-xr-xchallenge-168/steve-g-lynn/perl/ch-1.pl59
-rwxr-xr-xchallenge-168/steve-g-lynn/raku/ch-1.p645
3 files changed, 105 insertions, 0 deletions
diff --git a/challenge-168/steve-g-lynn/README b/challenge-168/steve-g-lynn/README
new file mode 100644
index 0000000000..7c58cc8d8c
--- /dev/null
+++ b/challenge-168/steve-g-lynn/README
@@ -0,0 +1 @@
+Solution by Steve G Lynn
diff --git a/challenge-168/steve-g-lynn/perl/ch-1.pl b/challenge-168/steve-g-lynn/perl/ch-1.pl
new file mode 100755
index 0000000000..a8729418aa
--- /dev/null
+++ b/challenge-168/steve-g-lynn/perl/ch-1.pl
@@ -0,0 +1,59 @@
+#!/usr/bin/perl
+
+use Math::Prime::XS qw(is_prime);
+
+local %saveprimes=();
+for (0..150) {
+ local $chk=&perrin($_);
+ (is_prime($chk)) && ($saveprimes{$chk}=1);
+ @saveprimes==26 && last;
+}
+
+print "( ";
+foreach (sort{$a<=>$b} keys %saveprimes){
+ print "$_ ";
+}
+print ")\n";
+
+#-- subs
+
+
+sub perrin {
+ local ($n)=@_;
+
+ if ($n==0) { return 3 }
+ else { return &postmult_302(&matpow($n)) }
+}
+
+#-- subs for fast computation of perrin number using matrix formula
+#-- see wikipedia https://en.wikipedia.org/wiki/Perrin_number
+
+#-- [0 1 0; 0 0 1; 1 1 0]^n (only need 1st row)
+sub matpow {
+ local ($n)=@_;
+
+ if ($n==1) { return (0,1,0) }
+ else {return &postmult_010_001_110(&matpow($n-1))}
+}
+
+
+# 3x3 matrix * [0 1 0; 0 0 1; 1 1 0] (retain 1st row of product)
+sub postmult_010_001_110 {
+ # [a11 a12 a13; a21 a22 a23; a31 a32 a33]*[0 1 0; 0 0 1; 1 1 0]
+ local ($a11,$a12,$a13)=@_; #just need 1st row
+
+ local $b11=$a13;
+ local $b12=$a11+$a13;
+ local $b13=$a12;
+
+ return ($b11,$b12,$b13) #-- return just 1st row
+}
+
+# 3x3 matrix * [3;0;2] retain 1st element of product
+sub postmult_302 {
+ local ($a11,$a12,$a13)=@_; #-- just need 1st row
+ # [a11 a12 a13; a21 a22 a23; a31 a32 a33]*[3;0;2]
+ #
+ return $a11*3+$a13*2; #-- 1st element
+
+}
diff --git a/challenge-168/steve-g-lynn/raku/ch-1.p6 b/challenge-168/steve-g-lynn/raku/ch-1.p6
new file mode 100755
index 0000000000..ace5c647e1
--- /dev/null
+++ b/challenge-168/steve-g-lynn/raku/ch-1.p6
@@ -0,0 +1,45 @@
+#!/usr/bin/raku
+
+my %saveprimes=();
+for ^Inf {
+ my $chk=perrin($_);
+ (is-prime($chk)) && (%saveprimes{$chk}=1);
+ %saveprimes.elems==13 && last;
+}
+
+say %saveprimes.keys.sort({.Int});
+
+
+#-- subs
+
+multi sub perrin(0) {3}
+multi sub perrin(Int $n where ($n>0)){postmult_302(matpow($n))}
+
+#-- subs for fast computation of perrin number using matrix formula
+#-- see wikipedia https://en.wikipedia.org/wiki/Perrin_number
+
+#[0 1 0; 0 0 1; 1 1 0]^n (retain 1st row)
+multi sub matpow(1){ (0,1,0) }
+multi sub matpow(Int $n where ($n>1)) { postmult_010_001_110 (matpow($n-1)) }
+
+# 3x3 matrix * [0 1 0; 0 0 1; 1 1 0] (retain 1st row of product)
+sub postmult_010_001_110 (*@inmatrix){
+ # [a11 a12 a13; a21 a22 a23; a31 a32 a33]*[0 1 0; 0 0 1; 1 1 0]
+ my ($a11,$a12,$a13)=@inmatrix; #just need 1st row
+
+ my $b11=$a13;
+ my $b12=$a11+$a13;
+ my $b13=$a12;
+
+ return ($b11,$b12,$b13) #-- return just 1st row
+}
+
+# 3x3 matrix * [3;0;2] retain 1st element of product
+sub postmult_302 (*@inmatrix){
+ my ($a11,$a12,$a13)=@inmatrix; #-- just need 1st row
+ # [a11 a12 a13; a21 a22 a23; a31 a32 a33]*[3;0;2]
+ #
+ my $b1=$a11*3+$a13*2; #-- 1st element
+
+ return $b1;
+}