Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #50357
| Path | csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <rosuav@gmail.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.008 |
| X-Spam-Evidence | '*H*': 0.98; '*S*': 0.00; 'algorithm': 0.04; '21,': 0.07; 'bootstrap': 0.07; 'smallest': 0.07; '22,': 0.09; 'generators': 0.09; 'here?': 0.09; 'key.': 0.09; 'subject:number': 0.09; 'python': 0.11; 'def': 0.12; '3.3,': 0.16; '__future__': 0.16; 'dictionary.': 0.16; 'division,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'mapped': 0.16; 'reinvent': 0.16; 'subject:Prime': 0.16; 'subject:generator': 0.16; 'true:': 0.16; '{2:': 0.16; 'library': 0.18; '(but': 0.19; 'import': 0.22; 'finally,': 0.24; 'instance,': 0.24; 'fine': 0.24; 'sort': 0.25; 'compare': 0.26; 'pass': 0.26; 'asking': 0.27; 'values': 0.27; 'to?': 0.30; 'message-id:@mail.gmail.com': 0.30; 'code': 0.31; '13,': 0.31; 'there.': 0.32; 'probably': 0.32; 'run': 0.32; 'quite': 0.32; 'something': 0.35; 'equal': 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; 'so,': 0.37; 'two': 0.37; 'list.': 0.37; 'star': 0.38; 'to:addr:python-list': 0.38; 'track': 0.38; 'to:addr:python.org': 0.39; 'either': 0.39; 'skip:p 20': 0.39; 'even': 0.60; 'entire': 0.61; 'first': 0.61; 'name': 0.63; 'map': 0.64; 'become': 0.64; 'series': 0.66; '20,': 0.68; 'lowest': 0.74; 'prime': 0.74; 'lack': 0.78; '11:': 0.84; '13:': 0.84; 'benchmark': 0.84; 'different.': 0.84; 'have?': 0.84; 'wheel': 0.84; 'composite': 0.91; 'notable': 0.91 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:date:message-id:subject:from:to:content-type; bh=lPkoWgEs3IoegIvu/R+V/mUdB97zTNO3r4EsWwTCz/c=; b=M5eJ002M6kewE7baWn0tffp1qwdwCdX2Wj3SATAaNRV6aWCjvHt8gGHgs4mEJ8aQNj ts6QfEZT+Eomb/6a6ewHA8bgNCj2IaIiOpbyWbc2aVRM9DRd7nJcr4aOjV7//DIgoJeM KtKggmoz7IIklicu6yiACwA2gY1ODf6QE/5f4IMxRQafJwx1a6ojTrL/RqNnWXEp9JNc +L0wuHheIDSA0x5ZvTQSw4rJKrXtsQtnU2S+9a1kEuELHwjHCQK0zSMAqwQ2HABZOxQZ l6SDUI7Sau1nr7puFwN7GJcd2JOKlhVwIozRjtwF/3l6bP2ewQMz30nfRYLJB+zn2ODf fj0w== |
| MIME-Version | 1.0 |
| X-Received | by 10.220.48.17 with SMTP id p17mr18532500vcf.97.1373464859456; Wed, 10 Jul 2013 07:00:59 -0700 (PDT) |
| Date | Thu, 11 Jul 2013 00:00:59 +1000 |
| Subject | Prime number generator |
| From | Chris Angelico <rosuav@gmail.com> |
| To | python-list@python.org |
| Content-Type | text/plain; charset=ISO-8859-1 |
| 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.4522.1373464867.3114.python-list@python.org> (permalink) |
| Lines | 53 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1373464867 news.xs4all.nl 15996 [2001:888:2000:d::a6]:56391 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:50357 |
Show key headers only | View raw
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? 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? 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? And finally, whose wheel
did I reinvent here? What name would this algorithm have?
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 primes():
"""Generate an infinite series of prime numbers."""
i=2
yield 2
prime={2:2} # Map a prime number to its next composite (but bootstrap with 2:2)
while True:
# Find the smallest value in prime[] and its key.
# Is there a standard library way to do this??
# (If two values are equal smallest, either can be returned.)
prm=None
for p,val in prime.items():
if prm is None or val<smallest:
prm,smallest=p,val
prime[prm]+=prm
while i<smallest:
yield i
prime[i]=i+i
i+=1
if i==smallest: i+=1
gen=primes()
for i in range(30):
print(next(gen),end="\t") # Star Trek?
print()
# -- end --
Back to comp.lang.python | Previous | Next — Next in thread | Find similar | Unroll thread
Prime number generator Chris Angelico <rosuav@gmail.com> - 2013-07-11 00:00 +1000
Re: Prime number generator Bas <wegwerp@gmail.com> - 2013-07-10 07:35 -0700
Re: Prime number generator Chris Angelico <rosuav@gmail.com> - 2013-07-11 01:12 +1000
Re: Prime number generator bas <blswinkels@gmail.com> - 2013-07-10 08:47 -0700
Re: Prime number generator Chris Angelico <rosuav@gmail.com> - 2013-07-11 02:15 +1000
Re: Prime number generator Joshua Landau <joshua@landau.ws> - 2013-07-10 18:47 +0100
Re: Prime number generator Ian Kelly <ian.g.kelly@gmail.com> - 2013-07-10 12:56 -0600
Re: Prime number generator Joshua Landau <joshua@landau.ws> - 2013-07-10 20:06 +0100
Re: Prime number generator bryanjugglercryptographer@yahoo.com - 2013-07-30 21:57 -0700
Re: Prime number generator Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-07-10 16:01 +0000
Re: Prime number generator Chris Angelico <rosuav@gmail.com> - 2013-07-11 02:52 +1000
Re: Prime number generator albert@spenarnc.xs4all.nl (Albert van der Horst) - 2013-07-30 10:58 +0000
Re: Prime number generator Ian Kelly <ian.g.kelly@gmail.com> - 2013-07-30 11:33 -0600
csiph-web