import time def fast_nth_prime(n, limit=125000): if limit % 2 != 0: limit += 1 primes = [True] * limit primes[0],primes[1] = [None] * 2 count = 0 # how many primes have we found? for ind,val in enumerate(primes): if val is True: # sieve out non-primes by multiples of known primes primes[ind*2::ind] = [False] * (((limit - 1)//ind) - 1) count += 1 if count == n: return ind return False start = time.time() prime = fast_nth_prime(10001) elapsed = (time.time() - start) print "found %s in %s seconds." % (prime,elapsed)
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
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