omega-rosetta
aien-dev/omega-rosetta
Brought by drakestapleton
omega-rosetta / tasks / extensible-prime-generator / oracle.py
| 1 | # Independent reference for "Extensible prime generator" (Python 3, standard library only). |
| 2 | # expected.txt is this program's stdout; omc never produced it. |
| 3 | import itertools |
| 4 | def primes(): |
| 5 | # incremental sieve: each composite maps to the primes that will strike it |
| 6 | d = {} |
| 7 | for q in itertools.count(2): |
| 8 | if q not in d: |
| 9 | yield q |
| 10 | d[q * q] = [q] |
| 11 | else: |
| 12 | for p in d.pop(q): |
| 13 | d.setdefault(p + q, []).append(p) |
| 14 | ps = list(itertools.islice(primes(), 10000)) |
| 15 | print("first 20 primes: " + " ".join(map(str, ps[:20]))) |
| 16 | print("primes between 100 and 150: " + " ".join(str(p) for p in ps if 100 <= p <= 150)) |
| 17 | print(f"number of primes between 7700 and 8000: {sum(1 for p in ps if 7700 <= p <= 8000)}") |
| 18 | print(f"the 10000th prime: {ps[9999]}") |