【发布时间】:2012-02-21 01:29:02
【问题描述】:
我正在尝试实施埃拉托色尼筛法。输出似乎是正确的(减去需要添加的“2”),但如果函数的输入大于 100k 左右,它似乎需要过多的时间。我可以通过哪些方式优化此功能?
def sieveErato(n):
numberList = range(3,n,2)
for item in range(int(math.sqrt(len(numberList)))):
divisor = numberList[item]
for thing in numberList:
if(thing % divisor == 0) and thing != divisor:
numberList.remove(thing)
return numberList
【问题讨论】:
-
我们可以从绘制分辨率时间作为 n 的函数开始,这可以给我们一些想法...
-
谢谢 Jason,我在几天前读过那篇文章,并且很难处理给定的 Eratosthenes 函数。我想我明白我现在错过了什么。
标签: python optimization primes sieve-of-eratosthenes