fork download
  1. #!/usr/bin/env python2
  2. from itertools import islice
  3. from math import log
  4.  
  5. def iprimes_upto(limit):
  6. is_prime = [True] * limit
  7. for n in xrange(2, limit):
  8. if is_prime[n]:
  9. yield n
  10. for i in xrange(n*n, limit, n): # start at ``n`` squared
  11. is_prime[i] = False
  12.  
  13. n = int(raw_input('Choose number of primes to print: '))
  14. N = max(12, int(n*(log(n) + log(log(n))) + .5)) # find limit
  15. for p in islice(iprimes_upto(N), n): # get n primes
  16. print p
Success #stdin #stdout 0.01s 7856KB
stdin
10
stdout
Choose number of primes to print: 2
3
5
7
11
13
17
19
23
29