Generator (computer programming): Difference between revisions

Content deleted Content added
Martnym (talk | contribs)
m Generator Expressions: Fixed misspelled word in description and changed a constant in the example code so it makes sense.
Python: make primes run on odds only, test by primes less than sqrt of n only... (check my code please)
Line 58:
 
# Example use: printing out the integers from 10 to 20.
# Note that this iteration terminates normally, despite countfrom() being
# countfrom() being written as an infinite loop.
 
for i in countfrom(10):
Line 70:
 
def primes():
n =yield 2
n = 3
p = []
while True:
if not any( n % f == 0 for f in p ): #This works in Python 2.5+ or with numpy's any()
if not any( n % f == 0 for f in takeWhile(lambda f: f*f <= n, p) ):
yield n
p.append( n )
n += 12
</source>