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

 OEIS A019546: Primes whose digits are primes.
 https://www.scribd.com/document/214851583/On-the-Smarandache-prime-digital-subsequence-sequences


with javascript_semantics
atom t0 = time()
sequence spds = {2,3,5,7}
atom nxt_candidate = 23
sequence adj = {{4,-4},sq_mul({1,2,2,-5},10)},
         adjn = {1,1}
 
include mpfr.e
mpz zprime = mpz_init()
 
procedure populate_spds(integer n)
    while length(spds)<n do
        mpz_set_d(zprime,nxt_candidate)
        if mpz_prime(zprime) then
            spds &= nxt_candidate
        end if
        for i=1 to length(adjn) do
            sequence adjs = adj[i]
            integer adx = adjn[i]
            nxt_candidate += adjs[adx]
            adx += 1
            if adx<=length(adjs) then
                adjn[i] = adx
                exit
            end if
            adjn[i] = 1
            if i=length(adjn) then
                -- (this is eg 777, by now 223 carry 1, -> 2223)
                adj = append(adj,sq_mul(adj[$],10))
                adjn = append(adjn, 1)
                nxt_candidate += adj[$][2]
                exit
            end if
        end for
    end while
end procedure
 
populate_spds(25)
printf(1,"spds[1..25]:%v\n",{spds[1..25]})
for n=2 to 5 do
    integer p = power(10,n)
    populate_spds(p)
    printf(1,"spds[%,d]:%,d\n",{p,spds[p]})
end for
for n=7 to 10 do
    atom p = power(10,n),
        dx = abs(binary_search(p,spds))-1
    printf(1,"largest spds prime less than %,15d:%,14d\n",{p,spds[dx]})
end for
?elapsed(time()-t0)
