【发布时间】:2018-11-24 00:42:01
【问题描述】:
我正在开发一个程序,该程序在用户输入最大整数时在一个范围内打印可能的素数列表。基本上是Erasthothenes的筛子。
现在,这个代码块应该迭代 consecInt(一个存储 2 到该最高整数的列表的变量)的所有值并删除它们的倍数。但由于某种原因我不太明白,它只适用于第一个值。
print(" Sieve of Erasthothenes\n Program that generates the possible prime numbers within a given range")
n = int(input("\nEnter the highest range: "))
def primeList(n):
j = 2
consecInt = [int(i) for i in range(2, n+1)]
#A = [i != False for i in range(2, n+1)]
for i in consecInt:
p = i
while j <= n:
c = j*p
if c <= n:
consecInt.remove(c)
j+=1
print(consecInt)
primeList(n)
【问题讨论】:
标签: python-3.x iteration