Created
July 3, 2021 19:43
-
-
Save AnweshGangula/8bd0eb5a52c100bd5dbd467febb8f195 to your computer and use it in GitHub Desktop.
Fastest way to get first n prime numbers
This file contains 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
def historic(n): | |
# Reference: https://stackoverflow.com/a/1638415/6908282 | |
def prime(i, primes): | |
for prime in primes: | |
if not (i == prime or i % prime): | |
return False | |
primes.add(i) | |
return i | |
primes = set([2]) | |
i, p = 2, 0 | |
while True: | |
if prime(i, primes): | |
p += 1 | |
if p == n: | |
return sorted(list(primes)) | |
i += 1 | |
print(historic(1000)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment