Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
21 lines (16 sloc) 512 Bytes
# coding: utf-8
def sieve(n):
"""筛法得到n以内的素数"""
primes = [True] * (n + 1)
primes[0] = primes[1] = False
i = 2
while i * 2 <= n:
if primes[i]:
primes[i * 2:n + 1:i] = [False] * (int((n - i * 2) / i) + 1)
# 等同于如下句子,但是更快
# for j in range(i * 2, n + 1, i):
# primes[j] = False
i += 1
return [i for i in range(n + 1) if primes[i]]
if __name__ == "__main__":
print(sieve(200))
You can’t perform that action at this time.