我试图尝试项目euler(click here)的第35个问题。问题如下:
这个数字197被称为圆形素数,因为数字的所有旋转:197,971和719本身都是素数。
在100:2,3,5,7,11,13,17,31,37,71,73,79和97之下有十三个这样的素数。
在一百万以下有多少个圆形素数?
所以我用一百万个数字创建了一个筛子,得到了一百万以下的所有素数,并用它来比较素数的旋转结果。
arr = []
for i in range(2, len(sieve)):
if sieve[i]:
sub_arr = retCircular(i)
count = len(sub_arr)
carry = 0
for j in sub_arr:
if sieve[j]:
carry += 1
sieve[j] = False
else:
break
if carry == count:
for j in sub_arr:
arr.append(j)
print "Number of circular primes =", len(arr)
这个程序给出了100万以下的循环素数为54,而实际答案是55.有人可以帮助我解决我的错误吗?
注意:
答案 0 :(得分:2)
我将在这里使用我的顾问ESP:你的retCircular方法没有正确处理重复模式,这使得它错过 repunit (1&#的字符串) 39; s)素数。特别是,retCircular(11)返回[11,11],这使得你的算法错过了这个数字作为循环素数。这是我的方法的蛮力版本:
def retCircular(prime):
prime_str = str(prime)
family = [prime]
for _ in range(len(prime_str)-1):
prime_str = prime_str[1:] + prime_str[0]
child = int(prime_str)
if child == prime:
break
family.append(int(prime_str))
return family
...我的主程序有55个素数:
Prime family: [2]
Prime family: [3]
Prime family: [5]
Prime family: [7]
Prime family: [11]
Prime family: [13, 31]
Prime family: [17, 71]
Prime family: [37, 73]
Prime family: [79, 97]
Prime family: [113, 131, 311]
Prime family: [197, 971, 719]
Prime family: [199, 991, 919]
Prime family: [337, 373, 733]
Prime family: [1193, 1931, 9311, 3119]
Prime family: [3779, 7793, 7937, 9377]
Prime family: [11939, 19391, 93911, 39119, 91193]
Prime family: [19937, 99371, 93719, 37199, 71993]
Prime family: [193939, 939391, 393919, 939193, 391939, 919393]
Prime family: [199933, 999331, 993319, 933199, 331999, 319993]
Number of circular primes = 55