Re: Generators/iterators, Pythonicity, and primes

2009-04-13 Thread Aaron Brady
On Apr 13, 1:39 am, Arnaud Delobelle wrote: > Duncan Booth writes: > > Duncan Booth wrote: > > >> John Posner wrote: > > >>> Do know what in the itertools implementation causes adding a 'if p <= > >>> sqrt(n)' clause to *decrease* performance, while adding a > >>> 'takewhile()' clause *increase

Re: Generators/iterators, Pythonicity, and primes

2009-04-12 Thread Arnaud Delobelle
Duncan Booth writes: > Duncan Booth wrote: > >> John Posner 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 woul

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

2009-04-12 Thread Duncan Booth
Duncan Booth wrote: > John Posner 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 fa

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

2009-04-12 Thread John Posner
Duncan Booth wrote: John Posner 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 on

Re: Generators/iterators, Pythonicity, and primes

2009-04-12 Thread Arnaud Delobelle
Duncan Booth writes: > John Posner 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 fas

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

2009-04-12 Thread Duncan Booth
John Posner 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 the sqrt(n) ha

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 Eratos

Re: Generators/iterators, Pythonicity, and primes

2009-04-11 Thread Arnaud Delobelle
John Posner 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 primes below a g

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 th

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) >> >

Re: Generators/iterators, Pythonicity, and primes

2009-04-05 Thread Kay Schluehr
On 5 Apr., 18:47, John Posner 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 i

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

RE: Generators/iterators, Pythonicity, and primes

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

Re: Generators/iterators, Pythonicity, and primes

2009-04-05 Thread Kay Schluehr
On 5 Apr., 17:14, John Posner 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 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 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

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

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 "

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 numb

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

Re: Generators/iterators, Pythonicity, and primes

2009-04-04 Thread Mark Tolonen
"John Posner" 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 the infinite, cont