Project 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?
# 1百万以下有多少环形素数?
思路:要是环形素数,必须由1379构成(2,5除外)
def struct(n):
string = '1379'
result = []
if n == 0:
return ' '
for i in string:
for j in struct(n-1):
temp = i + j
result.append(temp.strip())
return result
def change(string):
lst = [string]
for i in range(1,len(string)):
substr = string[i:] + string[:i]
lst.append(substr)
return lst
def isprime(n):
if n <= 1:
return False
for i in range(2,int(n**0.5+1)):
if n % i == 0:
return False
return True
lst2 = [2,5] # 2, 5 特殊
for i in range(1,7): # 最大6位数
for j in struct(i):
for s in change(j):
if not isprime(int(s)):
break
else:
lst2.append(int(j))
# lst 的长度就是个数