Skip to content

Instantly share code, notes, and snippets.

@jordantkohn
Last active April 21, 2020 01:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jordantkohn/c4aa1899f0d94e9575c97bb7f01e9e1a to your computer and use it in GitHub Desktop.
Save jordantkohn/c4aa1899f0d94e9575c97bb7f01e9e1a to your computer and use it in GitHub Desktop.
def sieve(n):
'''
find all primes less than n
parameter: a natural number n
return: array of length n
the ith element is a binary flag that indicates if the natural number i is prime
(1 for prime, 0 for composite)
'''
x = [1] * n
x[1] = 0
# from 2 to n/2 (non-inclusive)
for i in range(2,int(n/2)):
# start w/ double of i. next will be the triple, and so on
j = 2 * i
while j < n:
# mark the multiple as composite. then increment by i
x[j]=0
j = j+i
return x
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment