Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!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.006 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; '[0]': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'toss': 0.09; 'def': 0.12; 'jan': 0.12; 'internally': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'rem': 0.16; 'skip:t 100': 0.16; 'subject:search': 0.16; 'wrote:': 0.18; 'slightly': 0.19; 'header:User-Agent:1': 0.23; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; '>>>>': 0.31; 'probably': 0.32; 'skip:d 20': 0.34; 'to:addr :python-list': 0.38; 'pm,': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'ian': 0.60; 'number,': 0.60; 'results.': 0.60; '6.1': 0.84; 'received:fios.verizon.net': 0.84; 'subject:find': 0.84; 'subject:Best': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: Best search algorithm to find condition within a range Date: Tue, 07 Apr 2015 14:55:01 -0400 References: <2e3a3c01-20b3-4948-9b32-bd80ed46822b@googlegroups.com> <9fd13f49-8bfa-43ef-8787-21d5c76a6268@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: pool-98-114-97-173.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.6.0 In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20 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: 27 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1428432943 news.xs4all.nl 2900 [2001:888:2000:d::a6]:58282 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:88610 On 4/7/2015 1:44 PM, Ian Kelly wrote: >>>> def to_base(number, base): > ... digits = [] > ... while number > 0: > ... digits.append(number % base) > ... number //= base > ... return digits or [0] > ... >>>> to_base(2932903594368438384328325832983294832483258958495845849584958458435439543858588435856958650865490, 429496729) > [27626525, 286159541, 134919277, 305018215, 329341598, 48181777, > 79384857, 112868646, 221068759, 70871527, 416507001, 31] > About 15 microseconds. % and probably // call divmod internally and toss one of the results. Slightly faster (5.7 versus 6.1 microseconds on my machine) is def to_base_dm(number, base): digits = [] while number > 0: number, rem = divmod(number, base) digits.append(rem) return digits or [0] -- Terry Jan Reedy