Skip to content

Instantly share code, notes, and snippets.

@AnweshGangula
Created July 3, 2021 19:43
Show Gist options
  • Save AnweshGangula/8bd0eb5a52c100bd5dbd467febb8f195 to your computer and use it in GitHub Desktop.
Save AnweshGangula/8bd0eb5a52c100bd5dbd467febb8f195 to your computer and use it in GitHub Desktop.
Fastest way to get first n prime numbers
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