Re: [Tutor] primes - sieve of odds

2005-03-26 Thread Sean Perry
John Miller wrote: How does one actually use this module? For example: >>> import eratosthenes >>> eratosthenes.primes() >>> eratosthenes.primes().next() 2 >>> eratosthenes.primes().next() 2 >>> eratosthenes.primes().next() 2 How does one get beyond the first prime? it = eratosthenes.primes()

Re: [Tutor] primes - sieve of odds

2005-03-26 Thread John Miller
On Mar 24, 2005, at 6:01 AM, C Smith <[EMAIL PROTECTED]> wrote: What follows is an attempt based on the previous tutor-evolved sieve that extends the range in which to find the next prime by a factor of 2 over the last known prime. A similar algorithm is on ASPN (I bellieve), under Space-efficient

Re: [Tutor] primes - sieve of odds

2005-03-23 Thread C Smith
Now it would be fine to have an *equally fast* infinite prime number generator. Has anybody any suggestions? [cut] What follows is an attempt based on the previous tutor-evolved sieve that extends the range in which to find the next prime by a factor of 2 over the last known prime. A similar alg

Re: [Tutor] primes - sieve of odds

2005-03-23 Thread C Smith
On Wednesday, Mar 23, 2005, at 04:00 America/Chicago, [EMAIL PROTECTED] wrote: Now it would be fine to have an *equally fast* infinite prime number generator. Has anybody any suggestions? I think when you add the condition of it being an infinite generator, you are changing the rules and can't e

Re: [Tutor] primes - sieve of odds

2005-03-23 Thread Gregor Lingl
C Smith schrieb: Hi Gregor, I had done the same thing. I also noted that assigning (or inserting) an element into a list is faster than creating a new list: l.insert(0,2) is faster than l = [2]+l. ### def sieve (maximum): if maximum < 2: return [] limit = int(maximum**0.5) nums

Re: [Tutor] primes - sieve of odds

2005-03-22 Thread C Smith
Hi Gregor, I had done the same thing. I also noted that assigning (or inserting) an element into a list is faster than creating a new list: l.insert(0,2) is faster than l = [2]+l. ### def sieve (maximum): if maximum < 2: return [] limit = int(maximum**0.5) nums = range(1,maximum+

Re: [Tutor] primes - sieve of odds

2005-03-20 Thread Gregor Lingl
Hi Sean! Thanks for your measurements. In the meantime I did another amendment, leaving out the even numbers from the sieve. It goes like this: def sieve(maximum): nums = range(3, maximum+1, 2) for p in nums: if p: if p*p > maximum: break start = (p*p-2)//2

Re: [Tutor] primes (generator)

2005-03-20 Thread Sean Perry
Gregor Lingl wrote: The following variation of your sieve, using extended slice assignment seems to be sgnificantly faster. Try it out, if you like. Here are the numbers: Primes 1 - 1,000,000 timing: extendedSliceSieve: 0.708388 seconds listPrimes: 0.998758 seconds karlSi

Re: [Tutor] primes (generator)

2005-03-20 Thread Gregor Lingl
Karl Pflästerer schrieb: On 19 Mrz 2005, [EMAIL PROTECTED] wrote: [Code] Maybe sombody likes... I did it ... : def sieve (max): max = max + 1 border = round(max**0.5) nums = range(0, max) nums[0] = nums[1] = None for p in nums: if p: if p >= border: break

Re: [Tutor] primes (generator)

2005-03-19 Thread Karl Pflästerer
On 19 Mrz 2005, [EMAIL PROTECTED] wrote: [Code] > Maybe sombody likes to compare these algorithms to the > ones mentioned before in this thread. I would be > interested in the results. I did it and on my machine here (a slow one) the following straight forward version is the fastest one. It's no

[OT] Re: [Tutor] primes (Cautious remark on Python and Scheme)

2005-03-18 Thread Brian van den Broek
Gregor Lingl said unto the world upon 2005-03-18 19:57: Hi Danny! Preliminary remark: I know that "beautiful" and "beauty" are concepts very seldom applied to computer programs or programming languages. I suppose mainly because they are to a large extent a matter of taste ... Hi Gregor and all, Tho

Re: [Tutor] primes

2005-03-18 Thread Sean Perry
Pierre Barbier de Reuille wrote: def sieve( max ): max_val = int( sqrt( max ) ) s = Set(xrange( 4, max, 2 )) for i in xrange( 3, max_val, 2 ): s.update( xrange( 2*i, max, i ) ) return [ i for i in xrange( 2, max ) if i not in s ] just for grins, here is the same (mostly) code in haskell

Re: [Tutor] primes (Cautious remark on Python and Scheme)

2005-03-18 Thread Gregor Lingl
Concerning my previous posting - perhaps it would suffice to summarize: While Python is a language for the "pragmatic" lover, Scheme is much more "fundamentalistic". (thought-) *PROVOKING*? Gregor ___ Tutor maillist - Tutor@python.org http://mail.pytho

Re: [Tutor] primes (Cautious remark on Python and Scheme)

2005-03-18 Thread Gregor Lingl
Hi Danny! Preliminary remark: I know that "beautiful" and "beauty" are concepts very seldom applied to computer programs or programming languages. I suppose mainly because they are to a large extent a matter of taste ... Nevertheless: regardeless of the fact that I'm not a very skilled scheme-progr

Re: [Tutor] primes (generator)

2005-03-18 Thread Gregor Lingl
Hi all of you! Many thanks for your remarks, ideas and experiments. When I posted my input yesterday: [x for x in range(2,100) if not [y for y in range(2,x) if x%y==0]] my intention was indeed to find a "shortest" a program, disregarding efficiency. Thus range instead of xrange etc. (About one year

Re: [Tutor] primes

2005-03-18 Thread Pierre Barbier de Reuille
Gregor Lingl a écrit : Hi! Who knows a more concise or equally concise but more efficient expression, which returns the same result as [x for x in range(2,100) if not [y for y in range(2,x) if x%y==0]] Gregor P.S.: ... or a more beautiful one ;-) ___ Tuto

Re: [Tutor] primes

2005-03-18 Thread Max Noel
On Mar 18, 2005, at 02:15, Kent Johnson wrote: Max Noel wrote: #!/usr/bin/env python import math import timeit def primeConcise(limit): return [2] + [x for x in xrange(3, limit, 2) if not [y for y in [2] + range(3,x,2) if x%y==0]] def primeConciseOptimized(limit): return [2] + [x for x in

Re: [Tutor] primes

2005-03-17 Thread Sean Perry
Danny Yoo wrote: Here is one that's traduced... er... adapted from material from the classic textbook "Structure and Interpretation of Computer Programs": ## from itertools import ifilter, count def notDivisibleBy(n): ... def f(x): ... return x % n != 0 ... return f ... def siev

Re: [Tutor] primes

2005-03-17 Thread C Smith
On Thursday, Mar 17, 2005, at 17:49 America/Chicago, [EMAIL PROTECTED] wrote: On my system, it took 415 seconds to generate a list of primes < 50,000 using range, but only 386 seconds if I use the same code, but with xrange instead. If you only calculate y up to sqrt(x) you will see a dramatic

Re: [Tutor] primes

2005-03-17 Thread Kent Johnson
Max Noel wrote: #!/usr/bin/env python import math import timeit def primeConcise(limit): return [2] + [x for x in xrange(3, limit, 2) if not [y for y in [2] + range(3,x,2) if x%y==0]] def primeConciseOptimized(limit): return [2] + [x for x in xrange(3, limit, 2) if not [y for y in [2] +

Re: [Tutor] primes

2005-03-17 Thread Max Noel
On Mar 17, 2005, at 23:54, Danny Yoo wrote: Hi Gregor, Here is one that's traduced... er... adapted from material from the classic textbook "Structure and Interpretation of Computer Programs": Ooh, nifty. Okay, I decided to learn how to use the timeit module, so I used it to compare my algor

Re: [Tutor] primes

2005-03-17 Thread Danny Yoo
On Thu, 17 Mar 2005, Gregor Lingl wrote: > Hi! > Who knows a more concise or equally concise but more efficient > expression, which returns the same result as > > [x for x in range(2,100) if not [y for y in range(2,x) if x%y==0]] Hi Gregor, Here is one that's traduced... er... adapted from ma

Re: [Tutor] primes

2005-03-17 Thread jfouhy
Quoting Gregor Lingl <[EMAIL PROTECTED]>: > [x for x in range(2,100) if not [y for y in range(2,x) if x%y==0]] Heh. That's quite neat. I can only offer one suggestion --- if you replace range with xrange, you get a small speed improvement. eg: On my system, it took 415 seconds to generate a li

Re: [Tutor] primes

2005-03-17 Thread Max Noel
On Mar 17, 2005, at 22:28, Gregor Lingl wrote: Hi! Who knows a more concise or equally concise but more efficient expression, which returns the same result as [x for x in range(2,100) if not [y for y in range(2,x) if x%y==0]] Gregor P.S.: ... or a more beautiful one ;-) Hmm... I don't have "beauti