prime_sieve()
<def prime_sieve(n):
sieve = [True] * (n//2)
for i in range(3,int(n**0.5)+1,2):
if sieve[i//2]:
sieve[i*i//2::i] = [False] * ((n-i*i-1)//(2*i)+1)
return [2] + [2*i+1 for i in range(1,n//2) if sieve[i]]
def rotate(s):
return [s[n:] + s[:n] for n in range(1, len(s))]
s = set('024568')
L = int(input("Enter a search limit less than 1,000,000? "))
primes = set(['2','5']+[p for p in map(str, prime_sieve(L)) if not set(p).intersection(s)])
circular_primes = [int(p) for p in primes if all(pr in primes for pr in rotate(p))]
print ("Number of circular primes below", L, " is",len(circular_primes))
print ("They are:", sorted(circular_primes), " With a sum of",sum(circular_primes))
Thanks for asking the question. It's definitely open to interpretation. Although this solution was accepted by HackerRank I'm beginning to wonder why your view is not a better answer. Maybe just generate all circular primes below 1 million to a list and just reference that list for circular primes queries for 10<=N<1,000,000. Thanks for making me think about this again!