[Python-ideas] Is this PEP-able? for X in ListY while conditionZ:

Rob Cliffe rob.cliffe at btinternet.com
Wed Jul 3 01:17:39 CEST 2013


On 02/07/2013 23:35, Greg Ewing wrote:
> Oscar Benjamin wrote:
>> def primes():
>>     primes_seen = []
>>     for n in count(2):
>>         if all(n % p for p in primes_seen):
>>             yield n
>>             primes_seen.append(n)
>>
>> This algorithm is actually even poorer as it doesn't stop at sqrt(n).
>
> Nor should it! When you're only dividing by primes, you
> can't stop at sqrt(n), you have to divide by *all* the
> primes less than n. Otherise you could miss a prime
> factor greater than sqrt(n) whose cofactor is not prime.
>
> (Not relevant to the original disussion, I know, but
> my inner mathematician couldn't restrain himself.)
>
But you could at least stop at n/4.
Rob Cliffe


More information about the Python-ideas mailing list