def genprimes():
 D = {}
 q = 2
 while 1:
  if (q not in D):
   (yield q)
   D[(q * q)] = [q]
  else:
   for p in D[q]:
    D.setdefault((p + q), []).append(p)
   del D[q]
  q += 1

def genprimes ( ) : D = { } q = 2 while 1 : if ( q not in D ) : ( yield q ) D [ ( q * q ) ] = [ q ] else : for p in D [ q ] : D . setdefault ( ( p + q ) , [ ] ) . append ( p ) del D [ q ] q += 1
yields the sequence of prime numbers via the sieve of eratosthenes .