Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #50365

Re: Prime number generator

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <joshua.landau.ws@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.016
X-Spam-Evidence '*H*': 0.97; '*S*': 0.00; 'algorithm': 0.04; '21,': 0.07; 'bootstrap': 0.07; 'smallest': 0.07; '22,': 0.09; 'generators': 0.09; 'here?': 0.09; 'repeated': 0.09; 'subject:number': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'def': 0.12; '2*i': 0.16; '3.3,': 0.16; '__future__': 0.16; 'dict': 0.16; 'dictionary.': 0.16; 'division,': 0.16; 'mapped': 0.16; 'reinvent': 0.16; 'subject:Prime': 0.16; 'subject:generator': 0.16; 'true:': 0.16; '{2:': 0.16; 'sender:addr:gmail.com': 0.17; 'wrote:': 0.18; '(but': 0.19; 'import': 0.22; 'cc:addr:python.org': 0.22; 'finally,': 0.24; 'instance,': 0.24; 'fine': 0.24; 'cc:2**0': 0.24; 'sort': 0.25; "i've": 0.25; 'compare': 0.26; 'pass': 0.26; 'asking': 0.27; 'header:In-Reply-To:1': 0.27; 'chris': 0.29; 'to?': 0.30; 'message-id:@mail.gmail.com': 0.30; 'code': 0.31; '13,': 0.31; 'though.': 0.31; 'there.': 0.32; 'probably': 0.32; 'run': 0.32; 'quite': 0.32; 'older': 0.33; '"the': 0.34; 'something': 0.35; 'test': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'yield': 0.36; 'done': 0.36; 'next': 0.36; "i'll": 0.36; 'effort': 0.37; 'so,': 0.37; 'list.': 0.37; 'minimum': 0.38; 'mine': 0.38; 'star': 0.38; 'track': 0.38; 'skip:p 20': 0.39; 'even': 0.60; 'is.': 0.60; 'entire': 0.61; 'course': 0.61; 'first': 0.61; 'name': 0.63; 'july': 0.63; 'skip:n 10': 0.64; 'map': 0.64; 'more': 0.64; 'to:addr:gmail.com': 0.65; 'series': 0.66; '20,': 0.68; 'lowest': 0.74; 'now:': 0.74; 'prime': 0.74; 'lack': 0.78; '11:': 0.84; '13:': 0.84; 'benchmark': 0.84; 'different.': 0.84; 'have?': 0.84; 'proves': 0.84; 'wheel': 0.84; 'composite': 0.91; 'notable': 0.91; '2013': 0.98
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:sender:in-reply-to:references:from:date :x-google-sender-auth:message-id:subject:to:cc:content-type; bh=HQymwMpyRXeAXVnnWogr9rzQh5zEqnL9NUHkW8bXZKg=; b=xtn43OWB5EJPedT3l5pBDo0iTelV/4vbiXvsPYngmT909SXmQ3O98aGGI2rXcjuFK9 7Pym1OItkSdJ7BTGw4Ndk4TrKy9JLjQ65m+mIXasKn9Dc6nk2z8G0csR7JgwEWQhD/a7 9gMeu6RYM9ZpOvZDRGDlB1fVx8rGpHO3HUDLQ/9cfOQj64lJmy8NvdJ+sb19AKwTFZiA h91wVi9Vfh2+dzVkPao85p95o1Jj4YEX/2mujFPtysGfAc170r25nwGmLRGNeW/Tnu1/ N0T0/SNaXqjFZcFYWk8sU0h60csjIBe5lzqAQFiHuAoVPLn423qKvTeOlOM9bjlCmVlj Ejag==
X-Received by 10.112.182.39 with SMTP id eb7mr15173048lbc.30.1373471052914; Wed, 10 Jul 2013 08:44:12 -0700 (PDT)
MIME-Version 1.0
Sender joshua.landau.ws@gmail.com
In-Reply-To <CAPTjJmq_7+rtNV22cxpLXQ+G1XfCyJvKckN+2B3PjfVEEs-5Qw@mail.gmail.com>
References <CAPTjJmq_7+rtNV22cxpLXQ+G1XfCyJvKckN+2B3PjfVEEs-5Qw@mail.gmail.com>
From Joshua Landau <joshua@landau.ws>
Date Wed, 10 Jul 2013 16:43:32 +0100
X-Google-Sender-Auth 5q2AhiRRt977S95bjA_xgIiSBmA
Subject Re: Prime number generator
To Chris Angelico <rosuav@gmail.com>
Content-Type text/plain; charset=UTF-8
Cc python-list <python-list@python.org>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.4527.1373471055.3114.python-list@python.org> (permalink)
Lines 98
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1373471055 news.xs4all.nl 15948 [2001:888:2000:d::a6]:42582
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:50365

Show key headers only | View raw


On 10 July 2013 15:00, Chris Angelico <rosuav@gmail.com> wrote:
> And now for something completely different.
>
> I knocked together a prime number generator, just for the fun of it,
> that works like a Sieve of Eratosthenes but unbounded. It keeps track
> of all known primes and the "next composite" that it will produce -
> for instance, after yielding 13, the prime map will be {2: 20, 3: 18,
> 5: 20, 7: 21, 11: 22, 13: 26}, each one mapped to the first multiple
> greater than 13.
>
> Notable in the algorithm is an entire lack of division, or even
> multiplication. Everything is done with addition.
>
> So, a few questions. Firstly, is there a stdlib way to find the key
> with the lowest corresponding value?

Of course there is.

> In the above map, it would return
> 3, because 18 is the lowest value in the list. I want to do this with
> a single pass over the dictionary. Secondly, can the "while
> i<smallest... i+=1" loop become a for...range?

Of course it can.

> It's almost asking for
> it, but not quite there. Thirdly, is there any sort of half-sane
> benchmark that I can compare this code to?

Of course there is. I have no clue what, though.

> And finally, whose wheel
> did I reinvent here?

Mine :P. That just proves the algorithm is older still, though. You
also did it much more neatly and much more slowly, though. The trick
is to avoid that repeated effort of finding the minimum in the dict by
just keeping a sorted list.

I've mocked that up just now:

# Faster code #
from blist import sortedlist

def generate_primes():
    primes = sortedlist()
    primes.add([4, 2])

    yield 2
    i = 3

    while True:
        next_nonprime, prime_factor = primes.pop(0)

        for prime in range(i, next_nonprime):
            yield prime
            primes.add([prime*2, prime])

        i = next_nonprime + 1

        primes.add([next_nonprime + prime_factor, prime_factor])
# No longer code #

> What name would this algorithm have?

I'll call it "the flamingo".

> Code tested on Python 3.3, would probably run fine on pretty much any
> Python that supports yield, though I don't have a Py2.2 to test from
> __future__ import generators on!
>
> ChrisA
>
> # -- start --
def generate_primes():
        """Generate an infinite series of prime numbers."""
        i = 2
        yield 2

        primes = {2:2} # Map a prime number to its next composite (but
bootstrap with 2:2)
        while True:
                prm = min(primes, key=primes.__getitem__)
                smallest = primes[prm]

                primes[prm] += prm

                for prm in range(i, smallest):
                        yield prm
                        primes[i] = 2*i

                i = smallest + 1

gen=generate_primes()
for i in range(30):
        print(next(gen),end="\t") # Star Trek?
print()
> # -- end --

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Prime number generator Joshua Landau <joshua@landau.ws> - 2013-07-10 16:43 +0100

csiph-web