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

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Emirp primes step by step in the AWK 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 AWK programming language

Source code in the awk programming language

function is_prime(n,	p)
{
        if (!(n%2) || !(n%3)) {
		return 0 }
        p = 1
        while(p*p < n)
                if (n%(p += 4) == 0 || n%(p += 2) == 0) {
                        return 0 }
        return 1
}

function reverse(n,	r)
{
	r = 0
        for (r = 0; int(n) != 0; n /= 10)
                r = r*10 + int(n%10);
        return r
}

function is_emirp(n,   r)
{
        r = reverse(n)
	return ((r != n) && is_prime(n) && is_prime(r)) ? 1 : 0
}
 
BEGIN {
	c = 0
	for (x = 11; c < 20; x += 2) {
		if (is_emirp(x)) {
			printf(" %i,", x); ++c }
	}
	printf("\n")
	for (x = 7701; x < 8000; x += 2) {
		if (is_emirp(x)) {
			printf(" %i,", x); ++c }
	}
	printf("\n")
	c = 0
	for (x = 11; ; x += 2)
                        if (is_emirp(x) && ++c == 10000) {
                                printf(" %i", x);
                                break;
                        }
	printf("\n")
}


  

You may also check:How to resolve the algorithm Read a configuration file step by step in the Picat programming language
You may also check:How to resolve the algorithm Knapsack problem/Bounded step by step in the Pascal programming language
You may also check:How to resolve the algorithm Character codes step by step in the E programming language
You may also check:How to resolve the algorithm Longest common substring step by step in the Ada programming language
You may also check:How to resolve the algorithm Increasing gaps between consecutive Niven numbers step by step in the REXX programming language