Skip to content

Instantly share code, notes, and snippets.

@scythe
Last active August 23, 2023 20:10
Show Gist options
  • Save scythe/5fb962722934c58c60430180beab86ed to your computer and use it in GitHub Desktop.
Save scythe/5fb962722934c58c60430180beab86ed to your computer and use it in GitHub Desktop.
def gen_primes():
"""Generate an infinite sequence of prime numbers."""
D = {}
q = 2
S = 4
r = 2
while True:
if q not in D:
if q == S:
S = S + r
D.setdefault(S, []).append(r)
r = r + 1
S = S + r
else:
yield q
else:
for p in D[q]:
D.setdefault(p + q, []).append(p)
del D[q]
if q == S:
S = S + r
r = r + 1
S = S + r
q += 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment