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: 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 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 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