# Perl Weekly Challenge 158.

My solutions (task 1 and task 2 ) to the The Weekly Challenge - 158.

``````Submitted by: Mohammad S Anwar
Write a script to find out all Additive Primes <= 100.

Additive primes are prime numbers for which the sum of their
decimal digits are also primes.

Output
2, 3, 5, 7, 11, 23, 29, 41, 43, 47, 61, 67, 83, 89
``````

This is quite straightforward: Generate primes, split into digits, add and test for primeness, and it fits a small oneliner, borrowing functionality from available packages:

``````perl -MMath::Prime::Util=primes,is_prime -MList::Util=sum -E '
say join ", ", grep {is_prime(sum split "", \$_)} (@{primes(100)});'
``````

Results:

``````2, 3, 5, 7, 11, 23, 29, 41, 43, 47, 61, 67, 83, 89
``````

The full version is similar:

`````` 1  # Perl weekly challenge 158
3  #
5  use v5.12;
6  use warnings;
7  use Math::Prime::Util qw(primes is_prime);
8  use List::Util qw(sum);
9  die "Usage: ./ch-1.pl N\n\tto print additive primes up to N" unless @ARGV;
10  my \$N=shift;
11  die "N should be positive" unless \$N>0;
12  say "The additive primes up to \$N are ",
13      join ", ", grep {is_prime(sum split "", \$_)} (@{primes(\$N)});

./ch-1.pl 100
``````

Results:

``````The additive primes up to 100 are 2, 3, 5, 7, 11, 23, 29, 41, 43, 47, 61, 67, 83, 89
``````

# Task 2: First Series Cuban Primes

``````Submitted by: Mohammad S Anwar
Write a script to compute first series Cuban Primes

Output
7, 19, 37, 61, 127, 271, 331, 397, 547, 631, 919.
``````

The first series of cuban primes are primes which may be written as the difference of two consecutive numbers. Thus, to generate them I just generate pairs of consecutive numbers x+1 and x and test the difference of their cubes (x+1)3-x3=3x2+3x+1=1+3x(x+1) for primeness (nothing to do with Cuba, the country). As the difference is not smaller than 3x2, it is enough to test up to (√1000)/3. This yields a small one-liner

``````perl -MMath::Prime::Util=primes,is_prime -E '
say join ", ", grep{is_prime \$_}map{1+3*\$_*(\$_+1)}(1..sqrt(1000/3))'
``````

Results:

``````7, 19, 37, 61, 127, 271, 331, 397, 547, 631, 919
``````

The corresponding program is:

`````` 1  # Perl weekly challenge 158
2  # Task 1: First series cuban primes
3  #
5  use v5.12;
6  use warnings;
7  use Math::Prime::Util qw(is_prime);
8  die "Usage: ./ch-2.pl N\n\tto find cuban primes up to N" unless @ARGV;
9  my \$N=shift;
10  die "N should be positive" unless \$N>0;
11  my \$bound=(-3+sqrt(3)*sqrt(4*\$N-1))/6; # Solve (x+1)^3-x^3=N
12  say "The cuban primes up to \$N are ",
13      join ", ", grep {is_prime \$_} map {1+3*\$_*(\$_+1)} (1..\$bound);
``````

I make a better estimate of the upper bound in order not to go beyond the maximum desired cuban number.

``````./ch-2.pl 2000
``````

Results:

``````The cuban primes up to 2000 are 7, 19, 37, 61, 127, 271, 331, 397, 547, 631, 919, 1657, 1801, 1951
``````
Written on March 28, 2022