From 541cbe1b04d5e804d2cabe879317cff9ae3f5cb4 Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Tue, 7 Jun 2022 17:54:35 +0100 Subject: - Added guest contributions by Eric Cheung. --- challenge-168/eric-cheung/python/ch-1.py | 39 ++++++++++++++++++++++ challenge-168/eric-cheung/python/ch-2.py | 57 ++++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+) create mode 100755 challenge-168/eric-cheung/python/ch-1.py create mode 100755 challenge-168/eric-cheung/python/ch-2.py (limited to 'challenge-168') diff --git a/challenge-168/eric-cheung/python/ch-1.py b/challenge-168/eric-cheung/python/ch-1.py new file mode 100755 index 0000000000..df5ae80e8e --- /dev/null +++ b/challenge-168/eric-cheung/python/ch-1.py @@ -0,0 +1,39 @@ + +## Remarks +## https://en.wikipedia.org/wiki/Perrin_number + +import math + +def IsPrime(nInput): + + for nDiv in range(2, int(math.sqrt(nInput)) + 1): + if nInput % nDiv == 0: + return False + + return True + +arrPerrinPrime = [] +arrPerrinNum = [] + +arrPerrinNum.append(3) +arrPerrinNum.append(0) +arrPerrinNum.append(2) + +arrPerrinPrime.append(2) +arrPerrinPrime.append(3) + +while len(arrPerrinPrime) < 13: + nNuNum = arrPerrinNum[-2] + arrPerrinNum[-3] + arrPerrinNum.append(nNuNum) + + if not IsPrime(nNuNum): + continue + + nCount = arrPerrinPrime.count(nNuNum) + + if nCount > 0: + continue + + arrPerrinPrime.append(nNuNum) + +print (arrPerrinPrime) diff --git a/challenge-168/eric-cheung/python/ch-2.py b/challenge-168/eric-cheung/python/ch-2.py new file mode 100755 index 0000000000..4bcd6f1e3a --- /dev/null +++ b/challenge-168/eric-cheung/python/ch-2.py @@ -0,0 +1,57 @@ + +## Remarks +## https://en.wikipedia.org/wiki/Home_prime + +import math + +def IsPrime(nInput): + + for nDiv in range(2, int(math.sqrt(nInput)) + 1): + if nInput % nDiv == 0: + return False + + return True + + +def PrimeFact(nOrigInput): + + nInput = nOrigInput + arrPrimeFact = [] + + for nDiv in range(2, nOrigInput): + + while nInput % nDiv == 0 and nInput > 0: + + nInput = nInput / nDiv + arrPrimeFact.append(nDiv) + + if nInput == 0: + break + + return arrPrimeFact + + +def ConcatArray(arrInput): + + strResult = "" + + for arrElem in arrInput: + strResult = strResult + str(arrElem) + + return int(strResult) + + +## print (PrimeFact(511)) +## print (ConcatArray(PrimeFact(511))) +## print (ConcatArray(PrimeFact(8))) + + +nOrigInputNum = 10 + +nInputNum = nOrigInputNum + +while not IsPrime(nInputNum): + nInputNum = ConcatArray(PrimeFact(nInputNum)) + +print ("HP(" + str(nOrigInputNum) + ") = " + str(nInputNum)) + -- cgit From ac32e51ae34212cd68b2cfdd8f72f54681006e29 Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Tue, 7 Jun 2022 18:13:36 +0100 Subject: - Added solutions by Robert DiCicco. --- challenge-168/robert-dicicco/julia/ch-1.jl | 37 +++++++++++++++++++++ challenge-168/robert-dicicco/perl/ch-1.pl | 47 ++++++++++++++++++++++++++ challenge-168/robert-dicicco/perl/ch-2.pl | 51 +++++++++++++++++++++++++++++ challenge-168/robert-dicicco/raku/ch-1.raku | 41 +++++++++++++++++++++++ challenge-168/robert-dicicco/raku/ch-2.raku | 51 +++++++++++++++++++++++++++++ challenge-168/robert-dicicco/ruby/ch-1.rb | 37 +++++++++++++++++++++ 6 files changed, 264 insertions(+) create mode 100644 challenge-168/robert-dicicco/julia/ch-1.jl create mode 100644 challenge-168/robert-dicicco/perl/ch-1.pl create mode 100644 challenge-168/robert-dicicco/perl/ch-2.pl create mode 100644 challenge-168/robert-dicicco/raku/ch-1.raku create mode 100644 challenge-168/robert-dicicco/raku/ch-2.raku create mode 100644 challenge-168/robert-dicicco/ruby/ch-1.rb (limited to 'challenge-168') diff --git a/challenge-168/robert-dicicco/julia/ch-1.jl b/challenge-168/robert-dicicco/julia/ch-1.jl new file mode 100644 index 0000000000..45d1bbcd64 --- /dev/null +++ b/challenge-168/robert-dicicco/julia/ch-1.jl @@ -0,0 +1,37 @@ +#!julia.exe + +# AUTHOR: Robert DiCicco +# DATE: 2022-06-06 +# Challenge 168 Perrin Primes ( Julia ) + +using Primes + +perrin = [3,0,2] + +results = [] + +PRIME_COUNT = 13 + +i = 0 + +while i <= PRIME_COUNT + + slots = size(perrin,1) + + calc_val = perrin[slots - 1] + perrin[slots - 2] # since julia arrays are option base 1 + + push!(perrin,calc_val) + + if isprime(calc_val) + + push!(results,calc_val) + + global i += 1 + + end + +end + +results = unique!(sort(results)) + +println(results) diff --git a/challenge-168/robert-dicicco/perl/ch-1.pl b/challenge-168/robert-dicicco/perl/ch-1.pl new file mode 100644 index 0000000000..b72ac33280 --- /dev/null +++ b/challenge-168/robert-dicicco/perl/ch-1.pl @@ -0,0 +1,47 @@ +#!perl.exe + +# AUTHOR: Robert DiCicco +# DATE: 2022-06-06 +# Challenge 168 Perrin Primes ( Perl ) + +use strict; +use warnings; +use feature qw/say/; +use ntheory qw/is_prime/; +use List::MoreUtils qw(uniq); + +my @perrin = qw(3 0 2 ); # working array + +my @results = (); + +my $i = 0; + +my $PRIME_COUNT = 13; + +while($i <= $PRIME_COUNT) { + + my $slots = scalar(@perrin); + + my $calc_val = $perrin[$slots - 2] + $perrin[$slots - 3]; + + push(@perrin, $calc_val); + + if (is_prime($calc_val)) { + + push(@results, $calc_val); + + $i++; + + } + +} + +@results = sort { $a <=> $b } uniq(@results); + +foreach(@results) { + + print("$_ "); + +} + +print("\n"); diff --git a/challenge-168/robert-dicicco/perl/ch-2.pl b/challenge-168/robert-dicicco/perl/ch-2.pl new file mode 100644 index 0000000000..8ecf170a47 --- /dev/null +++ b/challenge-168/robert-dicicco/perl/ch-2.pl @@ -0,0 +1,51 @@ +#!perl.exe + +# AUTHOR: Robert DiCicco +# DATE: 2022-06-06 +# Challenge 168 Home Prime ( Perl ) + +use strict; +use warnings; +use ntheory qw/is_prime factor divisors/; + +my $hp = 8; +my @results; + +sub homeprime { + my $hp = shift; + + my @factors = factor($hp); + + my $hp_new = join('',@factors); + + return($hp_new); + +} + +my $flag = 1; + +push(@results, $hp); + +while ( $flag > 0) { + + my $retval = homeprime($hp); + + if ( is_prime($retval) ){ + + push(@results, $retval); + + $flag = 0; + + } else { + + push(@results, $retval); + + $hp = $retval; + + } + +} + + print ("@results "); + + print("\n"); diff --git a/challenge-168/robert-dicicco/raku/ch-1.raku b/challenge-168/robert-dicicco/raku/ch-1.raku new file mode 100644 index 0000000000..acfda4039c --- /dev/null +++ b/challenge-168/robert-dicicco/raku/ch-1.raku @@ -0,0 +1,41 @@ +use v6; + +# AUTHOR: Robert DiCicco +# DATE: 2022-06-06 +# Challenge 168 Perrin Primes ( Raku ) + +my @perrin = (3,0,2); + +my @results = (); + +my $PRIME_COUNT = 13; + +my $i = 0; + +while $i <= $PRIME_COUNT { + + my $slots = @perrin.elems; + + my $calc_val = @perrin[$slots - 2] + @perrin[$slots - 3]; + + @perrin.push: $calc_val; + + if $calc_val.is-prime { + + @results.push: $calc_val; + + $i++; + + } + +} + +@results = @results.sort.unique; + +for @results -> $val { + + print "$val "; + +} + +say ' '; diff --git a/challenge-168/robert-dicicco/raku/ch-2.raku b/challenge-168/robert-dicicco/raku/ch-2.raku new file mode 100644 index 0000000000..1db7b563b2 --- /dev/null +++ b/challenge-168/robert-dicicco/raku/ch-2.raku @@ -0,0 +1,51 @@ +use v6; + +# AUTHOR: Robert DiCicco +# DATE: 2022-06-06 +# Challenge 168 Home Primes ( Raku ) + +use Prime::Factor; + +my @results; + +sub homeprime( $hp) { + + my @factors = prime-factors($hp); + + return(@factors.join); + +} + +my $hp = 8; + +my $flag = 1; + +@results.push: $hp; + +while $flag > 0 { + + my $retval = homeprime($hp); + + if $retval.is-prime { + + @results.push: $retval; + + $flag = 0; + + } else { + + @results.push: $retval; + + $hp = $retval; + + } + +} + +for @results -> $val { + + print "$val "; + +} + +say ' '; diff --git a/challenge-168/robert-dicicco/ruby/ch-1.rb b/challenge-168/robert-dicicco/ruby/ch-1.rb new file mode 100644 index 0000000000..0a050d6120 --- /dev/null +++ b/challenge-168/robert-dicicco/ruby/ch-1.rb @@ -0,0 +1,37 @@ +#!ruby.exe + +# AUTHOR: Robert DiCicco +# DATE: 2022-06-06 +# Challenge 168 Perrin Primes ( Ruby ) + +require 'prime' + +perrin = Array[3,0,2] + +PRIME_COUNT = 14 + +results = Array.new() + +i = 0 + +while i < PRIME_COUNT + + slots = perrin.length() + + calc_val = perrin[slots - 2] + perrin[slots - 3] + + perrin.push(calc_val) + + if Prime.prime?(calc_val) + + results.push(calc_val) + + i += 1 + + end + +end + +results = results.sort.uniq + +puts "#{results }" -- cgit From fe5f85a0913b16129fd528a9f43472c3012bd2de Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Tue, 7 Jun 2022 18:18:31 +0100 Subject: - Added guest contribution by Robert DiCicco. --- challenge-168/robert-dicicco/julia/ch-2.jl | 45 ++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 challenge-168/robert-dicicco/julia/ch-2.jl (limited to 'challenge-168') diff --git a/challenge-168/robert-dicicco/julia/ch-2.jl b/challenge-168/robert-dicicco/julia/ch-2.jl new file mode 100644 index 0000000000..e486810da1 --- /dev/null +++ b/challenge-168/robert-dicicco/julia/ch-2.jl @@ -0,0 +1,45 @@ +#!julia.exe + +# AUTHOR: Robert DiCicco +# DATE: 2022-06-07 +# Challenge 168 Home Primes ( Julia ) + +using Primes + +results = [] + +function homeprime( hp ) + + temp = [] + + s = " " + + temp = factor(Array, hp) + + for i in 1:length(temp) + + s = s * repr(temp[i]) + + end + + return(parse(Int64, s)) + +end + +hp = 8 + +flag = 1 + +push!(results,hp) + +while flag > 0 + + retval = homeprime(hp) + + push!(results,retval) + + isprime(retval) ? (global flag = 0) : (global hp = retval) + +end + +println(results) -- cgit From e1189741ce4605921a45e07944a2d2be784d6139 Mon Sep 17 00:00:00 2001 From: "E. Choroba" Date: Tue, 7 Jun 2022 21:30:33 +0200 Subject: Add solutions to 168: Perrin Prime & Home Prime by E. Choroba --- challenge-168/e-choroba/perl/ch-1.pl | 24 ++++++++++++++++++++++++ challenge-168/e-choroba/perl/ch-2.pl | 17 +++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100755 challenge-168/e-choroba/perl/ch-1.pl create mode 100755 challenge-168/e-choroba/perl/ch-2.pl (limited to 'challenge-168') diff --git a/challenge-168/e-choroba/perl/ch-1.pl b/challenge-168/e-choroba/perl/ch-1.pl new file mode 100755 index 0000000000..4f1355475d --- /dev/null +++ b/challenge-168/e-choroba/perl/ch-1.pl @@ -0,0 +1,24 @@ +#!/usr/bin/perl +use warnings; +use strict; +use Syntax::Construct qw{ // }; + +use Math::Prime::Util qw{ is_prime }; + +sub perrin_prime { + my ($length) = @_; + my @perrin_sliding = (3, 0, 2); + my @perrin_primes; + while (@perrin_primes < $length) { + push @perrin_sliding, shift(@perrin_sliding) + $perrin_sliding[0]; + push @perrin_primes, $perrin_sliding[1] + if $perrin_sliding[1] > ($perrin_primes[-1] // 0) + && is_prime($perrin_sliding[1]); + } + return @perrin_primes +} + +use Test::More tests => 1; + +is_deeply [perrin_prime(13)], [2, 3, 5, 7, 17, 29, 277, 367, 853, 14197, + 43721, 1442968193, 792606555396977]; diff --git a/challenge-168/e-choroba/perl/ch-2.pl b/challenge-168/e-choroba/perl/ch-2.pl new file mode 100755 index 0000000000..f53c124f35 --- /dev/null +++ b/challenge-168/e-choroba/perl/ch-2.pl @@ -0,0 +1,17 @@ +#!/usr/bin/perl +use warnings; +use strict; + +use Math::Prime::Util qw{ factor is_prime }; + +sub home_prime { + my ($i) = @_; + my $concat = join "", sort { $a <=> $b } factor($i); + + return $concat if is_prime($concat); + + return home_prime($concat) +} + +use Test::More tests => 1; +is home_prime(10), 773; -- cgit From 0dda125086968453130299deda173151b91da413 Mon Sep 17 00:00:00 2001 From: Stephen Lynn Date: Wed, 8 Jun 2022 20:03:50 +0800 Subject: improved solution to ch-1 and added solution to ch-2 --- challenge-168/steve-g-lynn/julia/ch-1.jl | 29 +++++++++++++++ challenge-168/steve-g-lynn/perl/ch-1.pl | 48 +++---------------------- challenge-168/steve-g-lynn/perl/ch-2.pl | 37 +++++++++++++++++++ challenge-168/steve-g-lynn/raku/ch-1.p6 | 37 +++---------------- challenge-168/steve-g-lynn/raku/ch-2.p6 | 61 ++++++++++++++++++++++++++++++++ 5 files changed, 136 insertions(+), 76 deletions(-) create mode 100755 challenge-168/steve-g-lynn/julia/ch-1.jl create mode 100755 challenge-168/steve-g-lynn/perl/ch-2.pl create mode 100755 challenge-168/steve-g-lynn/raku/ch-2.p6 (limited to 'challenge-168') 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}); +} -- cgit From e4536342eb74eb731807602c65588d0895ef9505 Mon Sep 17 00:00:00 2001 From: Julien Fiegehenn Date: Wed, 8 Jun 2022 22:33:00 +0100 Subject: challenge 168, in Perl Written with Github Copilot --- challenge-168/julien-fiegehenn/perl/ch-1.pl | 48 ++++++++++++++++++ challenge-168/julien-fiegehenn/perl/ch-2.pl | 78 +++++++++++++++++++++++++++++ 2 files changed, 126 insertions(+) create mode 100644 challenge-168/julien-fiegehenn/perl/ch-1.pl create mode 100644 challenge-168/julien-fiegehenn/perl/ch-2.pl (limited to 'challenge-168') diff --git a/challenge-168/julien-fiegehenn/perl/ch-1.pl b/challenge-168/julien-fiegehenn/perl/ch-1.pl new file mode 100644 index 0000000000..2e331a2415 --- /dev/null +++ b/challenge-168/julien-fiegehenn/perl/ch-1.pl @@ -0,0 +1,48 @@ +#!/usr/bin/perl +use strict; +use warnings; +use feature 'say'; + +# Task 1: Perrin Prime +# Submitted by: Roger Bell_West +# The Perrin sequence is defined to start with [3, 0, 2]; after that, term N is the sum of terms N-2 and N-3. (So it continues 3, 2, 5, 5, 7, ….) + +# A Perrin prime is a number in the Perrin sequence which is also a prime number. + +# Calculate the first 13 Perrin Primes. + +# f(13) = [2, 3, 5, 7, 17, 29, 277, 367, 853, 14197, 43721, 1442968193, 792606555396977] + +sub is_prime { + my $n = shift; + return 0 if $n < 2; + return 1 if $n == 2; + return 0 if $n % 2 == 0; + for (my $i = 3; $i <= sqrt($n); $i += 2) { + return 0 if $n % $i == 0; + } + return 1; +} + +sub perrin_primes { + my $n = shift; + my %primes = map { $_ => 1 } 2, 3; + my @perrin = (3, 0, 2); + my $i = 3; + while (keys %primes < $n) { + $perrin[$i] = $perrin[$i - 2] + $perrin[$i - 3]; + if (is_prime($perrin[$i])) { + $primes{$perrin[$i]} = 1; + } + $i++; + } + return sort { $a <=> $b } keys %primes; +} + +my @primes = perrin_primes(13); +say "@primes"; + +__END__ +use Test::More; +is_deeply \@primes, [2, 3, 5, 7, 17, 29, 277, 367, 853, 14197, 43721, 1442968193, 792606555396977]; +done_testing; \ No newline at end of file diff --git a/challenge-168/julien-fiegehenn/perl/ch-2.pl b/challenge-168/julien-fiegehenn/perl/ch-2.pl new file mode 100644 index 0000000000..9e9812d039 --- /dev/null +++ b/challenge-168/julien-fiegehenn/perl/ch-2.pl @@ -0,0 +1,78 @@ +#!/usr/bin/perl +use strict; +use warnings; +use feature 'say'; + +# Task 2: Home Prime +# Submitted by: Mohammad S Anwar +# You are given an integer greater than 1. + +# Write a script to find the home prime of the given number. + +# In number theory, the home prime HP(n) of an integer n greater than 1 is the prime number obtained by repeatedly factoring the increasing concatenation of prime factors including repetitions. + +# Further information can be found on Wikipedia and OEIS. + +# Example +# As given in the Wikipedia page, + +# HP(10) = 773, as 10 factors as 2×5 yielding HP10(1) = 25, 25 factors as 5×5 yielding HP10(2) = HP25(1) = 55, 55 = 5×11 implies HP10(3) = HP25(2) = HP55(1) = 511, and 511 = 7×73 gives HP10(4) = HP25(3) = HP55(2) = HP511(1) = 773, a prime number. + +# written with github copilot + +# calculate the prime factors of a number +sub prime_factors { + my $n = shift; + my @factors = (); + my $i = 2; + while ($n > 1) { + while ($n % $i == 0) { + push @factors, $i; + $n /= $i; + } + $i++; + } + return @factors; +} + +sub is_prime { + my $n = shift; + return 0 if $n < 2; + return 1 if $n == 2; + return 0 if $n % 2 == 0; + for (my $i = 3; $i <= sqrt($n); $i += 2) { + return 0 if $n % $i == 0; + } + return 1; +} + +# calculate the home prime of a number +# In number theory, the home prime HP(n) of an integer n greater than 1 is the prime number obtained by repeatedly factoring the increasing concatenation of prime factors including repetitions. +sub home_prime { + my $n = shift; + + return $n if is_prime($n); + return home_prime(join '', prime_factors($n)); +} + +# a version of home_prime that produces debug output at every step +sub home_prime_debug { + my $n = shift; + + say "n = $n"; + return $n if is_prime($n); + my @factors = prime_factors($n); + say "factors = @factors"; + return home_prime_debug(join '', @factors); +} + +__END__ + +use Test::More; +is_deeply [prime_factors(10)], [2, 5]; +is_deeply [prime_factors(13)], [13]; +is_deeply [prime_factors(14)], [2, 7]; + +is home_prime(10), 773; +is home_prime_debug(10), 773; +done_testing; \ No newline at end of file -- cgit From dc6d3cd6cc5fee7911b59bee164cdf4bf32ee69f Mon Sep 17 00:00:00 2001 From: Roger Bell_West Date: Thu, 9 Jun 2022 09:15:38 +0100 Subject: Blog post for challenge #168 --- challenge-168/roger-bell-west/blog.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 challenge-168/roger-bell-west/blog.txt (limited to 'challenge-168') diff --git a/challenge-168/roger-bell-west/blog.txt b/challenge-168/roger-bell-west/blog.txt new file mode 100644 index 0000000000..aca56403ca --- /dev/null +++ b/challenge-168/roger-bell-west/blog.txt @@ -0,0 +1 @@ +https://blog.firedrake.org/archive/2022/06/The_Weekly_Challenge_168__At_Home_with_the_Perrins.html -- cgit From dceb0fcbb748fd8444bb8bf0a1589bc672d2d334 Mon Sep 17 00:00:00 2001 From: Stephen Lynn Date: Thu, 9 Jun 2022 19:03:02 +0800 Subject: improved raku version added julia example for ch-2 --- challenge-168/steve-g-lynn/julia/ch-2.jl | 15 +++++++ challenge-168/steve-g-lynn/raku/ch-2.p6 | 69 +++++++++++++++++++++----------- 2 files changed, 61 insertions(+), 23 deletions(-) create mode 100755 challenge-168/steve-g-lynn/julia/ch-2.jl (limited to 'challenge-168') diff --git a/challenge-168/steve-g-lynn/julia/ch-2.jl b/challenge-168/steve-g-lynn/julia/ch-2.jl new file mode 100755 index 0000000000..19d4c7d498 --- /dev/null +++ b/challenge-168/steve-g-lynn/julia/ch-2.jl @@ -0,0 +1,15 @@ +#! /usr/bin/julia + +using Primes + +function homeprime(n::Int64) + ncopy=n + while (!isprime(ncopy)) + ncopy=parse(Int64,join(factor(Vector,ncopy))) + end + return ncopy +end + +println(homeprime(16)) +#31636373 + diff --git a/challenge-168/steve-g-lynn/raku/ch-2.p6 b/challenge-168/steve-g-lynn/raku/ch-2.p6 index 71fa044657..b4183fd6d3 100755 --- a/challenge-168/steve-g-lynn/raku/ch-2.p6 +++ b/challenge-168/steve-g-lynn/raku/ch-2.p6 @@ -2,8 +2,7 @@ say homeprime(16); # 31636373 -#-- works but very slow: time real 0m59.301s user 0m58.334s sys 0m0.773s -#-- on MacBook Air running Linux FC36 +#-- works but slow #-- sub for home prime @@ -22,7 +21,8 @@ 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 @primes=prime_sieve($n); + my @primes=find_primes($n); my $retstring=""; my $ncopy=$n; @@ -37,25 +37,48 @@ multi sub factor (Int $n where $n > 1){ 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; - } +multi sub find_primes(Int $n where $n <= 13) { + my @primes=(2,3,5,7,11,13); + my @retval=(); + for @primes -> $prime { + ($prime < $n) && (push @retval, $prime); } - return (@retval.sort:{$^a <=> $^b}); + return (@retval.sort); } + +multi sub find_primes(Int $n where $n > 13) { +#-- naive approach by trial factorization + my @primes=(3,7,11,13); + + my $i=13; + + while ($i < $n) { + #-- count only odd numbers not ending in 5. Take 4 at a time. + my @testarray=(); + + (($i+4) < $n) && (push @testarray, ($i+4)); + (($i+6) < $n) && (push @testarray, ($i+6)); + (($i+8) < $n) && (push @testarray, ($i+8)); + (($i+10) < $n) && (push @testarray, ($i+10)); + + while (@testarray.elems > 0) { + my $testitem=shift(@testarray); + unless ((($testitem % 6)==1) || (($testitem % 6)==5)) { + #-- use the property that prime numbers above 5 are 6k+1 or 6k-1 + next; + } + for @primes -> $prime { + ($prime > sqrt($testitem)) && last; + (($testitem % $prime)==0) && last; + } + push @primes, $testitem; + } + $i += 10; + } + + splice(@primes,0,1, <2 3 5>); #-- left out 2 and 5 earlier to avoid + # unnecessary redundant factorizing + return (@primes.sort); +} + + -- cgit From 24bce6c7bbb60c50ed3bead06e8a16da9db8eeef Mon Sep 17 00:00:00 2001 From: Humberto Massa Date: Thu, 9 Jun 2022 12:23:22 -0300 Subject: Answer to challenge 168 by Massa, Humberto --- challenge-168/massa/raku/ch-1.raku | 2 ++ challenge-168/massa/raku/ch-2.raku | 28 ++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 challenge-168/massa/raku/ch-1.raku create mode 100644 challenge-168/massa/raku/ch-2.raku (limited to 'challenge-168') diff --git a/challenge-168/massa/raku/ch-1.raku b/challenge-168/massa/raku/ch-1.raku new file mode 100644 index 0000000000..d8fd9a0b66 --- /dev/null +++ b/challenge-168/massa/raku/ch-1.raku @@ -0,0 +1,2 @@ +use v6; +(3, 0, 2, * + * + 0 * * ... *).grep(*.is-prime)[^20].sort.unique[^13].say diff --git a/challenge-168/massa/raku/ch-2.raku b/challenge-168/massa/raku/ch-2.raku new file mode 100644 index 0000000000..535eaa16fa --- /dev/null +++ b/challenge-168/massa/raku/ch-2.raku @@ -0,0 +1,28 @@ +use v6; + +my sub factors(int \x is copy) { + my int $possible-factor = 2; + gather { + while x > 1 { + if $possible-factor.is-prime && x %% $possible-factor { + take $possible-factor; + x div= $possible-factor + } else { + Nil until is-prime ++$possible-factor + } + } + } +} + +my sub hp(int \x is copy) { + loop { + given +[~] factors x { + .return if .is-prime; + x = $_ + } + } +} + +sub MAIN(Int() \n where * > 1) { + say hp n +} -- cgit From 26d2ce2e72a752e3e622f9e0717c6b95fe205115 Mon Sep 17 00:00:00 2001 From: Humberto Massa Date: Thu, 9 Jun 2022 13:10:04 -0300 Subject: `head()` was the answer... :-) --- challenge-168/massa/raku/ch-1.raku | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'challenge-168') diff --git a/challenge-168/massa/raku/ch-1.raku b/challenge-168/massa/raku/ch-1.raku index d8fd9a0b66..2fbde00b8c 100644 --- a/challenge-168/massa/raku/ch-1.raku +++ b/challenge-168/massa/raku/ch-1.raku @@ -1,2 +1,2 @@ use v6; -(3, 0, 2, * + * + 0 * * ... *).grep(*.is-prime)[^20].sort.unique[^13].say +(3, 0, 2, * + * + 0 * * ... *).grep(*.is-prime).unique.head(13).sort.say -- cgit From 0e1f5b17707b5151487904b00ea69b3ba3b1e961 Mon Sep 17 00:00:00 2001 From: Lubos Kolouch Date: Thu, 9 Jun 2022 21:15:40 +0200 Subject: feat(challenge-168/lubos-kolouch/p[erl,ython]/ch-1.p[ly]): Challenge 168 Task 1 LK Perl Python --- challenge-168/lubos-kolouch/perl/ch-1.pl | 33 +++++++++++++++++++++++++ challenge-168/lubos-kolouch/python/ch-1.py | 39 ++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 challenge-168/lubos-kolouch/perl/ch-1.pl create mode 100644 challenge-168/lubos-kolouch/python/ch-1.py (limited to 'challenge-168') diff --git a/challenge-168/lubos-kolouch/perl/ch-1.pl b/challenge-168/lubos-kolouch/perl/ch-1.pl new file mode 100644 index 0000000000..956d2822e4 --- /dev/null +++ b/challenge-168/lubos-kolouch/perl/ch-1.pl @@ -0,0 +1,33 @@ +package main; +use strict; +use warnings; +use Math::Prime::Util qw(is_prime); + +sub generate_perrin_sequence { + my $n = shift; + my @perrin_sequence = ( 3, 0, 2 ); + + my %perrin_primes; + + while ( scalar keys %perrin_primes < $n ) { + my $next_number = $perrin_sequence[-3] + $perrin_sequence[-2]; + push @perrin_sequence, $next_number; + $perrin_primes{$next_number} = 1 if is_prime($next_number); + } + + my @primes = sort { $a <=> $b } keys %perrin_primes; + + return \@primes; +} + +use Test::More; + +is_deeply( + generate_perrin_sequence(13), + [ 2, 3, 5, 7, 17, 29, 277, 367, 853, 14197, 43721, 1442968193, + 792606555396977 + ] +); + +done_testing; +1; diff --git a/challenge-168/lubos-kolouch/python/ch-1.py b/challenge-168/lubos-kolouch/python/ch-1.py new file mode 100644 index 0000000000..8c8790c5dc --- /dev/null +++ b/challenge-168/lubos-kolouch/python/ch-1.py @@ -0,0 +1,39 @@ +""" Challenge 168 Task 1 LK Python """ + +from sympy import isprime + + +def generate_perrin_primes(n: int) -> list: + """ + Generate all primes up to n using the Perrin sequence. + """ + perrin_sequence = [3, 0, 2] + + perrin_primes: dict = {} + + while len(perrin_primes.keys()) < n: + + next_number = perrin_sequence[-3] + perrin_sequence[-2] + perrin_sequence.append(next_number) + + if isprime(next_number): + perrin_primes[next_number] = True + + return sorted(perrin_primes.keys()) + + +assert generate_perrin_primes(13) == [ + 2, + 3, + 5, + 7, + 17, + 29, + 277, + 367, + 853, + 14197, + 43721, + 1442968193, + 792606555396977, +] -- cgit From 53521e94fb339868c5b875f629ba3097166e50d3 Mon Sep 17 00:00:00 2001 From: Jörg Sommrey <28217714+jo-37@users.noreply.github.com> Date: Mon, 6 Jun 2022 19:50:54 +0200 Subject: Solution to task 1 --- challenge-168/jo-37/perl/ch-1.pl | 155 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 155 insertions(+) create mode 100755 challenge-168/jo-37/perl/ch-1.pl (limited to 'challenge-168') diff --git a/challenge-168/jo-37/perl/ch-1.pl b/challenge-168/jo-37/perl/ch-1.pl new file mode 100755 index 0000000000..8351305463 --- /dev/null +++ b/challenge-168/jo-37/perl/ch-1.pl @@ -0,0 +1,155 @@ +#!/usr/bin/perl -s + +use v5.16; +use Test2::V0 '!array'; +use bigint; +use List::Gen qw(:iterate :zip :source); +use List::Util qw(product); +use Math::Prime::Util qw(is_prime gcd); +use experimental qw(signatures postderef); + +our ($tests, $examples, $iv, $f); +$iv ||= '3, 0, 2'; +$f ||= '1, 1'; + +run_tests() if $tests || $examples; # does not return + +die < k. + Selecting unique primes from this sequence. + +Some known prime sequences: +IV=0,1 F=1,1: Fibonacci +IV=2,1 F=1,1: Lucas +IV=1,1,1 F=1,1: Padovan +IV=3,0,2 F=1,1: Perrin (default) +IV=0,1 F=1,2: Pell +IV=0,1 F=2,1: Jacobsthal + +CAUTION: Improper choice of IV and F will cause an endless loop! + +EOS + + +### Input and Output + +main: { + # Explicit conversion to Math::BigInt is required for a set of + # variables so that all newly generated values inherit therefrom. + # Linear factors: + my $f = [map Math::BigInt->new($_), split /, */, $f]; + # Initial values: + my $iv = [split /, */, $iv]; + + # "say(n)" prints the first n generated elements. + lin_recur_primes($iv, $f)->say(shift); +} + + +### Implementation + +# The solution to task 2 from challenge 154 with different starting +# values could be reused to solve this task. But this would by boring +# and thus the task will be generalized here. +# +# The excellent Math::Prime::Util ennobles List::Gen by referencing it in +# the "SEE ALSO" section. Indeed, it looks very cool but comes with a +# serious flaw: There has not been any development for over 10 years and +# one of the tests is broken. Needed "--force" to install. IMHO the +# test result is ok, but the expected outcome is not. +# "Failed test 'map & \(1 .. 3), 1 .. 2'" +# +# Building a generator for a sequence having the initial values +# S(1),...,S(k) and a recurrence relation defined by a linear +# combination of preceding elements: +# S(n) = F(1) * S(n - k) + ... + F(k) * S(n - 1) for n > k. +# and taking unique primes thereof. +# +sub lin_recur_primes ($s, $f) { + + # Lazily extend the initial sequence using the recurrence relation. + ($s + iterate {($s = [$s->@[1 .. $#$s], + tuples($f, $s)->map('product @$_')->sum])->[-1] + })->uniq->filter(sub {is_prime $_}); +} + + +### Examples and tests + +sub run_tests { + SKIP: { + skip "examples" unless $examples; + + # "take" returns the given number of elements. + is lin_recur_primes([3, 0, 2], [1, 1])->take(13), + [3, 2, 5, 7, 17, 29, 277, 367, 853, 14197, 43721, 1442968193, + 792606555396977], 'task 1'; + } + + SKIP: { + skip "tests" unless $tests; + + # "get" accesses elements by a zero-based index. Thus "get(15)" + # is *three* behind the last from "take(13)". + is lin_recur_primes([3, 0, 2], [1, 1])->get(15), + '22584751787583336797527561822649328254745329', + 'Perrin prime from http://oeis.org/A074788'; + + is lin_recur_primes([0, 1], [1, 1])->take(11), + [2, 3, 5, 13, 89, 233, 1597, 28657, 514229, 433494437, + 2971215073], + 'Fibonacci primes from Wiki'; + + is lin_recur_primes([1, 1, 1], [1, 1])->take(12), + [2, 3, 5, 7, 37, 151, 3329, 23833, 13091204281, 3093215881333057, + 1363005552434666078217421284621279933627102780881053358473, + 1558877695141608507751098941899265975115403618621811951868598809164180630185566719], + 'Padovan primes from Wiki'; + + is lin_recur_primes([2, 1], [1, 1])->take(15), + [2, 3, 7, 11, 29, 47, 199, 521, 2207, 3571, 9349, 3010349, + 54018521, 370248451, 6643838879], + 'Lucas primes from Wiki'; + + is lin_recur_primes([3, 1], [1, 1])->take(20), + [3, 5, 23, 37, 97, 157, 1741, 11933, 50549, 214129, 560597, + 16276621, 180510493, 398386576261, 1042989597313, + 41305516996050613, 174972977841043309, 13300248193487978669, + 238663270054423392193, 624828552868675407173], + 'A091157'; + + is lin_recur_primes([0, 1], [1, 2])->take(8), + [2, 5, 29, 5741, 33461, 44560482149, 1746860020068409, + 68480406462161287469], + 'Pell primes from Wiki'; + + is lin_recur_primes([0, 1], [2, 1])->take(15), + [3, 5, 11, 43, 683, 2731, 43691, 174763, 2796203, 715827883, + 2932031007403, 768614336404564651, 201487636602438195784363, + 845100400152152934331135470251, + 56713727820156410577229101238628035243], + 'Jacobsthal primes from Wiki'; + } + + done_testing; + exit; +} -- cgit