genPrimes()
unconditionally calls itself with exactly the same arguments. This leads to infinite recursion.
Here is one way to do it using a (non-recursive) generator:
def gen_primes():
n = 2
primes = set()
while True:
for p in primes:
if n % p == 0:
break
else:
primes.add(n)
yield n
n += 1
Note that this is optimized for simplicity and clarity rather than performance.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…