Re: [Tutor] what? (late nite entertainment) (*)
Now: early morning corrections ... Alfred Milgrom wrote:
Hi Gregor:
Hard to work out what is what :) as there seems to be a typo in one line of your code:
if x[z]: x[z*z:n:z]=y[z*z:n:z]
(At least it doesn't work on my machine with Python 2.2)
Yeah, I had the idea when playing around with some new features of Python 2.3, as I do sometimes late at night - before shutting down my computer. This one I found at: http://www.python.org/doc/2.3.3/whatsnew/section-slices.html and I looked for an example for use of "assignment with extended slices" ...
Also the subroutine breaks for n = 1 or less. Maybe you need a line at the start to say if n <2 : return [0]
what() was intended to generate all primes less than n. (In fact it's an implementation of the sieve of Eratosthenes). The bugs came not only from not beeing interested in primes less than 2 ;-) but also from beeing a little tired already. Sorry. I think the following version repairs that - and hopefully uses less obfuscating variable names (which in fact originally was part of the "riddle"): def primes(n): sieve, zeros = range(n), [0]*n sieve[:2]= 0,0 i = 1 while i*i < n: if sieve[i]: sieve[i*i:n:i]=zeros[i*i:n:i] i += 1 return [p for p in sieve if p] Regards, Gregor
def primes(n): sieve, zeros = range(n), [0]*n sieve[:2]= 0,0 i = 1 while i*i < n: if sieve[i]: sieve[i*i:n:i]=zeros[i*i:n:i] i += 1 return [p for p in sieve if p]
Regards, Gregor
Very nice. Here's a slight variation: def primes(n): sieve = [1]*n sieve[:2]= 0,0 i = 1 while i*i < n: if sieve[i]: sieve[i*i:n:i] = len(sieve[i*i:n:i]) * [0] i += 1 return [p for p in range(n) if sieve[p]] Kirby
Thank you for this little puzzle intro to extended slice notation. I didn't fully understand what it was doing until I walked through it line by line in the interpreter and inserted a print statement for each step in the while loop. Now, I won't claim to be a mathmetician (I'm not even sure I spelled it correctly), and so won't chime in on the 1 being prime issue, but if you wanted to include it in the return - you could do the following: def primes(n): sieve, zeros = range(n), [0]*n i = 2 while i*i < n: if sieve[i]: sieve[i*i:n:i]=zeros[i*i:n:i] i += 1 return [p for p in sieve if p] Trent
what() was intended to generate all primes less than n. (In fact it's an implementation of the sieve of Eratosthenes). The bugs came not only from not beeing interested in primes less than 2 ;-) but also from beeing a little tired already. Sorry.
I think the following version repairs that - and hopefully uses less obfuscating variable names (which in fact originally was part of the "riddle"):
def primes(n): sieve, zeros = range(n), [0]*n sieve[:2]= 0,0 i = 1 while i*i < n: if sieve[i]: sieve[i*i:n:i]=zeros[i*i:n:i] i += 1 return [p for p in sieve if p]
Regards, Gregor
def primes(n): sieve, zeros = range(n), [0]*n sieve[:2]= 0,0 i = 1 while i*i < n: if sieve[i]: sieve[i*i:n:i]=zeros[i*i:n:i] i += 1 return [p for p in sieve if p]
Regards, Gregor
Very nice. Here's a slight variation: def primes(n): sieve = [1]*n sieve[:2]= 0,0 i = 1 while i*i < n: if sieve[i]: sieve[i*i:n:i] = len(sieve[i*i:n:i]) * [0] i += 1 return [p for p in range(n) if sieve[p]] Kirby _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
participants (3)
-
Gregor Lingl
-
Kirby Urner
-
Trent Oliphant