#Python中这个非常简短的代码试图模拟前N个自然数的"Sieve of Eratosthenes",其约束为(0)脚本短; (1)最小化'if语句'和'for/while循环'; (2)CPU时间效率.
import numpy as np
N = 10**5
a = np.array(range(3,N,2))
for j in range(0, int(round(np.sqrt(N),0))):
a[(a!=a[j]) & (a%a[j] == 0)] = 0
a = a[a!=0]
a = [2]+list(a)
Run Code Online (Sandbox Code Playgroud)
在Intel Core I5上,它返回第一个中的素数:
有人愿意在上述约束条件下分享CPU时间方面更高效的代码吗?