Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #50363
| Path | csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder3.xlned.com!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!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.001 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'algorithm': 0.04; '21,': 0.07; 'explicit': 0.07; 'wednesday,': 0.07; '22,': 0.09; '[1]:': 0.09; '[2]:': 0.09; '[3]:': 0.09; 'marking': 0.09; 'methods,': 0.09; 'subject:number': 0.09; 'question.': 0.14; 'dictionary.': 0.16; 'division.': 0.16; 'doubles': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'lambda': 0.16; 'loop.': 0.16; 'multiples': 0.16; 'readability': 0.16; 'simple.': 0.16; 'subject:Prime': 0.16; 'subject:generator': 0.16; '{2:': 0.16; 'wrote:': 0.18; 'trying': 0.19; 'thu,': 0.19; 'unlike': 0.19; 'not,': 0.20; 'memory': 0.22; 'otherwise,': 0.22; 'stick': 0.24; 'first,': 0.26; 'pass': 0.26; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'chris': 0.29; 'am,': 0.29; 'start,': 0.30; 'message-id:@mail.gmail.com': 0.30; 'figure': 0.32; 'run': 0.32; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'picking': 0.36; 'next': 0.36; 'thanks': 0.36; "i'll": 0.36; 'so,': 0.37; 'list.': 0.37; 'performance': 0.37; 'improving': 0.38; 'to:addr:python-list': 0.38; 'fact': 0.38; 'does': 0.39; 'bad': 0.39; 'to:addr:python.org': 0.39; 'either': 0.39; 'skip:u 10': 0.60; 'july': 0.63; 'total': 0.65; '20,': 0.68; 'records': 0.73; 'jul': 0.74; 'lowest': 0.74; 'prime': 0.74; '11:': 0.84; '13:': 0.84; 'etc,': 0.84; 'moves': 0.84; 'severe': 0.91; '2013': 0.98 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=Jp5iYq3w9SQnC2eqWY23oJRUokj+Ng/VjUjlnC/46T0=; b=szv2rJdh8sEZcfJsXu8O0lvPei2JaXDH4cRgughFkcITefGV/y3fwg+tqokABcy+CJ sQHDRumXZ8r4V1VvVOY/syXu17gWL9RD2PaqokDPSlBuKfchkDSSuYxG0RT8jMLljq2z VsDLz9vFZ34gEIpXTVw99fphqTGKkUTy742teUkuCqS1kOR+QIXIKrhOLg7j8QAh+Rqk heMUUJFwBmUXmLH75B5IiLluwWqeWdK3dRWP1Q7Uxgkmc1lCUvSSozosrAEvnJMWfkk8 E7vbPsYC2AhL8Yw82MZ4SBGLlKIuLSzgg8t6LBxVHlH4HUCXdIjAAmyCkQX7LwuZNre6 FbxA== |
| MIME-Version | 1.0 |
| X-Received | by 10.220.182.193 with SMTP id cd1mr19482074vcb.32.1373469139139; Wed, 10 Jul 2013 08:12:19 -0700 (PDT) |
| In-Reply-To | <15167633-b6e7-46cc-a043-8dfe8aaad11e@googlegroups.com> |
| References | <mailman.4522.1373464867.3114.python-list@python.org> <15167633-b6e7-46cc-a043-8dfe8aaad11e@googlegroups.com> |
| Date | Thu, 11 Jul 2013 01:12:19 +1000 |
| Subject | Re: 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.4525.1373469147.3114.python-list@python.org> (permalink) |
| Lines | 36 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1373469147 news.xs4all.nl 15992 [2001:888:2000:d::a6]:56329 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:50363 |
Show key headers only | View raw
On Thu, Jul 11, 2013 at 12:35 AM, Bas <wegwerp@gmail.com> wrote:
> On Wednesday, July 10, 2013 4:00:59 PM UTC+2, Chris Angelico wrote:
> [...]
>> 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.
>
> In [1]: prime = {2: 20, 3: 18, 5: 20, 7: 21, 11: 22, 13: 26}
>
> In [2]: smallest_key = min(prime.iteritems(), key=lambda k_v: k_v[1])[0]
>
> In [3]: smallest_key
> Out[3]: 3
Well, that does answer the question. Unfortunately the use of lambda
there has a severe performance cost (roughly doubles the total run
time, when I ask for the thousandth prime), without majorly improving
readability. I'll bear it in mind if there's a way to make that work
on either readability or performance, but otherwise, I'll stick with
the explicit loop. Thanks anyway!
> Still trying to figure out your algorithm ...
It's pretty simple. (That's a bad start, I know!) Like the Sieve of
Eratosthenes, it locates prime numbers, then deems every multiple of
them to be composite. Unlike the classic sieve, it does the "deem"
part in parallel. Instead of marking all the multiples of 2 first,
then picking three and marking all the multiples of 3, then 5, etc,
this function records the fact that it's up to (say) 42 in marking
multiples of 2, and then when it comes to check if 43 is prime or not,
it moves to the next multiple of 2. This requires memory to store the
previously-known primes, similarly to other methods, but needs no
multiplication or division.
ChrisA
Back to comp.lang.python | Previous | Next — Previous in thread | 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