怎么用python求第521025个质数
import sys
import time
from math import sqrt
PW = 521025
def is_prime(n):
for i in range(3, int(sqrt(n))+2, 2):
if n % i == 0:
return False
return True
class ProgressBar:
def __init__(self, total=0, width=20):
self.total = total
self.width = width
def show(self, count, done='#', wait='-'):
proc = self.width * count // self.total
ok, undo = done * proc, wait * (self.width-proc)
print(f'\rRunning... [{ok}{undo}] {count}/{self.total}', end='')
def main(total=PW):
start = time.time()
n = 3
bar = ProgressBar(total)
for p in range(2, total):
while True:
n += 2
if is_prime(n):
bar.show(p+1)
break
end = time.time()
print(f'\ncost: {end-start} sec, result: {n}')
if __name__ == '__main__':
main()
Exec the code abort at terminal or cmd:
> python faster_zhishu.py
Running... [####################] 521025/521025
cost: 166.74403166770935 sec, result: 7701847