Skip to content

Instantly share code, notes, and snippets.

@gregglind
Created September 7, 2010 21:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gregglind/569102 to your computer and use it in GitHub Desktop.
Save gregglind/569102 to your computer and use it in GitHub Desktop.
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