From bd800cfa32a8468c61315c8d466dada2e1e538bc Mon Sep 17 00:00:00 2001 From: "E. Choroba" Date: Fri, 1 Apr 2022 18:35:00 +0200 Subject: Solve 158: Additive Primes & First Series Cuban Primes by E. Choroba --- challenge-158/e-choroba/perl/ch-1.pl | 24 ++++++++++++++++++++++++ challenge-158/e-choroba/perl/ch-2.pl | 26 ++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100755 challenge-158/e-choroba/perl/ch-1.pl create mode 100755 challenge-158/e-choroba/perl/ch-2.pl 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'; -- cgit