在python中查找素数

在python中查找素数,python,performance,python-2.7,math,primes,Python,Performance,Python 2.7,Math,Primes,我知道python“速度非常慢”,但我想制作一个快速高效的程序来查找素数。这就是我所拥有的: 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 o

我知道python“速度非常慢”,但我想制作一个快速高效的程序来查找素数。这就是我所拥有的:

    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
num=5#从5开始,手动打印2和3,4是2的倍数
打印(“2”)
打印(“3”)
def iPrime(n):
#它使用素数(2和3除外)的形式为6k-1或6k+1的事实,并且只查看这种形式的除数。
i=5
w=2
当我
这个能打印出所有的素数吗

正如公元前300年左右欧几里得所证明的那样,素数是无限多的,所以这个问题的答案很可能是否定的

这会打印出任何不是素数的数字吗

从外观上看,它不是。但是,可以肯定的是,为什么不编写一个单元测试呢

有没有更有效的方法(可能有)?-这会走多远(python的限制),有没有增加上限的方法

看到或

这个能打印出所有的素数吗

正如公元前300年左右欧几里得所证明的那样,素数是无限多的,所以这个问题的答案很可能是否定的

这会打印出任何不是素数的数字吗

从外观上看,它不是。但是,可以肯定的是,为什么不编写一个单元测试呢

有没有更有效的方法(可能有)?-这会走多远(python的限制),有没有增加上限的方法


请参阅或

检查num%2!=0,即使每次增加2似乎没有意义

我发现这个算法更快:

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

检查num%2!=0,即使每次增加2似乎没有意义

我发现这个算法更快:

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

这很简单。如果
num
是素数,下面的函数返回
True
,否则返回
False
。在这里,如果我们找到一个因子,而不是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
def是这个素数(num):
如果num<2:返回False#素数必须大于1
对于范围(2,num)中的i:#对于2和num之间的所有整数
if(num%i==0):#搜索num是否有除1以外的因子及其本身
return False#如果它确实中断,则无需进一步搜索,返回False
return True#如果没有达到该点,那么num是一个素数,返回True

这非常简单。如果
num
是素数,下面的函数返回
True
,否则返回
False
。在这里,如果我们找到除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
def是这个素数(num):
如果num<2:返回False#素数必须大于1
对于范围(2,num)中的i:#对于2和num之间的所有整数
if(num%i==0):#搜索num是否有除1以外的因子及其本身
return False#如果它确实中断,则无需进一步搜索,返回False
return True#如果没有达到该点,那么num是一个素数,返回True

我试着对代码进行了一些优化,这就是我所做的。我没有运行n次或n/2次循环,而是使用了条件语句。(我觉得速度快了一点)

def初始值(num1,num2):
输入数学
定义=[2,3,5,7,11]
结果=[]
对于范围内的i(num1,num2):
如果i%2!=0,i%3!=0,i%5!=0,i%7!=0,i%11!=0:
x=str(math.sqrt(i)).split('.'))
如果int(x[1][0])>0:
结果.追加(i)
其他:
持续
如果num1小于12,则返回def_u+结果,否则返回结果

我试着对代码进行了一些优化,这就是我所做的。我没有运行n次或n/2次循环,而是使用了条件语句。(我觉得速度快了一点)

def初始值(num1,num2):
输入数学
定义=[2,3,5,7,11]
结果=[]
对于范围内的i(num1,num2):
如果i%2!=0,i%3!=0,i%5!=0,i%7!=0,i%11!=0:
x=str(math.sqrt(i)).split('.'))
如果int(x[1][0])>0:
结果.追加(i)
其他:
持续
如果num1小于12,则返回def_u+结果,否则返回结果

非常相关,但不是完全重复:你不能走进Python教堂说“它慢得像灰尘”。即使是。你可以使用Miller-Rabin过滤、Pocklington测试等。非常相关,但不是完全重复:你不能走进Python教堂说“它慢得像灰尘”…即使是。你可以使用Miller-Rabin过滤、Pocklington测试等。我的意思是程序不会跳过任何一个。我想知道是否有任何错误可以做到,例如2、3、7、11(跳过5),是否有不会使用我的脚本打印的数字?再次,我建议编写一个单元测试。当然,由于素数的无限性,不可能测试每种情况。或者,您可以使用上面建议的链接中已经尝试和测试过的解决方案之一。使用“所有素数”,我的意思是该程序不会跳过任何错误。我想知道是否有任何错误,例如2、3、7、11(跳过5),是否有不会使用我的脚本打印的数字?再次,我建议编写一个单元测试。当然,由于质数的无限性,不可能测试每种情况。或者,您可以使用上面建议的链接中已经尝试和测试过的解决方案之一。我认为这并不比我的更有效?这是一个错误将所有潜在因素调整到“num”本身,但你知道你永远不必检查高于nHello@DriverUpdate平方根的任何东西。实际上不必检查!你可以自己检查。当检测到除1或数字本身以外的因子时,我返回false。我认为这并不比我的更有效?这会检查所有潜在因子,直到“num”它本身,但你知道你永远不必检查高于nHello@DriverUpdate平方根的任何东西。它实际上不需要检查!你可以自己检查它。当检测到一个因子时