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

(This rules out palindromic primes.)



Task

   show the first   twenty   emirps
   show all emirps between   7,700   and   8,000
   show the   10,000th   emirp


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.



See also

   Wikipedia, Emirp.
   The Prime Pages, emirp.
   Wolfram MathWorld™, Emirp.
   The On‑Line Encyclopedia of Integer Sequences, emirps (A6567).


with javascript_semantics
sequence emirps = {}
 
function rev(integer n)
    integer res = 0
    while n do
        res = res*10+remainder(n,10)
        n = floor(n/10)
    end while
    return res
end function
 
function emirp(integer n)
    if is_prime(n) then
        integer r = rev(n)
        if r!=n and is_prime(r) then
            return true
        end if
    end if
    return false
end function
 
procedure usage()
    printf(1,"use a single command line argument, with no spaces, eg \"1-20\" (first 20), \n")
    printf(1,"\"7700..8000\" (between 7700 and 8000), or \"10000\" (the 10,000th).\n")
    {} = wait_key()
    abort(0)
end procedure
 
procedure main(string arg3)
    sequence args
    integer n,m
    if find('-',arg3) then      -- nth to mth emirp range
        args = scanf(arg3,"%d-%d")
        if length(args)!=1 then usage() end if
        {{n,m}} = args
        integer k = 1
        while length(emirps)<m do
            if emirp(k) then emirps &= k end if
            k += 1
        end while
        printf(1,"emirps %d to %d: %v\n",{n,m,emirps[n..m]})
    elsif match("..",arg3) then -- emirps between n amd m
        args = scanf(arg3,"%d..%d")
        if length(args)!=1 then usage() end if
        {{n,m}} = args
        integer k = 1
        while length(emirps)=0 or emirps[$]<m do
            if emirp(k) then emirps &= k end if
            k += 1
        end while
        sequence s = {}
        for i=1 to length(emirps) do
            if emirps[i]>n then
                for j=i to length(emirps) do
                    if emirps[j]>m then
                        printf(1,"emirps between %d and %d: %v\n",{n,m,emirps[i..j-1]})
                        exit
                    end if
                end for
                exit
            end if
        end for
    else                        -- nth emirp
        args = scanf(arg3,"%d")
        if length(args)!=1 then usage() end if
        {{n}} = args
        integer k = 1
        while length(emirps)<n do
            if emirp(k) then emirps &= k end if
            k += 1
        end while
        printf(1,"emirp %d: %d\n",{n,emirps[n]})
    end if
end procedure
 
sequence cl = command_line()
if length(cl)=2 then
    main("1-20")
    main("7700..8000")
    main("10000")
elsif length(cl)=3 then
    main(cl[3])
else
    usage()
end if
