Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.015 X-Spam-Evidence: '*H*': 0.97; '*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; 'algorithmic': 0.16; 'complexity,': 0.16; 'hexadecimal': 0.16; 'scope.': 0.16; 'wrote:': 0.16; 'integer': 0.18; '>>>': 0.20; '(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; '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; '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; '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=bmPwO8/DLQWpQBZ1Fy7Q1vYoThlReIzfqsjLWTPcR2Y=; b=jxjCGKVBZuXSchDJKatFNqxzOuYXXLppYmDaLSnYESzv8YrpPOoyhwS9ltMesLT92Z vxvTiRmbkbI9NRQY4djOCKA8UaAn4lR+35SZdg18Qk5kMEHEFOOiHNPN+gFQvga+rXSU Njk22e40Sp2ckRw3o0+5zojRLZC9pWysvZwV064eL1MADQ9bkDT6x8o6tBEEg8z5lW11 FS/ROLPgF25IUIsaVu2JxEwdW50ZYVV2WlzNhHFExOuwhnnhVmuueSvQsmAaTiaU9QiJ POyYJjdV7u+NuM8NfC2xpUtyKDUEGWou4p+/kohoJHsPdZwpVk1UMo8+K2jGu4KO08uJ j9dA== X-Received: by 10.170.141.4 with SMTP id i4mr26345696ykc.32.1435678872163; Tue, 30 Jun 2015 08:41:12 -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:40:32 -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: 28 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1435680388 news.xs4all.nl 2917 [2001:888:2000:d::a6]:38833 X-Complaints-To: abuse@xs4all.nl Path: csiph.com!usenet.pasdenom.info!news.stben.net!border1.nntp.ams1.giganews.com!nntp.giganews.com!newsfeed.xs4all.nl!newsfeed7.news.xs4all.nl!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Xref: csiph.com comp.lang.python:93335 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.