Last active
July 21, 2021 09:04
-
-
Save igorvanloo/4cf01966207464d34aa06bfe8c3dbe24 to your computer and use it in GitHub Desktop.
Prime Generator Function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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