diff options
| author | Stephen Lynn <bizlsg@localhost.localdomain> | 2022-06-08 20:03:50 +0800 |
|---|---|---|
| committer | Stephen Lynn <bizlsg@localhost.localdomain> | 2022-06-08 20:03:50 +0800 |
| commit | 0dda125086968453130299deda173151b91da413 (patch) | |
| tree | 686327fde57bd1108430865c16b2942e54c8fe3d | |
| parent | 22fc5dcff65cb0cc13d002172f710c7246e0de4a (diff) | |
| download | perlweeklychallenge-club-0dda125086968453130299deda173151b91da413.tar.gz perlweeklychallenge-club-0dda125086968453130299deda173151b91da413.tar.bz2 perlweeklychallenge-club-0dda125086968453130299deda173151b91da413.zip | |
improved solution to ch-1 and added solution to ch-2
| -rwxr-xr-x | challenge-168/steve-g-lynn/julia/ch-1.jl | 29 | ||||
| -rwxr-xr-x | challenge-168/steve-g-lynn/perl/ch-1.pl | 48 | ||||
| -rwxr-xr-x | challenge-168/steve-g-lynn/perl/ch-2.pl | 37 | ||||
| -rwxr-xr-x | challenge-168/steve-g-lynn/raku/ch-1.p6 | 37 | ||||
| -rwxr-xr-x | challenge-168/steve-g-lynn/raku/ch-2.p6 | 61 |
5 files changed, 136 insertions, 76 deletions
diff --git a/challenge-168/steve-g-lynn/julia/ch-1.jl b/challenge-168/steve-g-lynn/julia/ch-1.jl new file mode 100755 index 0000000000..e58d43e005 --- /dev/null +++ b/challenge-168/steve-g-lynn/julia/ch-1.jl @@ -0,0 +1,29 @@ +#!/usr/bin/julia +#-- slower than perl or raku for this task because of slow startup + +using Primes + +function perrin(n::Int64) + return ( ([0 1 0; 0 0 1; 1 1 0]^n)*[3;0;2] )[1]; +end + +savedprimes=Array{Int64}(undef,13) +savedprimes[1]=2; savedprimes[2]=3;savedprimes[3]=5;savedprimes[4]=7 +ctr=3; + +for n in (6:1000) + perrinval=perrin(n) + if (isprime(perrinval) && (ctr <=13)) + savedprimes[ctr]=perrinval + global ctr=ctr+1 + end +end + +print("(") +for n in (1:13) + print(savedprimes[n]) + print(" ") +end +print(")\n") + + diff --git a/challenge-168/steve-g-lynn/perl/ch-1.pl b/challenge-168/steve-g-lynn/perl/ch-1.pl index a8729418aa..35818da7f9 100755 --- a/challenge-168/steve-g-lynn/perl/ch-1.pl +++ b/challenge-168/steve-g-lynn/perl/ch-1.pl @@ -3,8 +3,11 @@ use Math::Prime::XS qw(is_prime); local %saveprimes=(); -for (0..150) { - local $chk=&perrin($_); +local @perrin=(3,0,2); +for (1..150) { + local $chk=$perrin[0]; + push @perrin, $perrin[0]+$perrin[1]; + shift @perrin; (is_prime($chk)) && ($saveprimes{$chk}=1); @saveprimes==26 && last; } @@ -15,45 +18,4 @@ foreach (sort{$a<=>$b} keys %saveprimes){ } 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/perl/ch-2.pl b/challenge-168/steve-g-lynn/perl/ch-2.pl new file mode 100755 index 0000000000..d1f6f0aced --- /dev/null +++ b/challenge-168/steve-g-lynn/perl/ch-2.pl @@ -0,0 +1,37 @@ +#!/usr/bin/perl + +use Math::Prime::XS qw(is_prime sieve_primes); + + +print &home_prime(16),"\n"; +#-- should be 31636373 [wikipedia example] + +sub home_prime { + local ($n)=@_; + while (1){ + $n=&factors($n); + (is_prime($n)) && last; + } + return $n; +} + + +sub factors { + #--return concatenated prime factors of a number n + local ($n)=@_; + local @primes=sieve_primes($n); + local $retstring=""; + + if (is_prime($n)){ + return $n; + } + else { + foreach $prime (sort{$a<=>$b} @primes){ + while ( ($n % $prime) == 0){ + $n /= $prime; + $retstring .= $prime; + } + } + } + return $retstring; +} diff --git a/challenge-168/steve-g-lynn/raku/ch-1.p6 b/challenge-168/steve-g-lynn/raku/ch-1.p6 index ace5c647e1..9610889cf2 100755 --- a/challenge-168/steve-g-lynn/raku/ch-1.p6 +++ b/challenge-168/steve-g-lynn/raku/ch-1.p6 @@ -1,8 +1,11 @@ #!/usr/bin/raku my %saveprimes=(); +my ($p1,$p2,$p3)=(3,0,2); for ^Inf { - my $chk=perrin($_); + my $chk=$p1; + my $p4=$p1+$p2; + $p1=$p2; $p2=$p3;$p3=$p4; (is-prime($chk)) && (%saveprimes{$chk}=1); %saveprimes.elems==13 && last; } @@ -10,36 +13,4 @@ for ^Inf { 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; -} diff --git a/challenge-168/steve-g-lynn/raku/ch-2.p6 b/challenge-168/steve-g-lynn/raku/ch-2.p6 new file mode 100755 index 0000000000..71fa044657 --- /dev/null +++ b/challenge-168/steve-g-lynn/raku/ch-2.p6 @@ -0,0 +1,61 @@ +#!/usr/bin/raku + +say homeprime(16); +# 31636373 +#-- works but very slow: time real 0m59.301s user 0m58.334s sys 0m0.773s +#-- on MacBook Air running Linux FC36 + +#-- sub for home prime + +sub homeprime(Int $n){ + my $ncopy=$n; + while (1) { + $ncopy=factor($ncopy).Int; + ($ncopy.is-prime) && last; + } + return $ncopy; +} + +#--sub for factorizing + +multi sub factor (1) {1} + +multi sub factor (Int $n where $n > 1){ +#-- returns string concatenation of prime factors + my @primes=prime_sieve($n); + my $retstring=""; + my $ncopy=$n; + + ($n.is-prime) && (return $n); + + for @primes -> $prime { + while ( ($ncopy % $prime)==0) { + $ncopy /= $prime; + $retstring ~= $prime; + } + } + return $retstring; +} + +#--sub for sieve of Eratosthenes +#-- (algorihm from wikipedia) +sub prime_sieve(Int $n where $n > 1){ + my @a = 2..$n; + my (%retval=(),@retval=()); + for (@a) -> $i { + %retval{$i}=1; + } + my @i= 2..round(sqrt($n)); + for @i -> $i { + loop ( my $j=($i*$i); $j <= $n; $j += $i){ + %retval{$j}=0; + } + } + my @k = %retval.keys; + for @k -> $k { + if (%retval{$k}==1) { + push @retval, $k; + } + } + return (@retval.sort:{$^a <=> $^b}); +} |
