""" Got this from StackOverflow (http://stackoverflow.com/questions/1628949/to-find-first-n-prime-numbers-in-python). The regular expression use kinda blew my mind. """ import re, sys def isPrime(n): # see http://www.noulakaz.net/weblog/2007/03/18/a-regular-expression-to-check-for-prime-numbers/ return re.match(r'^1?$|^(11+?)\1+$', '1' * n) == None N = int(12) # number of primes wanted (from command-line) sys.argv[1] M = 100 # upper-bound of search space l = list() # result list while len(l) < N: l += filter(isPrime, range(M - 100, M)) # append prime element of [M - 100, M] to l M += 100 # increment upper-bound print l[:N] # print result list limited to N elements
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question