waltermilner

Untitled

Dec 23rd, 2018
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.60 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. import re, sys
  4.  
  5.  
  6. def isPrime(n):
  7.     # see http://www.noulakaz.net/weblog/2007/03/18/a-regular-expression-to-check-for-prime-numbers/
  8.     return re.match(r'^1?$|^(11+?)\1+$', '1' * n) == None
  9.  
  10.  
  11. N = int(sys.argv[1]) # number of primes wanted (from command-line)
  12. M = 100              # upper-bound of search space
  13. l = list()           # result list
  14.  
  15. while len(l) < N:
  16.     l += filter(isPrime, range(M - 100, M)) # append prime element of [M - 100, M] to l
  17.     M += 100                                # increment upper-bound
  18.  
  19. print l[:N] # print result list limited to N elements
Advertisement
Add Comment
Please, Sign In to add comment