Skip to content

Instantly share code, notes, and snippets.

@igorvanloo
Last active July 21, 2021 09:04
Show Gist options
  • Save igorvanloo/4cf01966207464d34aa06bfe8c3dbe24 to your computer and use it in GitHub Desktop.
Save igorvanloo/4cf01966207464d34aa06bfe8c3dbe24 to your computer and use it in GitHub Desktop.
Prime Generator Function
#Returns a list of True and False indicating if a number is prime, for example list[7] = True, list[8] = False
def list_primality(n):
result = [True] * (n + 1)
result[0] = result[1] = False
for i in range(int(math.sqrt(n)) + 1):
if result[i]:
for j in range(2 * i, len(result), i):
result[j] = False
return result
#Returns all the prime numbers less than or equal to n, in ascending order.
def list_primes(n):
return [i for (i, isprime) in enumerate(list_primality(n)) if isprime]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment