求质数
start=datetime.datetime.now() count=0 for i in range(2,1000): for j in range(2,i): if i%j==0: break else: #print(i) count+=1 end=(datetime.datetime.now()-start).total_seconds() print(count,end) 168 0.015
优化除数
start=datetime.datetime.now() count=0 for i in range(2,1000): for j in range(2,int(i**0.5+1)): if i%j==0: break else: #print(i) count+=1 end=(datetime.datetime.now()-start).total_seconds() print(count,end) 168 0.004
排除偶数,除0,2外,所有偶数都是合数
2是偶数又是最小的质数,排除2,count+1
最小值取3,步数为2
start=datetime.datetime.now() count=1 for i in range(3,1000,2): for j in range(3,int(i**0.5+1),2): if i%j==0: break else: #print(i) count+=1 end=(datetime.datetime.now()-start).total_seconds() print(count,end) 168 0.002