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))
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]:
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)