【发布时间】:2022-01-25 15:57:14
【问题描述】:
"""7. By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.
What is the 10 001st prime number?"""
countprime=0
n=2
def prime(number):
for factors in range(2, number):
if number%factors==0:
return False
break
return True
while countprime!=10001:
if prime(n)==True:
countprime+=1
n+=1
print(n)
答案应该是 104743,但由于某种原因,我的蛮力程序得到了 104744,还有 1 个。有谁知道为什么是一次性的吗?
【问题讨论】:
-
看来您在更新计数之前找到了素数。您可以通过将 while 语句替换为
countprime!=1来看到这一点。
标签: python