def is_palindrome(x):
assert isinstance(x,str)
return x == x[::-1]
def gen_primes():
from __builtin__ import next
from itertools import count, ifilter
sieve = lambda prime,gen: ifilter(prime.__rmod__,gen)
prime = 2
gen = sieve(prime,count(start=3,step=2))
while 1:
yield prime
prime = next(gen)
gen = sieve(prime,gen)
if __name__ == '__main__':
from itertools import islice,ifilter
def _compose(f,g):
"XXX: compose two functions which takes one argument."
return lambda x:f(g(x))
gen_palindrome_primes = ifilter(_compose(is_palindrome,str),gen_primes())
for idx,num in enumerate(islice(gen_palindrome_primes,50),start=1):
print idx,num
CmRlZiBpc19wYWxpbmRyb21lKHgpOgogICAgYXNzZXJ0IGlzaW5zdGFuY2UoeCxzdHIpCiAgICByZXR1cm4geCA9PSB4Wzo6LTFdCgpkZWYgZ2VuX3ByaW1lcygpOgogICAgZnJvbSBfX2J1aWx0aW5fXyBpbXBvcnQgbmV4dAogICAgZnJvbSBpdGVydG9vbHMgaW1wb3J0IGNvdW50LCBpZmlsdGVyCiAgICBzaWV2ZSA9IGxhbWJkYSBwcmltZSxnZW46IGlmaWx0ZXIocHJpbWUuX19ybW9kX18sZ2VuKQoKICAgIHByaW1lID0gMgogICAgZ2VuID0gc2lldmUocHJpbWUsY291bnQoc3RhcnQ9MyxzdGVwPTIpKQoKICAgIHdoaWxlIDE6CiAgICAgICAgeWllbGQgcHJpbWUKICAgICAgICBwcmltZSA9IG5leHQoZ2VuKQogICAgICAgIGdlbiA9IHNpZXZlKHByaW1lLGdlbikKCmlmIF9fbmFtZV9fID09ICdfX21haW5fXyc6CiAgICBmcm9tIGl0ZXJ0b29scyBpbXBvcnQgaXNsaWNlLGlmaWx0ZXIKCiAgICBkZWYgX2NvbXBvc2UoZixnKToKICAgICAgICAiWFhYOiBjb21wb3NlIHR3byBmdW5jdGlvbnMgd2hpY2ggdGFrZXMgb25lIGFyZ3VtZW50LiIKICAgICAgICByZXR1cm4gbGFtYmRhIHg6ZihnKHgpKQoKICAgIGdlbl9wYWxpbmRyb21lX3ByaW1lcyA9IGlmaWx0ZXIoX2NvbXBvc2UoaXNfcGFsaW5kcm9tZSxzdHIpLGdlbl9wcmltZXMoKSkKICAgIGZvciBpZHgsbnVtIGluIGVudW1lcmF0ZShpc2xpY2UoZ2VuX3BhbGluZHJvbWVfcHJpbWVzLDUwKSxzdGFydD0xKToKICAgICAgICBwcmludCBpZHgsbnVtCg==