这是我的密码。我试图做一个有效但简单的erastotenes筛选,但当我运行程序时,它只会不断返回整数,不管我把范围放多大。这是在python3中。你知道吗
lyst = []
for i in range(2, 100):
print(i)
lyst.append(i)
count = 2
index = 1
for x in lyst:
print(str(index) + " : " + str(count))
if x%count == 0 and x != count:
lyst.remove(x) #removing all multiples of count
if max(lyst) == count: #breaks loop if count is the largest prime
break
else:
count = lyst[lyst.index(count)+1] #turns count into next prime
index += 1 #this is just for me to track how many primes i've found
x和count的值将始终具有相同的值:
remove
将不执行结论:它们在每次迭代开始时具有相同的值,因此
if
条件永远不会满足。你知道吗第二,Erasthotenes的筛子不应该从筛子中除去元素,而是将某些值标记为非素数。它的功能是将值保持在原始索引,因此
.remove()
实际上不应该出现在纯筛算法中。你知道吗要获得找到正确实现的灵感,您可以查看以下几个答案:
这是您的代码和wikipedia description的混合体:
这应该可以。。。看看移除倍数的内部循环:
相关问题 更多 >
编程相关推荐