简体   繁体   English

在python中查找素数

[英]Finding primes in python

I know that python is "slow as dirt", but i would like to make a fast and efficient program that finds primes. 我知道python像泥土一样慢,但是我想做一个快速有效的查找素数的程序。 This is what i have: 这就是我所拥有的:

    num = 5 #Start at five, 2 and 3 are printed manually and 4 is a        multiple of 2
    print("2")
    print("3")

def isPrime(n):
#It uses the fact that a prime (except 2 and 3) is of form 6k - 1 or 6k + 1 and looks only at divisors of this form.

    i = 5
    w = 2
    while (i * i <= n): #You only need to check up too the square root of n
        if (n % i == 0): #If n is divisable by i, it is not a prime
            return False
        i += w
        w = 6 - w
    return True #If it isn´t ruled out by now, it is a prime


while True:
    if ((num % 2 != 0) and (num % 3 != 0)): #save time, only run the     function of numbers that are not multiples of 2 or 3
        if (isPrime(num) == True):
            print(num) #print the now proved prime out to the screen
    num += 2 #You only need to check odd numbers

Now comes my questions: 现在出现我的问题:
-Does this print out ALL prime numbers? -是否打印出所有素数?
-Does this print out any numbers that aren't primes? -这个打印出来的数字不是素数吗?
-Are there more efficient ways(there probably are)? -是否有更有效的方法(可能有)? -How far will this go(limitations of python), and are there any ways to increase upper limit? -这会走多远(python的局限性),还有什么方法可以增加上限?

Using python 2.7.12 使用python 2.7.12

Does this print out ALL prime numbers? 这会打印出所有质数吗?

There are infinitely many primes, as demonstrated by Euclid around 300 BC. 正如公元前300年的欧几里得所证明的那样,有无数个素数。 So the answer to that question is most likely no. 因此,该问题的答案很可能不是。

Does this print out any numbers that aren't primes? 这会打印出任何不是素数的数字吗?

By the looks of it, it doesn't. 从外观上看,事实并非如此。 However, to be sure; 但是,可以肯定的是; why not write a unit test? 为什么不编写单元测试?

Are there more efficient ways(there probably are)? 有没有更有效的方法(可能有)? -How far will this go(limitations of python), and are there any ways to increase upper limit? -这会走多远(python的局限性),还有什么方法可以增加上限?

See Fastest way to list all primes below N or Finding the 10001st prime - how to optimize? 请参见列出N以下所有质数的最快方法查找第10001个质数-如何进行优化?

Checking for num % 2 != 0 even though you increment by 2 each time seems pointless. 检查num%2!= 0,即使每次每次增加2似乎都是没有意义的。

I have found that this algorithm is faster: 我发现此算法更快:

primes=[]

n=3

print("2")
while True:
    is_prime=True
    for prime in primes:
        if n % prime ==0:
            is_prime=False
            break
        if prime*prime>n:
            break

    if is_prime:
        primes.append(n)
        print (n)

    n+=2

This is very simple. 这是非常简单的。 The function below returns True if num is a prime, otherwise False . 如果num是素数,则下面的函数返回True ,否则返回False Here, if we find a factor, other than 1 and itself, then we early stop the iterations because the number is not a prime. 在这里,如果找到一个非1的因子,则因为数字不是质数,所以我们会尽早停止迭代。

def is_this_a_prime(num):
    if num < 2 : return False # primes must be greater than 1
    for i in range(2,num): # for all integers between 2 and num
        if(num % i == 0): # search if num has a factor other than 1 and itself
            return False # if it does break, no need to search further, return False
    return True # if it doesn't we reached that point, so num is a prime, return True

I tried to optimize the code a bit, and this is what I've done.Instead of running the loop for n or n/2 times, I've done it using a conditional statements.(I think it's a bit faster) 我尝试对代码进行一些优化,这就是我所做的事情,而不是运行循环n或n / 2次,而是使用条件语句来完成它(我认为这样做会更快一些)。

def prime(num1, num2):
import math
def_ = [2,3,5,7,11]
result = []
for i in range(num1, num2):
    if i%2!=0 and i%3!=0 and i%5!=0 and i%7!=0 and i%11!=0:
        x = str(math.sqrt(i)).split('.')
        if int(x[1][0]) > 0:
            result.append(i)
    else:
        continue

return def_+result if num1 < 12 else result

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM