Created
September 7, 2010 21:06
-
-
Save gregglind/569102 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
description = ''' | |
By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6^(th) prime is 13. | |
What is the 10001^(st) prime number? | |
''' | |
# modifying our sieve | |
from __future__ import division | |
def gen_primes(n_primes): | |
''' this is a sieve of eratosthenes approach | |
could use a wheel for better (faster) performance | |
''' | |
primes = [] | |
n = 1 | |
while len(primes) < n_primes: | |
n += 1 | |
good = True | |
for x in primes: | |
if n % x == 0: | |
good = False | |
break | |
if good: | |
yield n # yield before we append, so that we don't | |
# have to go up to the next prime before we can exit. | |
primes.append(n) | |
P = list(gen_primes(10001)) | |
print P[-1] | |
# 104743 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment