【发布时间】:2021-09-19 09:44:15
【问题描述】:
嘿,伙计们,我有一个问题,我为 projecteuler 编写了这段代码以找到一个特定的素数,但是 它最终以无限循环结束,我查找并找到了许多替代方案,但想了解为什么这段代码不起作用。我是编程新手,所以如果您也有任何提示可以改进我的代码,我将不胜感激。
import math
x = 1 #number that is getting checked
y = 0 #indicator of how many prime numbers found
a = 0 #the most recent prime number
while y < 6:
for i in range (2, int(math.sqrt(x))):
if (x % i ) == 0:
x = x + 1
break
else:
a = x
x = x + 1
y = y + 1
break
print (a)
【问题讨论】:
-
对于所有感兴趣的人,这是问题的链接projecteuler.net/problem=7
-
你想回答链接中的问题吗?
-
啊,不像我的问题中所说的,我找到了其他解决方案,但我想了解为什么我的代码以无限循环结束