我最近开始搞乱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
任何帮助和简明的解释都将不胜感激
答案 0 :(得分:1)
编辑:我刚刚意识到了这个问题。 @tichodroma解决了这个问题,但是通过编辑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