Path: csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed7.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.012 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'base.': 0.05; 'binary': 0.05; 'linear': 0.07; 'subject:skip:b 10': 0.07; 'logic': 0.09; 'bug': 0.10; 'def': 0.14; 'algorithm.': 0.16; 'algorithmic': 0.16; 'complexity,': 0.16; 'hexadecimal': 0.16; 'mappings,': 0.16; 'scope.': 0.16; 'wrote:': 0.16; 'integer': 0.18; '>>>': 0.20; 'machine': 0.21; '(the': 0.22; 'am,': 0.23; '2015': 0.23; 'header :In-Reply-To:1': 0.24; 'converting': 0.27; 'operations,': 0.27; 'sequence': 0.27; 'message-id:@mail.gmail.com': 0.28; 'division': 0.29; 'function:': 0.29; 'value)': 0.29; 'subject:time': 0.31; "can't": 0.32; 'true.': 0.33; 'running': 0.34; 'received:google.com': 0.34; 'to:addr:python-list': 0.35; 'faster': 0.36; 'subject:: ': 0.37; "skip:' 20": 0.37; 'tue,': 0.38; 'christian': 0.38; 'building': 0.38; 'to:addr:python.org': 0.39; 'where': 0.40; 'some': 0.40; '30,': 0.63; 'relatively': 0.63; 'different': 0.64; 'therefore': 0.65; 'believe': 0.67; 'square': 0.76; 'actually,': 0.84; 'faster.': 0.84; 'gollwitzer': 0.84; 'to:name:python': 0.84; 'ratio': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=rgnm1rZA3+ddoJzYNreZRf9/8inaBxQ43sCNzAOwjUw=; b=MY2HaYGFGyc3YMOaQb8fRBsEDwWgEh6KjG/RmAvv0SsHZ5QrKGBGQX6K8CUE6Yt3yZ /65hqudZwKg4xVOzyA0tAa8kxzrayEZit3EH3OQJyaJf5fzGUQMurlqGK/tyZWqrEGe4 c43DUGeidMTZ8hgMGYXf2u2pv+m1oNF+Kw7dDfzuJ/fxjzZiS/1YJjyj+lbJklPkYduA 1eKtgidYW61KNLGEv0ZdadAfoQnhmG1CSxBjC84zKVLrz7I20LMT+zXii5KDT6cw7api zS78ptBOzLxj5MsZXYClMDseJe52N1tolwFJhfLrHOQQV1RlfnUlvMg3rYYiNqX8bpw1 PxZw== X-Received: by 10.129.56.70 with SMTP id f67mr26308401ywa.85.1435679145265; Tue, 30 Jun 2015 08:45:45 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: References: <777831f0-d4b4-48f6-ae0b-c9b1ea7ffc06@googlegroups.com> <87r3ouawgt.fsf@bsb.me.uk> <7c6dac9d-5722-4179-bd7e-ceaac6698490@googlegroups.com> From: Ian Kelly Date: Tue, 30 Jun 2015 09:45:05 -0600 Subject: Re: Linear time baseconversion To: Python Content-Type: text/plain; charset=UTF-8 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: 34 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1435679147 news.xs4all.nl 2887 [2001:888:2000:d::a6]:58725 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:93333 On Tue, Jun 30, 2015 at 9:40 AM, Ian Kelly wrote: > On Tue, Jun 30, 2015 at 3:07 AM, Christian Gollwitzer wrote: >> Am 30.06.15 um 10:52 schrieb jonas.thornvall@gmail.com: >>> >>> It still bug out on very big numbers if base outside integer scope. >>> I am very keen on suggestions regarding the logic to make it faster. >> >> >> Concerning the algorithmic complexity, it can't be faster than square time >> in the number of digits N. Baseconversion needs to do a sequence of division >> operations, where every operation gves you one digit in the new base. The >> number of digits in the new base is proportional to the number of digits in >> the old base (the ratio is log b1/log b2). Therefore it will be O(N^2). > > I don't think that's true. Here's a linear hexadecimal to binary function: > >>>> def hextobin(value): > ... digits = {'0': '0000', '1': '0001', '2': '0010', '3': '0011', > ... '4': '0100', '5': '0101', '6': '0110', '7': '0111', > ... '8': '1000', '9': '1001', 'A': '1010', 'B': '1011', > ... 'C': '1100', 'D': '1101', 'E': '1110', 'F': '1111'} > ... return ''.join(digits[d.upper()] for d in value) > ... >>>> hextobin('3f') > '00111111' > > I believe this approach can be extended to arbitrary bases with some > effort, although for converting arbitrary base b1 to b2, you would > need up to b2 different mappings if b1 and b2 are relatively prime. Actually, I think you need up to b1 * b2 mappings, as you're effectively building a state machine with b1 * b2 states. The mappings can be pre-computed, however, so actually running the state machine would then just be a linear algorithm.