Shortest prime number program

Ian Bygrave ian at bygrave.me.uk
Sat Feb 11 08:33:58 EST 2006


On Sat, 11 Feb 2006 12:43:23 +0000, Ian Bygrave wrote:
> p,r=[],range(2,99)
> while r:p,r=p+r[:1],[x for x in r if x%r[0]]
> 
> And the result's in p.

Well, given a hypothetical new function 'sieve'

def sieve(f,l):
    if not l:
        return l
    head,tail=l[0],l[1:]
    def filter_func(x):
        return f(x,head)
    tail=filter(filter_func,tail)
    return [head]+tail

The prime generation can be reduced to:

from operator import *
sieve(mod,range(2,99))

Is there any precedent for such a function, or any other uses?
 
--Ian Bygrave




More information about the Python-list mailing list