我在执行“Eratosthenes筛选”时出现错误,以获取指定范围内的所有素数。我知道我的代码还没有素检,我会在解决错误后添加它们。实施Eratosthenes筛选时的错误
def foo(l,r):
if l == r:
return "Error"
if l > r:
return "Error"
if l < r:
pool = set(xrange(l,r + 1))
prime = 2
if l == 1:
print "Discard 1 for now"
while prime <= r:
rem = set(xrange(prime,r + 1,prime))
pool.difference_update(rem)
a = 0
while prime >= pool[a]:
a = a + 1
prime = pool[a]
print pool
foo(1,31623)
错误:
Traceback (most recent call last):
File "D:\code\sieve_of_eratothenes.py", line 32, in <module>
foo(1,31623)
File "D:\code\sieve_of_eratothenes.py", line 27, in foo
while prime >= pool[a]:
TypeError: 'set' object does not support indexing
有关使用'set'做一个筛子的示例:http://stackoverflow.com/a/9302299/5987 –