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.
在此方法中,运行两个循环,第一个循环是增加数字,第二个循环