[Tutor] Re: primes
André Roberge
andre.roberge at gmail.com
Fri Mar 18 00:59:36 CET 2005
jfouhy at paradise.net.nz wrote:
> Quoting Gregor Lingl <glingl at aon.at>:
>
>
>>[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 list of primes < 50,000
> using range, but only 386 seconds if I use the same code, but with xrange instead.
>
What about using something like range(2, math.sqrt(x)) instead?
More information about the Tutor
mailing list