Python Prime Number Sieves
sanghan
17.3K views
Open Source Your Knowledge, Become a Contributor
Technology knowledge has to be shared and made accessible for free. Join the movement.
Implementation
Python Sieve
1
2
3
4
5
6
7
8
9
10
11
12
13
14
def sieve(n: int) -> list:
"""
Sieve away and only primes are left.
"""
primes = 2*[False] + (n-1)*[True]
for i in range(2, int(n**0.5+1.5)):
for j in range(i*i, n+1, i):
primes[j] = False
return [prime for prime, checked in enumerate(primes) if checked]
if __name__ == '__main__':
print(sieve(100))
Enter to Rename, Shift+Enter to Preview
Open Source Your Knowledge: become a Contributor and help others learn. Create New Content