On 6/14/06, Jamie Wilkinson <[EMAIL PROTECTED]> wrote:
List comprehensions!

>def is_prime(n, primes):
>   """ primes is a list of primes where sqrt(n) <= max(primes) < n"""
>   for p in primes while p*p <= n:
>       if n % p == 0:
>           return False
>   return True
>
>Does this look like a sane piece of syntax to other people? If it
>existed would you use it? Does something like this exist somewhere and
>I'm just really slow?

[p for p in [p in primes if p*p < n] if n % p == 0]

and then fold that all together ;-)

Yeah, you can do that, but your inner loop still iterates over all p
in primes, when you know that once p*p >= n you can stop looking.

Tim

_______________________________________________
coders mailing list
coders@slug.org.au
http://lists.slug.org.au/listinfo/coders

_______________________________________________
coders mailing list
coders@slug.org.au
http://lists.slug.org.au/listinfo/coders

Reply via email to