Re: Shortest prime number program
On Sat, 11 Feb 2006 02:03:46 -0800, swisscheese wrote: > I figured someone out there must have written a minimal code size prime > number generator. I did not find one after a bit of searching around. > For primes up to 100 the best I could do was 70 characters (including > spaces): > > r=range(2,99) > m=[x*y for x in r for y in r] > [x for x in r if not x in m] I swore I'd never play Python golf. 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. --Ian Bygrave -- http://mail.python.org/mailman/listinfo/python-list
Re: Shortest prime number program
On Sat, 11 Feb 2006 12:43:23 +, 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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Shortest prime number program
On Sat, 11 Feb 2006 13:33:58 +, Ian Bygrave wrote: > Well, given a hypothetical new function 'sieve' which should have been: 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]+sieve(f,tail) > The prime generation can be reduced to: > > from operator import * > sieve(mod,range(2,99)) --Ian Bygrave -- http://mail.python.org/mailman/listinfo/python-list