fork download
  1.  
  2. def is_palindrome(x):
  3. assert isinstance(x,str)
  4. return x == x[::-1]
  5.  
  6. def gen_primes():
  7. from __builtin__ import next
  8. from itertools import count, ifilter
  9. sieve = lambda prime,gen: ifilter(prime.__rmod__,gen)
  10.  
  11. prime = 2
  12. gen = sieve(prime,count(start=3,step=2))
  13.  
  14. while 1:
  15. yield prime
  16. prime = next(gen)
  17. gen = sieve(prime,gen)
  18.  
  19. if __name__ == '__main__':
  20. from itertools import islice,ifilter
  21.  
  22. def _compose(f,g):
  23. "XXX: compose two functions which takes one argument."
  24. return lambda x:f(g(x))
  25.  
  26. gen_palindrome_primes = ifilter(_compose(is_palindrome,str),gen_primes())
  27. for idx,num in enumerate(islice(gen_palindrome_primes,50),start=1):
  28. print idx,num
  29.  
Success #stdin #stdout 0.84s 5172KB
stdin
Standard input is empty
stdout
1 2
2 3
3 5
4 7
5 11
6 101
7 131
8 151
9 181
10 191
11 313
12 353
13 373
14 383
15 727
16 757
17 787
18 797
19 919
20 929
21 10301
22 10501
23 10601
24 11311
25 11411
26 12421
27 12721
28 12821
29 13331
30 13831
31 13931
32 14341
33 14741
34 15451
35 15551
36 16061
37 16361
38 16561
39 16661
40 17471
41 17971
42 18181
43 18481
44 19391
45 19891
46 19991
47 30103
48 30203
49 30403
50 30703