怎么用python求第521025个质数

怎么用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
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值