[Edu-sig] Generating prime numbers

John Posner jjposner at snet.net
Sun Apr 5 17:27:53 CEST 2009


kirby urner wrote:
> Excellente! kind sir.
>   
Here's a Python solution from Kay Schluehr on python-list. I've 
pretty-printed it, sort of. If the line endings get garbled, just look 
for the backslashes:

g = (lambda primes = []: \
        (n for n in count(2) if \
            (lambda n, primes: \
                (n in primes if (primes and n<=primes[-1]) \
                             else (primes.append(n) or True if all(n%p 
for p in primes if p <= sqrt(n)) \
                                                            else False) \
                ) \
            )(n, primes) \
        ) \
    )()

"g" is a generator, so get the values with g.next().


> On Sat, Apr 4, 2009 at 2:30 PM, Edward Cherlin <echerlin at gmail.com> wrote:
>   
>> Your message text got stuck in an attachment, where I almost missed
>> it. I wonder what your mail system thinks it is doing? Comment below.
>>     
Yes, I know it's bad. This message is sent from Thunderbird 2.0.0.21, 
instead of MS-Outlook 2003. Any better?

-John



More information about the Edu-sig mailing list