0

我最近开始玩python,我写了一个程序来打印第1000个素数,但输出只显示一个闪烁的光标,代码如下所示:

number = 3
count= 1

while count <= 1000:   
          prime = True
          for x in range(2, number):
          if number % x == 0:
               prime= False
          if prime == True:
               count = count + 1
          if count <= 1000:
               number = number + 1

print number

任何帮助和简洁的解释将不胜感激

4

1 回答 1

1

编辑:我刚刚意识到这个问题。@ticodroma 解决了这个问题,但通过编辑 OP 帖子来解决这个问题。所以当我找到它时,它已经解决了,然而,他通过将打印放入循环来解决它,因此许多数字瀑布。但它应该在循环之外,以便只显示最终结果。还有——在编辑前查看了OP代码后,它的编写方式是需要很长时间才能运行,而“闪烁的线”是系统在后台工作

def isprime(n):
    '''check if integer n is a prime'''
    # make sure n is a positive integer
    n = abs(int(n))
    # 0 and 1 are not primes
    if n < 2:
        return False
    # 2 is the only even prime number
    if n == 2: 
        return True    
    # all other even numbers are not primes
    if not n & 1: 
        return False
    # range starts with 3 and only needs to go up the squareroot of n
    # for all odd numbers
    for x in range(3, int(n**0.5)+1, 2):
        if n % x == 0:
            return False
    return True

counter = 0
number = 0

while True:
    if isprime(number):
        counter+=1
    if counter == 10000:
        break
    number+=1

print number
于 2012-07-31T07:47:55.277 回答