简体   繁体   English

(Python)质数函数无限循环

[英](Python) Prime Number Function infinite loop

I'm currently working on a supercomputer to distribute a task (find prime numbers) to other machines. 我目前正在使用超级计算机将任务(查找质数)分配给其他计算机。 Right now I'm hitting an infinite loop somewhere, and I think I've tracked it down to my prime number function, but can't figure exactly where it is. 现在,我在某个地方遇到了无限循环,我想我已经找到了它的质数函数,但无法确切知道它在哪里。 I'm only in into to compsci, so any help is greatly appreciated. 我只是对compsci感兴趣,因此非常感谢您的帮助。

LB is the lower bound, UB upper bound of the range I want prime numbers from. LB是我想要质数的范围的下限,UB是上限。

Thanks! 谢谢!

def do_work(self,LB,UB):

    msg = M_DATA
    initialdata = self.recv()
    n = UB
    p = 2
    total = 0


    for p in range(LB,UB):
        prime = True
        for i in range(2, p):
            if p % i == 0:
                prime = False
                break
        if prime == True:
            #print numbers found
            #print (p)
            total += 1

    return (total)

The distance between two primes increases and "tends towards infinity". 两个质数之间的距离增加并且“趋于无穷”。 Hence for quite big LB , the number of iterations necessary to find primes gets big enough to look like you're stuck in an infinite loop. 因此,对于相当大的LB ,找到质数所需的迭代次数变得足够大,以至于看起来您陷入了无限循环。 That's why primes are hard to find. 这就是为什么很难找到素数的原因。

You can improve your algorithm without much changes by looking at the definition of prime numbers. 您可以通过查看质数的定义来改进算法而无需进行太多更改。 You don't need to check for the remainder of each integer less than prime , but only the primes lower or equal than sqrt(prime) to give a definitive answer. 您不需要检查每个整数是否小于prime数的余prime ,而只需检查小于或等于sqrt(prime)即可得出确定的答案。 Take 2 for instance: if a number isn't divided by 2, it won't be devided by an even number (4, 6, 8, 10, etc.). 以2为例:如果一个数字不除以2,则不会被偶数(4、6、8、10等)划分。

You can then halve the number of iterations necessary by using range(UB, LB, 2) in your outer loop, given that UB is odd (that is range(UB + (1 - (UB % 2), LB, 2) ). 然后,如果UB是奇数(即range(UB + (1 - (UB % 2), LB, 2) ),则可以在外循环中使用range(UB, LB, 2)将所需的迭代次数减半。

For your inner loop, store a list of found primes and iterate over it while the it's lower than sqrt(prime_to_check) . 对于您的内部循环,存储找到的素数列表,并在其低于sqrt(prime_to_check)时对其进行迭代。 That'll help a lot. 那会很有帮助。

Have you tested the function? 您测试过功能了吗? See below, for an example function extracted from your method. 请参见下文,以了解从您的方法中提取的示例函数。 Without going into its efficiency and all, it doesn't seem to have an infinite loop, though you can see it as per the logic as well. 尽管您可以从逻辑上也看到它,但没有深入了解它的效率和所有方面,它似乎没有无限循环。

#! /usr/bin/env python

def do_work(LB,UB):
    p = 2
    total = 0
    primes = []

    for p in range(LB,UB):
        prime = True
        for i in range(2, p):
            if p % i == 0:
                prime = False
                break
        if prime == True:
            #print numbers found
            primes.append(p)
            total += 1

    return LB, UB, total, primes

for i in xrange(100): print do_work(2, i)

A couple of things to note: you have variables like msg, initialdata, n which are not being used. 需要注意的两件事:您有诸如msg,initialdata,n之类的变量未被使用。 The return statement is somewhat strange. return语句有点奇怪。 return (total,) would return a tuple containing total. return (total,)将返回一个包含total的元组。 return total would return just the total. return total将只返回总数。

M_DATA and self.recv() are unknown here, so I venture that you might have an infinite loop related to those. M_DATA和self.recv()在这里是未知的,因此我敢冒险您可能有一个与之相关的无限循环。

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

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