This is a Python module I have just finished writing which I plan to use at Project Euler. Please let me know how I have done and what I could do to improve it.
# This constant is more or less an overestimate for the range in which
# n primes exist. Generally 100 primes exist well within 100 * CONST numbers.
CONST = 20
def primeEval(limit):
''' This function yields primes using the
Sieve of Eratosthenes.
'''
if limit:
opRange = [True] * limit
opRange[0] = opRange[1] = False
for (ind, primeCheck) in enumerate(opRange):
if primeCheck:
yield ind
for i in range(ind*ind, limit, ind):
opRange[i] = False
def listToNthPrime(termin):
''' Returns a list of primes up to the nth
prime.
'''
primeList = []
for i in primeEval(termin * CONST):
primeList.append(i)
if len(primeList) >= termin:
break
return primeList
def nthPrime(termin):
''' Returns the value of the nth prime.
'''
primeList = []
for i in primeEval(termin * CONST):
primeList.append(i)
if len(primeList) >= termin:
break
return primeList[-1]
def listToN(termin):
''' Returns a list of primes up to the
number termin.
'''
return list(primeEval(termin))
def lastToN(termin):
''' Returns the prime which is both less than n
and nearest to n.
'''
return list(primeEval(termin))[-1]