Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.024 X-Spam-Evidence: '*H*': 0.95; '*S*': 0.00; 'python,': 0.02; 'algorithm': 0.04; 'base.': 0.05; 'motivated': 0.05; 'binary': 0.07; "wouldn't": 0.14; "(it's": 0.16; '37,': 0.16; 'exponent': 0.16; 'left,': 0.16; 'nearest': 0.16; 'remainder': 0.16; 'subject:search': 0.16; 'subtract': 0.16; 'top-level': 0.16; 'all.': 0.16; 'wrote:': 0.18; 'code.': 0.18; 'first.': 0.19; 'header:User-Agent:1': 0.23; 'error': 0.23; 'certainly': 0.24; 'header:In-Reply-To:1': 0.27; 'record': 0.27; 'am,': 0.29; 'scale': 0.29; 'converting': 0.30; 'skip:( 20': 0.30; 'code': 0.31; 'limitation': 0.33; 'actual': 0.34; "i'd": 0.34; 'could': 0.34; 'convert': 0.35; 'but': 0.35; 'add': 0.35; 'there': 0.35; 'doing': 0.36; 'next': 0.36; 'error.': 0.37; 'example,': 0.37; 'to:addr:python-list': 0.38; 'list,': 0.38; 'to:addr:python.org': 0.39; 'how': 0.40; 'dave': 0.60; 'save': 0.62; 'email addr:gmail.com': 0.63; 'more': 0.64; 'charset:windows-1252': 0.65; 'biggest': 0.67; 'received:74.208': 0.68; 'reverse': 0.68; 'hand': 0.80; 'trial': 0.83; "'3'": 0.84; "'3',": 0.84; '2015': 0.84; 'divide': 0.84; 'subject:find': 0.84; '378': 0.91; 'subject:Best': 0.91 Date: Tue, 07 Apr 2015 10:26:48 -0400 From: Dave Angel User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.5.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Best search algorithm to find condition within a range References: <2e3a3c01-20b3-4948-9b32-bd80ed46822b@googlegroups.com> <2d885c21-95c9-4ea1-b82e-d5420d886c8b@googlegroups.com> In-Reply-To: <2d885c21-95c9-4ea1-b82e-d5420d886c8b@googlegroups.com> Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-Provags-ID: V03:K0:eRT7a3g5jBZEY1VT+C3RGO5Amut4rIu3borR/wrueQG90HMq5Mg bC9U2ILKbeFKIcdImELiVQ3Xw9VSg0j1uqyoySHZQbqchoTxZxoJI83g8nD7rXn6Aj0L1H4 N0nbxEYJwYgyvi7wXnUIwyV2hUoMhobQpVsybx0xrn1Z5MCUnsKKT9lVaOMW2TdLPWbJj89 4aDY9+/JYhPp/bgDjyDww== X-UI-Out-Filterresults: notjunk:1; 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: 43 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1428416825 news.xs4all.nl 2905 [2001:888:2000:d::a6]:58315 X-Complaints-To: abuse@xs4all.nl Path: csiph.com!usenet.pasdenom.info!news.franciliens.net!feed.ac-versailles.fr!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Xref: csiph.com comp.lang.python:88578 On 04/07/2015 10:10 AM, jonas.thornvall@gmail.com wrote: > Den tisdag 7 april 2015 kl. 15:30:36 UTC+2 skrev Dave Angel: >> >> If that code were in Python, I could be more motivated to critique it. >> The whole algorithm could be much simpler. But perhaps there is some >> limitation of javascript that's crippling the code. >> >> How would you do it if you were converting the base by hand? I >> certainly wouldn't be doing any trial and error. For each pass, I'd >> calculate quotient and remainder, where remainder is the digit, and >> quotient is the next value you work on. >> >> >> -- >> DaveA > > I am doing it just like i would do it by hand finding the biggest digit first. To do that i need to know nearest base^exp that is less than the actual number. Add up the digit (multiply) it to the nearest smaller multiple. Subtract that number (base^exp*multiple). > > Divide / Scale down the exponent with base. And record the digit. > And start looking for next digit doing same manipulation until remainder = 0. > > And that is what i am doing. > Then I don't know why you do the call to reverse() in the top-level code. If I were doing it, I'd have no trial and error in the code at all. Generate the digits right to left, then reverse them before returning. For example, if you want to convert 378 to base 10 (it's binary internally), you'd divide by 10 to get 37, remainder 8. Save the 8, and loop again. Divide 37 by 10 and get 3, remainder 7. Save the 7. Divide again by 10 and get 0, remainder 3. Save the 3 Now you have '8', '7', '3' So you reverse the list, and get '3', '7', '8' -- DaveA