How to resolve the algorithm Emirp primes step by step in the Perl programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Emirp primes step by step in the Perl programming language

Table of Contents

Problem Statement

An   emirp   (prime spelled backwards)   are primes that when reversed   (in their decimal representation)   are a different prime. (This rules out palindromic primes.)

In each list, the numbers should be in order. Invoke the (same) program once per task requirement, this will show what limit is used as the upper bound for calculating surplus (regular) primes. The specific method of how to determine if a range or if specific values are to be shown will be left to the programmer.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Emirp primes step by step in the Perl programming language

Source code in the perl programming language

use feature 'say';
use ntheory qw(forprimes is_prime);

# Return the first $count emirps using expanding segments.
# Can efficiently generate millions of emirps.
sub emirp_list {
  my $count = shift;
  my($i, $inc, @n) = (13, 100+10*$count);
  while (@n < $count) {
    forprimes {
      push @n, $_ if is_prime(reverse $_) && $_ ne reverse($_);
    } $i, $i+$inc-1;
    ($i, $inc) = ($i+$inc, int($inc * 1.03) + 1000);
  }
  splice @n, $count;  # Trim off excess emirps
  @n;
}

say "First 20: ", join " ", emirp_list(20);
print "Between 7700 and 8000:";
forprimes { print " $_" if is_prime(reverse $_) && $_ ne reverse($_) } 7700,8000;
print "\n";
say "The 10_000'th emirp: ", (emirp_list(10000))[-1];


  

You may also check:How to resolve the algorithm Word ladder step by step in the 11l programming language
You may also check:How to resolve the algorithm Loops/For step by step in the Emacs Lisp programming language
You may also check:How to resolve the algorithm Rot-13 step by step in the BCPL programming language
You may also check:How to resolve the algorithm Bitmap step by step in the MiniScript programming language
You may also check:How to resolve the algorithm Take notes on the command line step by step in the J programming language