【问题标题】:Finding primes in python在python中寻找素数
【发布时间】:2017-02-27 10:22:55
【问题描述】:

我知道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

现在是我的问题:
- 这会打印出所有素数吗?
- 这会打印出任何不是素数的数字吗?
- 有没有更有效的方法(可能有)? -这会走多远(python的限制),有没有办法提高上限?

使用python 2.7.12

【问题讨论】:

  • 非常相关,但不完全相同:stackoverflow.com/q/2068372/1639625
  • 你不能只是走进 Python 教堂并说 “它像泥土一样慢”.. 即使是这样。
  • 可以使用Miller-Rabin过滤、Pocklington测试

标签: python performance python-2.7 math primes


【解决方案1】:

这会打印出所有素数吗?

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

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

从表面上看,它没有。但是,可以肯定的是;为什么不写一个单元测试呢?

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

Fastest way to list all primes below NFinding the 10001st prime - how to optimize?

【讨论】:

  • 对于“所有素数”,我的意思是程序不会跳过任何一个。我想知道是否有任何错误可以做,例如2、3、7、11(跳过5),有没有用我的脚本不会打印的数字?
  • 再次;我建议编写一个单元测试。由于素数的无限性质,当然不可能测试所有情况。或者,您可以只使用上面建议的链接中已经尝试和测试过的解决方案之一。
【解决方案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

【讨论】:

    【解决方案3】:

    这很简单。如果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
    

    【讨论】:

    • 这并不比我的效率高,我想?这会检查直到“num”本身的所有潜在因素,但你知道你永远不必检查任何高于 n 的平方根
    • 你好@DriverUpdate。它实际上没有!你可以自己检查。当检测到除 1 或数字本身以外的因素时,我返回 false。
    【解决方案4】:

    我尝试稍微优化一下代码,这就是我所做的。我没有将循环运行 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
    

    【讨论】:

    • 如果有任何错误请告诉我
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-08
    • 1970-01-01
    • 2013-01-17
    • 2011-04-25
    • 2011-10-22
    • 2021-01-18
    • 1970-01-01
    相关资源
    最近更新 更多