from itertools import islice, count
from fileinput import input
# ideone.com/aVndFM
def postponed_sieve(): # postponed sieve, by Will Ness
yield 2; yield 3; yield 5; yield 7; # original code David Eppstein,
sieve = {} # Alex Martelli, ActiveState Recipe 2002
ps = postponed_sieve() # a separate base Primes Supply:
p = next(ps) and next(ps) # (3) a Prime to add to dict
q = p*p # (9) its sQuare
for c in count(9,2): # the Candidate
if c in sieve: # c's a multiple of some base prime
s = sieve.pop(c) # i.e. a composite ; or
elif c < q:
yield c # a prime
continue
else: # (c==q): # or the next base prime's square:
s=count(q+2*p,2*p) # (9+6, by 6 : 15,21,27,33,...)
p=next(ps) # (5)
q=p*p # (25)
for m in s: # the next multiple
if m not in sieve: # no duplicates
break
sieve[m] = s # original test entry: ideone.com/WFv4f
def sieve(): # from code.activestate.com/recipes
yield 2 # /117119-sieve-of-eratosthenes
D = {} # original code by
c = 3 # David Eppstein, UC Irvine, 28 Feb 2002
while True:
s = D.pop(c, 0)
if s:
add(D,c + s,s)
else:
yield c
D[c*c] = 2*c
c += 2
def postponed_sieve1(): # postponed sieve, by Will Ness, ideone.com/WFv4f
yield 2 #
D = {} # see also: stackoverflow.com/a/10733621/849891
c = 3 # stackoverflow.com/a/8871918/849891
ps = (p for p in sieve())
p = ps.next() and ps.next() # 3
q = p*p # 9
while True:
s = D.pop(c, 0)
if s:
add(D,c + s,s)
else:
if c < q:
yield c
else:
add(D,c + 2*p,2*p)
p=ps.next()
q=p*p
c += 2
def add(D,x,s):
while x in D: x += s
D[x] = s
for line in input():
n = int(line)
print(n)
print( list( islice( postponed_sieve(), n-1, n+1)))
break
# - base - - postponed -
#
# tested Sept 2012:
#
# 1500000 11.65s-10.9 n^0.99
# 1000000 11.14s-33.9 n^1.23 7.81s-10.9 n^1.01
# 800000 8.46s-33.6 n^1.10 6.24s-10.9 n^1.11
# 400000 3.96s-21.3 n^1.05 2.89s-10.9 n^1.02
# 200000 1.91s-11.4 n^1.09 1.43s-10.9 n^1.01
# 100000 0.90s-11.4MB 0.71s-10.9MB
#
#
# tested in early 2012:
#
# 1500000 13.28s-4.7 n^1.09
# 1000000 10.83s-28.0 n^1.23 8.53s-4.7 n^1.08
# 800000 8.23s-28.0 n^1.13 6.70s-4.7 n^1.09
# 400000 3.76s-15.8 n^1.11 3.14s-4.7 n^1.07
# 200000 1.74s-4.9MB 1.50s-4.7MB
ZnJvbSBpdGVydG9vbHMgaW1wb3J0IGlzbGljZSwgY291bnQKZnJvbSBmaWxlaW5wdXQgaW1wb3J0IGlucHV0CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIyBpZGVvbmUuY29tL2FWbmRGTQpkZWYgcG9zdHBvbmVkX3NpZXZlKCk6ICAgICAgICAgICAgICAgICAgICMgcG9zdHBvbmVkIHNpZXZlLCBieSBXaWxsIE5lc3MgICAgICAKICAgIHlpZWxkIDI7IHlpZWxkIDM7IHlpZWxkIDU7IHlpZWxkIDc7ICAjIG9yaWdpbmFsIGNvZGUgRGF2aWQgRXBwc3RlaW4sIAogICAgc2lldmUgPSB7fSAgICAgICAgICAgICAgICAgICAgICAgICAgICMgICBBbGV4IE1hcnRlbGxpLCBBY3RpdmVTdGF0ZSBSZWNpcGUgMjAwMgogICAgcHMgPSBwb3N0cG9uZWRfc2lldmUoKSAgICAgICAgICAgICAgICMgYSBzZXBhcmF0ZSBiYXNlIFByaW1lcyBTdXBwbHk6CiAgICBwID0gbmV4dChwcykgYW5kIG5leHQocHMpICAgICAgICAgICAgIyAoMykgYSBQcmltZSB0byBhZGQgdG8gZGljdAogICAgcSA9IHAqcCAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICMgKDkpIGl0cyBzUXVhcmUgCiAgICBmb3IgYyBpbiBjb3VudCg5LDIpOiAgICAgICAgICAgICAgICAgIyB0aGUgQ2FuZGlkYXRlCiAgICAgICAgaWYgYyBpbiBzaWV2ZTogICAgICAgICAgICAgICAjIGMncyBhIG11bHRpcGxlIG9mIHNvbWUgYmFzZSBwcmltZQogICAgICAgICAgICBzID0gc2lldmUucG9wKGMpICAgICAgICAgIyAgICAgaS5lLiBhIGNvbXBvc2l0ZSA7IG9yCiAgICAgICAgZWxpZiBjIDwgcTogIAogICAgICAgICAgICAgeWllbGQgYyAgICAgICAgICAgICAgICAgIyBhIHByaW1lCiAgICAgICAgICAgICBjb250aW51ZSAgICAgICAgICAgICAgCiAgICAgICAgZWxzZTogICAjIChjPT1xKTogICAgICAgICAgICAjIG9yIHRoZSBuZXh0IGJhc2UgcHJpbWUncyBzcXVhcmU6CiAgICAgICAgICAgIHM9Y291bnQocSsyKnAsMipwKSAgICAgICAjICAgICg5KzYsIGJ5IDYgOiAxNSwyMSwyNywzMywuLi4pCiAgICAgICAgICAgIHA9bmV4dChwcykgICAgICAgICAgICAgICAjICAgICg1KQogICAgICAgICAgICBxPXAqcCAgICAgICAgICAgICAgICAgICAgIyAgICAoMjUpCiAgICAgICAgZm9yIG0gaW4gczogICAgICAgICAgICAgICAgICAjIHRoZSBuZXh0IG11bHRpcGxlIAogICAgICAgICAgICBpZiBtIG5vdCBpbiBzaWV2ZTogICAgICAgIyBubyBkdXBsaWNhdGVzCiAgICAgICAgICAgICAgICBicmVhawogICAgICAgIHNpZXZlW21dID0gcyAgICAgICAgICAgICAgICAgIyBvcmlnaW5hbCB0ZXN0IGVudHJ5OiBpZGVvbmUuY29tL1dGdjRmCiAgICAgICAgCiAgICAgICAgCgpkZWYgc2lldmUoKTogICAgICAgICAjIGZyb20gY29kZS5hY3RpdmVzdGF0ZS5jb20vcmVjaXBlcwogICAgeWllbGQgMiAgICAgICAgICAjICAgICAgICAgICAgICAgICAvMTE3MTE5LXNpZXZlLW9mLWVyYXRvc3RoZW5lcwogICAgRCA9IHt9ICAgICAgICAgICAjIG9yaWdpbmFsIGNvZGUgYnkKICAgIGMgPSAzICAgICAgICAgICAgIyAgICAgICBEYXZpZCBFcHBzdGVpbiwgVUMgSXJ2aW5lLCAyOCBGZWIgMjAwMgogICAgd2hpbGUgVHJ1ZToKICAgICAgICBzID0gRC5wb3AoYywgMCkKICAgICAgICBpZiBzOgogICAgICAgICAgICBhZGQoRCxjICsgcyxzKQogICAgICAgIGVsc2U6CiAgICAgICAgICAgIHlpZWxkIGMKICAgICAgICAgICAgRFtjKmNdID0gMipjCiAgICAgICAgYyArPSAyCgpkZWYgcG9zdHBvbmVkX3NpZXZlMSgpOiAjIHBvc3Rwb25lZCBzaWV2ZSwgYnkgV2lsbCBOZXNzLCBpZGVvbmUuY29tL1dGdjRmCiAgICB5aWVsZCAyICAgICAgICAgICAgIyAgCiAgICBEID0ge30gICAgICAgICAgICAgIyBzZWUgYWxzbzogIHN0YWNrb3ZlcmZsb3cuY29tL2EvMTA3MzM2MjEvODQ5ODkxCiAgICBjID0gMyAgICAgICAgICAgICAgICAgICAgICAgICAjIHN0YWNrb3ZlcmZsb3cuY29tL2EvODg3MTkxOC84NDk4OTEKICAgIHBzID0gKHAgZm9yIHAgaW4gc2lldmUoKSkKICAgIHAgPSBwcy5uZXh0KCkgYW5kIHBzLm5leHQoKSAgICAgIyAzCiAgICBxID0gcCpwICAgICAgICAgICAgICAgICAgICAgICAgICMgOQogICAgd2hpbGUgVHJ1ZToKICAgICAgICBzID0gRC5wb3AoYywgMCkKICAgICAgICBpZiBzOgogICAgICAgICAgICBhZGQoRCxjICsgcyxzKQogICAgICAgIGVsc2U6CiAgICAgICAgICAgIGlmIGMgPCBxOgogICAgICAgICAgICAgICAgeWllbGQgYwogICAgICAgICAgICBlbHNlOgogICAgICAgICAgICAgICAgYWRkKEQsYyArIDIqcCwyKnApCiAgICAgICAgICAgICAgICBwPXBzLm5leHQoKQogICAgICAgICAgICAgICAgcT1wKnAKICAgICAgICBjICs9IDIKCmRlZiBhZGQoRCx4LHMpOgogICAgd2hpbGUgeCBpbiBEOiB4ICs9IHMKICAgIERbeF0gPSBzCgpmb3IgbGluZSBpbiBpbnB1dCgpOgogICAgbiA9IGludChsaW5lKQogICAgcHJpbnQobikKICAgIHByaW50KCBsaXN0KCBpc2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