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
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
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
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
> 42
Make that 41 and 40.
--
http://mail.python.org/mailman/listinfo/python-list
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
> [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
[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
[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
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
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
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 [
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):
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]
>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
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
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
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
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]
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]
--
20 matches
Mail list logo