Circular primes
Problem 35
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?
from tools import rwh_primes2
def run(limit=1000000):
primes = list(rwh_primes2(limit))
count = 1 # Include 2 being prime too.
for p in primes:
p = str(p)
if any(d in p for d in '24680'):
continue
rotations = [p[i + 1:] + p[:i + 1] for i in range(len(p))]
if all(int(e) in primes for e in rotations):
count += 1
return count