From 22fc5dcff65cb0cc13d002172f710c7246e0de4a Mon Sep 17 00:00:00 2001 From: Stephen Lynn Date: Tue, 7 Jun 2022 22:08:17 +0800 Subject: challenge 1 --- challenge-168/steve-g-lynn/README | 1 + challenge-168/steve-g-lynn/perl/ch-1.pl | 59 +++++++++++++++++++++++++++++++++ challenge-168/steve-g-lynn/raku/ch-1.p6 | 45 +++++++++++++++++++++++++ 3 files changed, 105 insertions(+) create mode 100644 challenge-168/steve-g-lynn/README create mode 100755 challenge-168/steve-g-lynn/perl/ch-1.pl create mode 100755 challenge-168/steve-g-lynn/raku/ch-1.p6 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; +} -- cgit