Back
Close

Python Prime Number Sieves

sanghan
3,592 views
Previous: Algorithm

Implementation

Python Sieve
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))
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.
Go to tech.io
codingame x discord
Join the CodinGame community on Discord to chat about puzzle contributions, challenges, streams, blog articles - all that good stuff!
JOIN US ON DISCORD
Online Participants