1

我知道,已经有很多关于素数的问题,但我不要求代码。我只是想知道,我的矿出了什么问题(我希望评论能帮助你理解我在做什么):

from math import ceil

def isPrimeNumber (n, out='short'): #Checks, whether n is a prime number
    answer = 'Yes'
    for p in range(2, int(ceil(n**0.5))+1): #Checks all numbers lower than 
                                            #SQRT(n), if they are divisors of n
        if n%p == 0:    #If p is a divisor, n isn't prime
            answer = 'No'  
            if out == 'verbose':
                print 'Least divisor is', p
            return False
            break
    if answer == 'Yes': #If there isn't a p, that is a divisor, n is prime
        if out == 'verbose':
            print 'No divisors except for 1 and', str(n)+'!'
        return True   

def primeNumbers (start = 1, stop = 1000, numbers = 0):
    N = stop
    if numbers == 0: #Calculates all prime numbers in N numbers in a row
                     #(N=1000 -> calculates all prime numbers in 1000 numbers,
                     #by default from 1 to 997)
        primes = []
        for i in range(start, N+1):
            if isPrimeNumber(i) == True:
                primes.append(i)
    elif numbers == 1: #Calculates N prime numbers in a row 
                       #(N=1000 -> calculates 1000 prime numbers)
        primes = [start]
        i = len(primes)
        j = 1
        while i <= N: #Stops, when we get N prime numbers - doesn't work!
            n = max(primes) + 1
            while j != 'stop':
                if isPrimeNumber(n, out='short') == True:
                    primes.append(n)
                    i = i + 1
                    j = 'stop' #Stops nested cycle, when reached 
                               #the first prime number
                else:
                    n = n + 1
    else:
        print 'Wrong input! 3rd number in function call must be either 0 or 1'
    return primes

函数 isPrimeNumber() 工作正常。当 numbers=0 时,函数 primeNumbers 也可以正常工作。但是如果数字=1,那么,看起来,其中一个循环永远不会停止,我不明白为什么......

4

3 回答 3

2

你在这里有一个无限循环:

    while i <= N:
        n = max(primes) + 1 ### resetting `n'!
        while j != 'stop':
            if isPrimeNumber(n, out='short') == True:
                ...
                j = 'stop'
            else:
                n = n + 1

一旦j设置为'stop',您将永远不会将其更改回来。一旦发生这种情况,内部while实际上变成了无操作,将外部while变成了无限循环。

于 2012-05-05T12:59:11.413 回答
1

问题是您的 j 变量最终设置为“停止”,然后再也不会重新设置,所以while j!='stop'第一次唯一有效。

    # don't initialize j here
    while i <= N: #Stops, when we get N prime numbers - doesn't work!
        n = max(primes) + 1
        j = 1  #initialize it here
        while j != 'stop':
            if isPrimeNumber(n, out='short') == True:
                primes.append(n)
                i = i + 1
                j = 'stop' #Stops nested cycle, when reached 
                           #the first prime number
            else:
                n = n + 1
于 2012-05-05T13:07:12.580 回答
0

我只是想我会清理一下...

from math import ceil
def isprime (n,out="short"):
    answer = True
    for p in range(2,int(ceil(n ** 0.5)) + 1):
        if n % p == 0:
            answer = False
            if out == "verbose":
                print "Least Divisor: " + str(p)
            return False
    if answer:
        if out == "verbose":
            print "No Divisors (Except For 1 & " + str(n) + "!"
        return True
def primenumbers (start = 1,stop = 1000,numbers = False):
    N = stop
    if numbers:
        primes = [start]
        i = len(primes)
        j = 1
        while i <= N:
            n = max(primes) + 1
            j = true
            while j:
                if isPrimeNumber(n):
                    primes.append(n)
                    i = i + 1
                    j = false
                else:
                    n = n + 1
    else
        primes = []
        for i in range(start,N + 1):
            if isPrimeNumber(i):
                primes.append(i)
    return primes

我确实稍微改变了实际程序。

于 2013-09-03T21:42:01.917 回答