gpt4 book ai didi

python - 素数生成器花费太多时间

转载 作者:太空狗 更新时间:2023-10-30 01:49:07 24 4
gpt4 key购买 nike

<分区>

我正在解决问题:

By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.
What is the 10 001st prime number?

def checkPrime(x):
facs = 0
for i in range(1,x):
if x%i==0:
facs = facs + 1
if facs == 2:
return True
else :
return False

i = 1
noPrime = 0
done = False
while(done==False):
i = i + 1
print "i = {0} and noPrime={1}".format(i,noPrime)
if checkPrime(i)==True:
noPrime = noPrime + 1
if noPrime==10001 :
print i
done=True

但是要花很多时间。
我怎样才能加快速度?

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com