def newprime(n=(primes[-1]+2)): isprime= False while not isprime: for prime in primes: if n % prime == 0: isprime = False break else: isprime = True if isprime: primes.append(n) f = open('primes.py', 'w') f.write('primes = {0}'.format(str(primes))) f.close() yield n n +=2 isprime = False
def sieve(topvalue): if primes[-1] >= topvalue: return # list from largest determined prime to topvalue list = range(primes[-1] + 1, topvalue + 1) # for instance, two, removes all even numbers for prime in primes: index = 0 while prime * index <= topvalue: try: list.remove(prime * index) except: pass index += 1 if len(list) > 0: primes.append(list[0]) return sieve(topvalue)