diff options
| author | E. Choroba <choroba@matfyz.cz> | 2022-04-01 18:35:00 +0200 |
|---|---|---|
| committer | E. Choroba <choroba@matfyz.cz> | 2022-04-01 18:35:00 +0200 |
| commit | bd800cfa32a8468c61315c8d466dada2e1e538bc (patch) | |
| tree | 01c0631b5a86f683dd341d08f92ff9984acce8b8 | |
| parent | 94c76f89337d76da7cb27601d2b02a5553462bf9 (diff) | |
| download | perlweeklychallenge-club-bd800cfa32a8468c61315c8d466dada2e1e538bc.tar.gz perlweeklychallenge-club-bd800cfa32a8468c61315c8d466dada2e1e538bc.tar.bz2 perlweeklychallenge-club-bd800cfa32a8468c61315c8d466dada2e1e538bc.zip | |
Solve 158: Additive Primes & First Series Cuban Primes by E. Choroba
| -rwxr-xr-x | challenge-158/e-choroba/perl/ch-1.pl | 24 | ||||
| -rwxr-xr-x | challenge-158/e-choroba/perl/ch-2.pl | 26 |
2 files changed, 50 insertions, 0 deletions
diff --git a/challenge-158/e-choroba/perl/ch-1.pl b/challenge-158/e-choroba/perl/ch-1.pl new file mode 100755 index 0000000000..f565667695 --- /dev/null +++ b/challenge-158/e-choroba/perl/ch-1.pl @@ -0,0 +1,24 @@ +#!/usr/bin/perl +use warnings; +use strict; +use experimental 'signatures'; + +use List::Util qw{ sum }; +use Math::Prime::Util qw{ is_prime nth_prime }; + +sub additive_primes ($n) { + my @ap; + my $i = 0; + while (1) { + my $p = nth_prime(++$i); + last if $p > $n; + + push @ap, $p if is_prime(sum(split //, $p)); + } + return \@ap +} + +use Test::More tests => 1; +is_deeply additive_primes(100), + [2, 3, 5, 7, 11, 23, 29, 41, 43, 47, 61, 67, 83, 89], + 'Example'; diff --git a/challenge-158/e-choroba/perl/ch-2.pl b/challenge-158/e-choroba/perl/ch-2.pl new file mode 100755 index 0000000000..b64a65a9eb --- /dev/null +++ b/challenge-158/e-choroba/perl/ch-2.pl @@ -0,0 +1,26 @@ +#!/usr/bin/perl +use warnings; +use strict; + +use experimental 'signatures'; +use Math::Prime::Util qw{ is_prime }; + +sub cuban_primes ($n) { + my @c; + my $y = 0; + while (1) { + # centered hexagonal number + my $chn = 3 * $y * $y + 3 * $y + 1; + last if $chn > $n; + + push @c, $chn if is_prime($chn); + ++$y; + } + return \@c +} + +use Test::More tests => 1; + +is_deeply cuban_primes(1000), + [7, 19, 37, 61, 127, 271, 331, 397, 547, 631, 919], + 'Example'; |
