买了实体书,正在看小说《学霸的黑科技系统》。
里面提到了孪生素数,然后刚刚用python也写了一个。
求取6千万以内的最大孪生素数,
count: 280558 59999999 60000001
我的电脑用时12分钟左右跑完结果。
import math
def TwinPrime(x):
if x>0:
sq=math.sqrt(x)+1
else:
return 0
i=5
while i<=sq:
ys = x % i
i+=2 #从5开始,只考虑奇数
if ys==0:
return 0
else:
continue
return 1
if __name__ == '__main__':
n=10000000
i=0
count=1
while i<=n:
a=6*i-1
b=6*i+1
if TwinPrime(a)==1:
if TwinPrime(b)==1:
#print('count:',count,'\t',a,'\t',b)
count+=1
i+=1
print('count:', count, '\t', a, '\t', b)