在国外网站有一个计算速度对比(https://tiplanet.org/forum/viewtopic.php?f=55&t=24837),其中一项计算素数,程序如下:
try:from time import monotonic
except:pass
def hastime():
try:
monotonic()
return True
except:return False
def nodivisorin(n,l):
for k in l:
if n//k*k==n:
return False
return True
def isprimep(n):
t=hastime()
s,l,k=0 or t and monotonic(),[3],7
if n==2 or n==5:return True
if int(n)!=n or n//2*2==n or n//5*5==5:
return False
if n<k:return n in l
while k*k<n:
if nodivisorin(k,l):l.append(k)
k+=2+2*((k+2)//5*5==k+2)
r=nodivisorin(n,l)
return (t and monotonic() or 1)-s,r
最后结果是:
|