Re: Shortest prime number program

2006-02-13 Thread Tim Hochberg
In the spirit of pointless pessimization and obfuscation I have crushed something very similar to Alex Martelli's eratosthenes function onto a single line. It's truly monstrous, but somewhat entertaining [Some preemptive linebreaks added]: def short():import itertools as it;D={};g=D.get;return

Re: Shortest prime number program

2006-02-12 Thread Christoph Zwerschke
Tim Hochberg wrote: > from itertools import count, ifilter > def sieve(s=count(2)): > while 1:p=s.next();s=ifilter(p.__rmod__,s);yield p Nice! -- Christoph -- http://mail.python.org/mailman/listinfo/python-list

Re: Shortest prime number program

2006-02-12 Thread Tim Hochberg
While not nearly the shortest proposed thus far, I'm fond of: from itertools import count, ifilter def sieve(s=count(2)): while 1:p=s.next();s=ifilter(p.__rmod__,s);yield p It will generate quite a large number of primes before blowing up (at least 50,000 primes, p=611,957) and it's much fast

Re: Shortest prime number program

2006-02-11 Thread Ralf Muschall
Christoph Zwerschke wrote: > [EMAIL PROTECTED] schrieb: >> How about: >> [2]+[x for x in range(1,99) if 2**x%x==2] > If the range goes beyond 340, it also gives non-primes... [2,3]+[x for x in range(1,99) if 2**x%x==2 and 3**x%x==3] [2,3,5]+[x for x in range(1,99) if 2**x%x==2 and 3**x%x==3 and

Re: Shortest prime number program

2006-02-11 Thread swisscheese
> 42 Make that 41 and 40. -- http://mail.python.org/mailman/listinfo/python-list

Re: Shortest prime number program

2006-02-11 Thread bearophileHUGS
This is a little shorter :-) [2]+[x for x in range(2,99)if 2**x%x==2] Bye, bearophile -- http://mail.python.org/mailman/listinfo/python-list

Re: Shortest prime number program

2006-02-11 Thread swisscheese
> [2]+[x for x in range(1,99) if 2**x%x==2] 42 - brilliant! 41: [2]+[x for x in range(1,99)if 2**x%x==2] ... although it appears Christoph is right that it's not scalable. -- http://mail.python.org/mailman/listinfo/python-list

Re: Shortest prime number program

2006-02-11 Thread Jeffrey Schwab
[EMAIL PROTECTED] wrote: > swisscheese wrote: > >>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] > > > How about: > > [2]+[x for x in range(1,99) if 2**x%x==2] 43. I'll be chewing on this one for a while. Thank you. :) -- http://mail.python.org/mailman/listinfo

Re: Shortest prime number program

2006-02-11 Thread Christoph Zwerschke
[EMAIL PROTECTED] schrieb: > How about: > > [2]+[x for x in range(1,99) if 2**x%x==2] If the range goes beyond 340, it also gives non-primes... -- Christoph -- http://mail.python.org/mailman/listinfo/python-list

Re: Shortest prime number program

2006-02-11 Thread dickinsm
swisscheese wrote: > > 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] How about: [2]+[x for x in range(1,99) if 2**x%x==2] Mark -- http://mail.python.org/mailman/listinfo/python-list

Re: Shortest prime number program

2006-02-11 Thread Grant Edwards
On 2006-02-11, swisscheese <[EMAIL PROTECTED]> wrote: >>You can save two bytes with > > 56 - nice catch. > 55: > r=range(2,99) > [x for x in r if sum(x%d<1 for d in r)<2] And if this were FORTRAN: r=range(2,99) [xforxinrifsum(x%d<1fordinr)<2] ;) -- Grant Edwards grante

Re: Shortest prime number program

2006-02-11 Thread Ian Bygrave
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 [

Re: Shortest prime number program

2006-02-11 Thread Ian Bygrave
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):

Re: Shortest prime number program

2006-02-11 Thread [EMAIL PROTECTED]
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]

Re: Shortest prime number program

2006-02-11 Thread swisscheese
>You can save two bytes with 56 - nice catch. 55: r=range(2,99) [x for x in r if sum(x%d<1 for d in r)<2] -- http://mail.python.org/mailman/listinfo/python-list

Re: Shortest prime number program

2006-02-11 Thread Martin v. Löwis
You can save two bytes with r=range(2,99) [x for x in r if sum(x%d==0 for d in r)<2] -- http://mail.python.org/mailman/listinfo/python-list

Re: Shortest prime number program

2006-02-11 Thread Ian Bygrave
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

Re: Shortest prime number program

2006-02-11 Thread swisscheese
At 58, very nice :-) Building on yours we get 57: r=range(2,99) [x for x in r if sum([x%d==0 for d in r])<2] -- http://mail.python.org/mailman/listinfo/python-list

Re: Shortest prime number program

2006-02-11 Thread Mitja Trampus
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]

Shortest prime number program

2006-02-11 Thread swisscheese
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] --