Re: Generators/iterators, Pythonicity, and primes

2009-04-13 Thread Arnaud Delobelle
Duncan Booth duncan.bo...@invalid.invalid writes: Duncan Booth duncan.bo...@invalid.invalid wrote: John Posner jjpos...@snet.net wrote: Do know what in the itertools implementation causes adding a 'if p = sqrt(n)' clause to *decrease* performance, while adding a 'takewhile()' clause

Re: Generators/iterators, Pythonicity, and primes

2009-04-13 Thread Aaron Brady
On Apr 13, 1:39 am, Arnaud Delobelle arno...@googlemail.com wrote: Duncan Booth duncan.bo...@invalid.invalid writes: Duncan Booth duncan.bo...@invalid.invalid wrote: John Posner jjpos...@snet.net wrote: Do know what in the itertools implementation causes adding a 'if p = sqrt(n)' clause

Re: Re: Generators/iterators, Pythonicity, and primes

2009-04-12 Thread Duncan Booth
John Posner jjpos...@snet.net wrote: Do know what in the itertools implementation causes adding a 'if p = sqrt(n)' clause to *decrease* performance, while adding a 'takewhile()' clause *increases* performance? I haven't timed it, but I would guess that the takewhile was faster only because

Re: Generators/iterators, Pythonicity, and primes

2009-04-12 Thread Arnaud Delobelle
Duncan Booth duncan.bo...@invalid.invalid writes: John Posner jjpos...@snet.net wrote: Do know what in the itertools implementation causes adding a 'if p = sqrt(n)' clause to *decrease* performance, while adding a 'takewhile()' clause *increases* performance? I haven't timed it, but I

Re: Re: Re: Generators/iterators, Pythonicity, and primes

2009-04-12 Thread John Posner
Duncan Booth wrote: John Posner jjpos...@snet.net wrote: Do know what in the itertools implementation causes adding a 'if p = sqrt(n)' clause to *decrease* performance, while adding a 'takewhile()' clause *increases* performance? I haven't timed it, but I would guess that the

Re: Re: Generators/iterators, Pythonicity, and primes

2009-04-12 Thread Duncan Booth
Duncan Booth duncan.bo...@invalid.invalid wrote: John Posner jjpos...@snet.net wrote: Do know what in the itertools implementation causes adding a 'if p = sqrt(n)' clause to *decrease* performance, while adding a 'takewhile()' clause *increases* performance? I haven't timed it, but I

Re: Generators/iterators, Pythonicity, and primes

2009-04-11 Thread Arnaud Delobelle
John Posner jjpos...@snet.net writes: Inspired by recent threads (and recalling my first message to Python edu-sig), I did some Internet searching on producing prime numbers using Python generators. Most algorithms I found don't go for the infinite, contenting themselves with list all the

Re: Re: Generators/iterators, Pythonicity, and primes

2009-04-11 Thread John Posner
Arnaud Delobelle wrote: You could do something like this with the help of itertools.ifilter: prime_gen = ifilter( lambda n, P=[]: all(n%p for p in P) and not P.append(n), count(2) ) Formidable! (both the English and French meanings) This is the most elegant, Sieve of

Re: Generators/iterators, Pythonicity, and primes

2009-04-05 Thread Steven D'Aprano
On Sun, 05 Apr 2009 00:28:17 -0400, Miles wrote: def prime_gen(): primes = [] return (primes.append(n) or n for n in count(2) if all(n%p for p in primes if p=sqrt(n))) That version is only marginally faster than your original version. The biggest performance penalty is that the

Re: Generators/iterators, Pythonicity, and primes

2009-04-05 Thread John Posner
Kay Schluehr said: g = (lambda primes = []: (n for n in count(2) \ if (lambda n, primes: (n in primes if primes and n=primes[-1] \ else (primes.append(n) or True \ if all(n%p for p in primes if p = sqrt(n)) \

Re: Generators/iterators, Pythonicity, and primes

2009-04-05 Thread Kay Schluehr
On 5 Apr., 17:14, John Posner jjpos...@snet.net wrote: Kay Schluehr said: g = (lambda primes = []: (n for n in count(2) \ if (lambda n, primes: (n in primes if primes and n=primes[-1] \ else (primes.append(n) or True \

RE: Generators/iterators, Pythonicity, and primes

2009-04-05 Thread Nick Stinemates
:37 AM To: python-list@python.org Subject: Re: Generators/iterators, Pythonicity, and primes On 5 Apr., 17:14, John Posner jjpos...@snet.net wrote: Kay Schluehr said: g = (lambda primes = []: (n for n in count(2) \ if (lambda n, primes: (n in primes if primes

Re: Generators/iterators, Pythonicity, and primes

2009-04-05 Thread John Posner
Kay Schluehr wrote: That's because it is *one* expression. The avoidance of named functions makes it look obfuscated or prodigious. Once it is properly dissected it doesn't look that amazing anymore. Start with: (n for n in count(2) if is_prime(n, primes)) The is_prime function has

Re: Generators/iterators, Pythonicity, and primes

2009-04-05 Thread Kay Schluehr
On 5 Apr., 18:47, John Posner jjpos...@snet.net wrote: Kay Schluehr wrote: That's because it is *one* expression. The avoidance of named functions makes it look obfuscated or prodigious. Once it is properly dissected it doesn't look that amazing anymore. Start with: (n for n

Re: Generators/iterators, Pythonicity, and primes

2009-04-05 Thread John Posner
g = (lambda primes = []: (n for n in count(2) if (lambda x, primes: (primes.append(x) or True if all(x%p for p in primes if p = sqrt(x)) else False) )(n, primes) ) )() This is

Re: Generators/iterators, Pythonicity, and primes

2009-04-05 Thread Scott David Daniels
Steven D'Aprano wrote: On Sun, 05 Apr 2009 00:28:17 -0400, Miles wrote: def prime_gen(): ... That's pretty sweet, but we can make it even faster. We can speed things up by incrementing by two instead of one, to avoid pointlessly testing even numbers that we know must fail. We can also speed

Generators/iterators, Pythonicity, and primes

2009-04-04 Thread John Posner
Inspired by recent threads (and recalling my first message to Python edu-sig), I did some Internet searching on producing prime numbers using Python generators. Most algorithms I found don't go for the infinite, contenting themselves with list all the primes below a given number. Here's a very

Re: Generators/iterators, Pythonicity, and primes

2009-04-04 Thread Mark Tolonen
John Posner jjpos...@snet.net wrote in message news:af9fbcc3a7624599a6f51bad2397e...@amdup... Inspired by recent threads (and recalling my first message to Python edu-sig), I did some Internet searching on producing prime numbers using Python generators. Most algorithms I found don't go for

RE: Generators/iterators, Pythonicity, and primes

2009-04-04 Thread John Posner
Mark Tolonen said: p = sqrt(n) works a little better :^) -Mark Right you are -- I found that bug in my last-minute check, and then I forgot to trannscribe the fix into the email message. Duh -- thanks! -John E-mail message checked by Spyware Doctor (6.0.0.386) Database version:

Re: Generators/iterators, Pythonicity, and primes

2009-04-04 Thread Terry Reedy
John Posner wrote: Inspired by recent threads (and recalling my first message to Python edu-sig), I did some Internet searching on producing prime numbers using Python generators. Most algorithms I found don't go for the infinite, contenting themselves with list all the primes below a given

Re: Generators/iterators, Pythonicity, and primes

2009-04-04 Thread Miles
On Sat, Apr 4, 2009 at 2:50 PM, John Posner wrote: Inspired by recent threads (and recalling my first message to Python edu-sig), I did some Internet searching on producing prime numbers using Python generators. Most algorithms I found don't go for the infinite, contenting themselves with list

Re: Generators/iterators, Pythonicity, and primes

2009-04-04 Thread Kay Schluehr
Question: Is there a way to implement this algorithm using generator expressions only -- no yield statements allowed? Yes. Avoiding the yield statement is easy but one might eventually end up with two statements because one has to produce a side effect on the primes list. However we can use