2013-04-17 14:34:39 +08:00

43 lines
830 B
Python

''' 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 math import log10
pp = [2]
for i in xrange(3, 1000, 2):
for x in pp:
if i % x == 0:
break
else:
pp.append(i)
def isp(a):
for i in pp:
if a % i == 0:
if a == i:
return True
return False
return True
def loop(x):
length = int(log10(x))
return (x % 10) * 10 ** length + x / 10
def lote(n):
tt = n
while 1:
if not isp(tt):
return False
tt = loop(tt)
if tt == n:
return True
out = [2]
for ii in xrange(3, 1000000, 2):
if lote(ii):
out.append(ii)
print len(out)