带有 numpy 的 Eratosthenes 筛分,没有“for”或“while”循环

问题描述

我为我的问题找到了一个算法。现在我想只使用 numpy 数组函数解决没有循环的问题。 到目前为止,我的代码如下所示:

def sieve(n):
    a= np.ones(n,dtype=bool)
    a[0] = a[1] = False
    for i in range(2,math.floor(math.sqrt(n)) + 1):
        if a[i]:
            a[i*i::i] = False
    return np.nonzero(a)[0]

print(sieve(2000))

代码有效,但我的任务是在没有循环的情况下重新创建它。我不知道了。

得到的帮助是:

# set each second element starting from element 4 to 0
b[4::2] = 0

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)