How to resolve the algorithm Smarandache prime-digital sequence step by step in the AWK programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Smarandache prime-digital sequence step by step in the AWK programming language

Table of Contents

Problem Statement

The Smarandache prime-digital sequence (SPDS for brevity) is the sequence of primes whose digits are themselves prime. For example 257 is an element of this sequence because it is prime itself and its digits: 2, 5 and 7 are also prime.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Smarandache prime-digital sequence step by step in the AWK programming language

Source code in the awk programming language

# syntax: GAWK -f SMARANDACHE_PRIME-DIGITAL_SEQUENCE.AWK
BEGIN {
    limit = 25
    printf("1-%d:",limit)
    while (1) {
      if (is_prime(++n)) {
        if (all_digits_prime(n) == 1) {
          if (++count <= limit) {
            printf(" %d",n)
          }
          if (count == 100) {
            printf("\n%d: %d\n",count,n)
            break
          }
        }
      }
    }
    exit(0)
}
function all_digits_prime(n, i) {
    for (i=1; i<=length(n); i++) {
      if (!is_prime(substr(n,i,1))) {
        return(0)
      }
    }
    return(1)
}
function is_prime(x,  i) {
    if (x <= 1) {
      return(0)
    }
    for (i=2; i<=int(sqrt(x)); i++) {
      if (x % i == 0) {
        return(0)
      }
    }
    return(1)
}


  

You may also check:How to resolve the algorithm Rate counter step by step in the PicoLisp programming language
You may also check:How to resolve the algorithm XML/Input step by step in the Arturo programming language
You may also check:How to resolve the algorithm Primality by Wilson's theorem step by step in the Lua programming language
You may also check:How to resolve the algorithm Deal cards for FreeCell step by step in the Logo programming language
You may also check:How to resolve the algorithm Special characters step by step in the Tcl programming language