n=5;count=2;i=3;j=2;
while (count <= n):
for j in range (2,i):
if(i%j == 0):
break
if(j==i):
print i
count = count +1
i = i+1
我试图找到前 n 个质数,但不知何故这段代码似乎无法编译。 程序卡在 for 循环。 我曾尝试使用相同的逻辑在 C 中编写代码,它似乎工作正常,但由于我需要大量支持,python 似乎是一个明显的选择,因此想在 python 中运行。 任何帮助都会很棒。
最佳答案
range(a, b)
从 a
到 b-1
。
n=5;count=2;i=3;j=2;
while (count <= n):
for j in range (2,i):
if(i%j == 0):
break
if(j==i-1):
print i
count = count +1
i = i+1
我打赌你有
int j;
for(j = 2; j < i; j++) {
}
因此到质数循环结束时,j
将是 i
。
Python 在使用 range
时不会超过限制。
关于python - 无法在python中找到质数代码中的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19982323/