python求素数算法_Python程序最多可计算n个质数(使用不同算法)

python求素数算法

There are various methods through which we can calculate prime numbers upto n.

我们可以通过多种方法来计算最大为n的素数

1) General Method

1)一般方法

In this method, we usually run two for loops in which the First one is used to increase the number and the second one is used to check whether the number is prime or not. Second loop runs from 2 to ( n / 2 + 1 ) ( for better performance).

在这种方法中,我们通常运行两个for循环,其中第一个循环用于增加数字,第二个循环用于检查数字是否为质数。 第二个循环从2到(n / 2 +1)运行(以获得更好的性能)。

Note: This is the least efficient method (one should not use this if efficiency is required.)

注意:这是效率最低的方法(如果需要效率,则不应使用此方法。)

2) Square-Root Method

2)平方根法

In this method, two loops run first one is to increase the number and the second one is to check whether the number is prime or not. The second loop runs from 2 to square root (number) (a number which is to be check), that’s why the run length of second for loop is relatively small, that’s why it’s efficient than the naïve approach.

在此方法中,运行两个循环,第一个循环是增加数字,第二个循环

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值