The number, $197$, is called a circular prime because all rotations of the digits: $197$, $971$, and $719$, are themselves prime.

There are thirteen such primes below $100$: $2$, $3$, $5$, $7$, $11$, $13$, $17$, $31$, $37$, $71$, $73$, $79$, and $97$.

How many circular primes are there below one million?

For solution lets generate all primes below one million.


In [8]:
N=10^6
@time pa = primes(2, N);
@time p=IntSet(pa);
println(length(pa))


  0.006152 seconds (23 allocations: 2.255 MB, 48.20% gc time)
  0.000635 seconds (19 allocations: 512.672 KB)
78498

In [9]:
function is_cp(i)
    s = i
    n = ndigits(i) - 1
    is_circular_prime = true
    while true
        is_circular_prime = i in p
        if is_circular_prime == false 
            break
        end
        i = (10^n * mod(i, 10)) + div(i , 10)
        if s == i
            break
        end
    end
    return is_circular_prime
end
is_cp(197)


Out[9]:
true

In [12]:
cps = []
for i in pa
    t = is_cp(i)
    if t
        push!(cps, i)
    end
end
println(sum(cps), " ", length(cps) )
println(cps)


8184200 55
Any[2,3,5,7,11,13,17,31,37,71,73,79,97,113,131,197,199,311,337,373,719,733,919,971,991,1193,1931,3119,3779,7793,7937,9311,9377,11939,19391,19937,37199,39119,71993,91193,93719,93911,99371,193939,199933,319993,331999,391939,393919,919393,933199,939193,939391,993319,999331]