
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