素数
欧式筛法求素数
def euler_primary(n):
"""
记录[0,n)之间的素数
"""
is_primary = [1]*(n+1)
is_primary[0] = is_primary[1] =0
primary = []
for num in range(2, n):
if is_primary[num]:
primary.append(num)
for prime in primary:
if num * prime > n:
break
is_primary[num * prime] = 0
if num % prime == 0:
break
return primary
print(euler_primary(998))
- 时间复杂度:O(n)
- 空间复杂度:O(n)