from itertools import islice
def integers_from(n): # 2,3,4,...
while True:
yield n
n += 1
def sieve(s):
x = next(s)
yield x
ps = sieve( integers_from(2)) # independent primes supply
p = next(ps)
q = p*p ; print((p,q))
while True:
x = next(s)
while x<q:
yield x
x = next(s)
# here x == q
s = filter(lambda y,p=p: y % p, s) # filter creation postponed
p = next(ps) # until square of p seen in input
q = p*p
print( list( islice( sieve( integers_from(2)), 19990, 20000) ))
# http://i...content-available-to-author-only...e.com/kYh7Di (non-postponed): this:
# 390-400 0.11s 9.8MB 1000: 0.07s 9.6MB
# 590-600 0.18s 10.2MB 10000: 0.34s 9.6MB 20k:0.80s-9.6M
# 890-900 0.34s 10.5MB 100000: 6.96s 9.6MB n^1.3 !!! (optimal TD)
# 990-1000 **run-time error: signal: 25 (SIGXFSZ)** 150000: 11.71s 10.3MB n^1.3
ZnJvbSBpdGVydG9vbHMgaW1wb3J0IGlzbGljZQoKZGVmIGludGVnZXJzX2Zyb20obik6ICAgICMgMiwzLDQsLi4uCiAgICB3aGlsZSBUcnVlOgogICAgICAgIHlpZWxkIG4KICAgICAgICBuICs9IDEKCmRlZiBzaWV2ZShzKToKICAgIHggPSBuZXh0KHMpICAKICAgIHlpZWxkIHgKICAgIHBzID0gc2lldmUoIGludGVnZXJzX2Zyb20oMikpICAgICAgICAgICAjIGluZGVwZW5kZW50IHByaW1lcyBzdXBwbHkKICAgIHAgPSBuZXh0KHBzKSAKICAgIHEgPSBwKnAgICAgICAgOyBwcmludCgocCxxKSkKICAgIHdoaWxlIFRydWU6CiAgICAgICAgeCA9IG5leHQocykKICAgICAgICB3aGlsZSB4PHE6IAogICAgICAgICAgICB5aWVsZCB4CiAgICAgICAgICAgIHggPSBuZXh0KHMpCiAgICAgICAgIyBoZXJlIHggPT0gcQogICAgICAgIHMgPSBmaWx0ZXIobGFtYmRhIHkscD1wOiB5ICUgcCwgcykgICMgZmlsdGVyIGNyZWF0aW9uIHBvc3Rwb25lZCAKICAgICAgICBwID0gbmV4dChwcykgICAgICAgICAgICAgICAgICAgICAgICAjICAgdW50aWwgc3F1YXJlIG9mIHAgc2VlbiBpbiBpbnB1dAogICAgICAgIHEgPSBwKnAgICAgIAoKcHJpbnQoIGxpc3QoIGlzbGljZSggc2lldmUoIGludGVnZXJzX2Zyb20oMikpLCAxOTk5MCwgMjAwMDApICkpIAoKICAjIGh0dHA6Ly9pLi4uY29udGVudC1hdmFpbGFibGUtdG8tYXV0aG9yLW9ubHkuLi5lLmNvbS9rWWg3RGkgKG5vbi1wb3N0cG9uZWQpOiAgICAgICAgIHRoaXM6CiAgIyAzOTAtNDAwICAgMC4xMXMgIDkuOE1CICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIDEwMDA6ICAwLjA3cyAgOS42TUIKICAjIDU5MC02MDAgICAwLjE4cyAxMC4yTUIgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAxMDAwMDogIDAuMzRzICA5LjZNQiAgMjBrOjAuODBzLTkuNk0KICAjIDg5MC05MDAgICAwLjM0cyAxMC41TUIgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIDEwMDAwMDogIDYuOTZzICA5LjZNQiAgIG5eMS4zICAgISEhIChvcHRpbWFsIFREKQogICMgOTkwLTEwMDAgICAqKnJ1bi10aW1lIGVycm9yOiBzaWduYWw6IDI1IChTSUdYRlNaKSoqICAgMTUwMDAwOiAxMS43MXMgMTAuM01CICAgbl4xLjMgICA=
(2, 4)
(2, 4)
(2, 4)
(2, 4)
(2, 4)
[224629, 224633, 224669, 224677, 224683, 224699, 224711, 224717, 224729, 224737]