diff options
| author | E. Choroba <choroba@matfyz.cz> | 2022-06-07 21:30:33 +0200 |
|---|---|---|
| committer | E. Choroba <choroba@matfyz.cz> | 2022-06-07 21:31:19 +0200 |
| commit | e1189741ce4605921a45e07944a2d2be784d6139 (patch) | |
| tree | 6a21aae049c03ad6bcdb224052edae35f8940eab | |
| parent | fe5f85a0913b16129fd528a9f43472c3012bd2de (diff) | |
| download | perlweeklychallenge-club-e1189741ce4605921a45e07944a2d2be784d6139.tar.gz perlweeklychallenge-club-e1189741ce4605921a45e07944a2d2be784d6139.tar.bz2 perlweeklychallenge-club-e1189741ce4605921a45e07944a2d2be784d6139.zip | |
Add solutions to 168: Perrin Prime & Home Prime by E. Choroba
| -rwxr-xr-x | challenge-168/e-choroba/perl/ch-1.pl | 24 | ||||
| -rwxr-xr-x | challenge-168/e-choroba/perl/ch-2.pl | 17 |
2 files changed, 41 insertions, 0 deletions
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; |
