I have these two functions which work together. The first generates the next prime number. The second appends that prime number to a list of primes. I feel like I am overusing variables in the second function when I basically say i = next(n) = nextPrime(primeList). Is there a better way to write this?
def nextPrime(primeList):
checkNum = 3
while True:
for i in primeList:
if checkNum % i == 0:
break
if i > math.sqrt(checkNum):
yield checkNum
break
checkNum += 2
def primeNumbers(limit):
primeList = [2]
i = 0
n = nextPrime(primeList)
while i <= limit:
i = next(n)
primeList.append(i)
return primeList
primeList = primeNumbers(200000)
Does this work out okay?