Last active
August 23, 2023 20:10
-
-
Save scythe/5fb962722934c58c60430180beab86ed to your computer and use it in GitHub Desktop.
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 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