omega-rosetta
aien-dev/omega-rosetta
Brought by drakestapleton
omega-rosetta / tasks / twin-primes / oracle.py
| 1 | # Independent reference for "Twin primes" (Python 3, standard library only). |
| 2 | # expected.txt is this program's stdout; omc never produced it. |
| 3 | import bisect, itertools |
| 4 | N = 10**8 |
| 5 | s = bytearray([1]) * N |
| 6 | s[0] = s[1] = 0 |
| 7 | for i in range(2, 10**4): |
| 8 | if s[i]: |
| 9 | s[i * i::i] = bytes(len(range(i * i, N, i))) |
| 10 | ps = list(itertools.compress(range(N), s)) |
| 11 | # the larger member of each twin pair, so "below lim" means that member < lim |
| 12 | big = [p + 2 for p in ps if p + 2 < N and s[p + 2]] |
| 13 | for e in range(1, 9): |
| 14 | print(f"twin prime pairs below {10**e}: {bisect.bisect_left(big, 10**e)}") |