Smarandache prime-digital sequence

From Rosetta Code
Smarandache prime-digital sequence is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

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.

Task
  • Show the first 25 SPDS primes.
  • Show the hundredth SPDS prime.


See also

Go

As this task doesn't involve large numbers, a simple prime test routine is adequate. <lang go>package main

import "fmt"

func isPrime(n int) bool {

   if n < 2 {
       return false
   }
   if n%2 == 0 {
       return n == 2
   }
   if n%3 == 0 {
       return n == 3
   }
   d := 5
   for d*d <= n {
       if n%d == 0 {
           return false
       }
       d += 2
       if n%d == 0 {
           return false
       }
       d += 4
   }
   return true

}

func isSPDSPrime(n int) bool {

   if !isPrime(n) {
       return false
   }
   for n > 0 {
       r := n % 10
       if r != 2 && r != 3 && r != 5 && r != 7 {
           return false
       }
       n /= 10
   }
   return true

}

func listSPDSPrimes(startFrom, countFrom, countTo int, printOne bool) int {

   count := countFrom
   for n := startFrom; ; n += 2 {
       if isSPDSPrime(n) {
           count++
           if !printOne {
               fmt.Printf("%2d. %d\n", count, n)
           }
           if count == countTo {
               if printOne {
                   fmt.Printf("%2d. %d\n", count, n)
               }
               return n
           }
       }
   }

}

func main() {

   fmt.Println("The first 25 terms of the Smarandache prime-digital sequence are:")
   fmt.Println(" 1. 2")
   n := listSPDSPrimes(3, 1, 25, false)
   fmt.Println("\nThe hundredth term of the sequence is:")
   listSPDSPrimes(n+2, 25, 100, true)

}</lang>

Output:
The first 25 terms of the Smarandache prime-digital sequence are:
 1. 2
 2. 3
 3. 5
 4. 7
 5. 23
 6. 37
 7. 53
 8. 73
 9. 223
10. 227
11. 233
12. 257
13. 277
14. 337
15. 353
16. 373
17. 523
18. 557
19. 577
20. 727
21. 733
22. 757
23. 773
24. 2237
25. 2273

The hundredth term of the sequence is:
100. 33223

Perl 6

<lang perl6># Implemented as a lazy, extendable list

my @spds = lazy flat 2,3,5,7, (1..*).map: { grep { .is-prime }, flat ( [X] |((2,3,5,7) xx $_), (3,7) )».join };

say 'Smarandache prime-digitals:';

printf "%4d: %s\n", 1+$_, @spds[$_] for flat ^25, 99;</lang>

Output:
Smarandache prime-digitals:
   1: 2
   2: 3
   3: 5
   4: 7
   5: 23
   6: 37
   7: 53
   8: 73
   9: 223
  10: 227
  11: 233
  12: 257
  13: 277
  14: 337
  15: 353
  16: 373
  17: 523
  18: 557
  19: 577
  20: 727
  21: 733
  22: 757
  23: 773
  24: 2237
  25: 2273
 100: 33223

Ring

<lang ring>

  1. Project: Calmo primes

load "stdlib.ring" limit = 25 max = 300000 num = 0 see "working..." + nl see "wait for done..." + nl see "First 25 Calmo primes are:" + nl for n = 1 to max

   if isprime(n)
      res = calmo(n)
      if res = 1
         num = num + 1
         if num < limit + 1
            see "" + num + ". " + n + nl
         ok
         if num = 100
            see "The hundredth Calmo prime is:" + nl
            see "" + num + ". " + n + nl
            exit
         ok
      ok
   ok

next see "done..." + nl

func calmo(p)

    sp = string(p)
    for n = 1 to len(sp)
        if not isprime(sp[n])
           return 0
        ok
    next
    return 1

</lang>

Output:
working...
wait for done...
First 25 Calmo primes are:
1. 2
2. 3
3. 5
4. 7
5. 23
6. 37
7. 53
8. 73
9. 223
10. 227
11. 233
12. 257
13. 277
14. 337
15. 353
16. 373
17. 523
18. 557
19. 577
20. 727
21. 733
22. 757
23. 773
24. 2237
25. 2273
The hundredth Calmo prime is:
100. 33223
done...

zkl

Library: GMP

GNU Multiple Precision Arithmetic Library

Using GMP ( probabilistic primes), because it is easy and fast to generate primes.

Extensible prime generator#zkl could be used instead. <lang zkl>var [const] BI=Import("zklBigNum"); // libGMP

spds:=Walker.zero().tweak(fcn(ps){

  var [const] nps=T(0,0,1,1,0,1,0,1,0,0);  // 2,3,5,7
  p:=ps.nextPrime().toInt();
  if(p.split().filter( fcn(n){ 0==nps[n] }) ) return(Void.Skip);
  p   //  733 --> (7,3,3) --> () --> good,       29 --> (2,9) --> (9) --> bad

}.fp(BI(1)));</lang> <lang zkl>println("The first 25 terms of the Smarandache prime-digital sequence are:"); spds.walk(25).concat(",").println();

println("The hundredth term of the sequence is: ",spds.drop(100-25).value);</lang>

Output:
The first 25 terms of the Smarandache prime-digital sequence are:
2,3,5,7,23,37,53,73,223,227,233,257,277,337,353,373,523,557,577,727,733,757,773,2237,2273
The hundredth term of the sequence is: 33223