Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.019 X-Spam-Evidence: '*H*': 0.96; '*S*': 0.00; 'algorithm': 0.04; 'root': 0.05; 'odd': 0.07; 'result,': 0.07; '128': 0.09; 'bits': 0.09; 'cc:addr:python-list': 0.11; 'compute': 0.16; 'computes': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'iteration,': 0.16; 'sat,': 0.16; 'wrote:': 0.18; 'obviously': 0.18; 'bit': 0.19; 'seems': 0.21; 'cc:addr:python.org': 0.22; 'integer': 0.24; 'initial': 0.24; 'math': 0.24; 'cc:2**0': 0.24; 'header:In-Reply-To:1': 0.27; 'point': 0.28; 'message- id:@mail.gmail.com': 0.30; 'closer': 0.31; 'guess': 0.33; 'could': 0.34; 'but': 0.35; 'received:google.com': 0.35; 'really': 0.36; 'doing': 0.36; 'example,': 0.37; 'too': 0.37; 'two': 0.37; 'christian': 0.38; 'nov': 0.38; 'fact': 0.38; 'pm,': 0.38; 'skip:x 10': 0.40; 'even': 0.60; 'length': 0.61; 'lower': 0.61; 'range': 0.61; 'simply': 0.61; 'first': 0.61; 'guarantee': 0.63; 'such': 0.63; 'skip:n 10': 0.64; '(that': 0.65; 'between': 0.67; 'therefore': 0.72; 'square': 0.74; 'power': 0.76; 'to:none': 0.92 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=n6wrHElXO4ZQgq4rmlKG9adkbHGFSoF/XY4vhRexDPg=; b=Jd9Tz0mMPIIWhWnj7mRiLTi18i285scQ16cvhzPgtY3rdC+BzkMNWpHjknuF+Zv7uS AzLsDovWzHNYGz7MVxT5zbSCXzKiWIPxvhSSj9ep/yQYcSdsWKD2wQC4WUKvAOGFvaj8 dkroiDfnokdcymriafZjM20EjdCxCCa5FQkiTcyMY9d255BtBPYb9d51ZEoLj/n4mMNu 0RBthNE1Gk8sC8aKYOu4NoHILidxymSAm3fk9SwCPRhU660ONHfskhnfJWNW03ieh3Vd z2/Wyeckik35HbeNdLP5UekumJjhaUkgPJmOE+jvmPaQCRX3fp+nuPpesSge7rPalRVV bL7g== MIME-Version: 1.0 X-Received: by 10.107.154.144 with SMTP id c138mr33845673ioe.11.1414829614749; Sat, 01 Nov 2014 01:13:34 -0700 (PDT) In-Reply-To: References: <54543787$0$13005$c3e8da3$5496439d@news.astraweb.com> Date: Sat, 1 Nov 2014 19:13:34 +1100 Subject: Re: Challenge: optimizing isqrt From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 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: 24 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1414830070 news.xs4all.nl 2839 [2001:888:2000:d::a6]:56142 X-Complaints-To: abuse@xs4all.nl Path: csiph.com!usenet.pasdenom.info!bete-des-vosges.org!feed.ac-versailles.fr!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Xref: csiph.com comp.lang.python:80077 On Sat, Nov 1, 2014 at 7:02 PM, Christian Gollwitzer wrote: > Your above algorithm is obviously doing Heron- or Newton-Raphson iterations, > so the same as with floating point math. The first line before the while > loop computes some approximation to sqrt(n). Instead of doing bit shuffling, > you could compute this by FP math and get closer to the desired result, > unless the integer is too large to be represented by FP. Now, the > terminating condition seems to rely on the fact that the initial estimate > x>=sqrt(n), but I don't really understand it. My guess is that if you do > x=int(sqrt(n)), then do the first iteration, then swap x and y such that > x>y, then enter the loop, you would simply start with a better estimate in > case that the significant bits can be represented by the float. Part of the point of that algorithm is that it never uses FP, and is therefore not limited by FP restrictions. As to the assumption that x>=sqrt(n), that would be safe: if the bit length is even (that is, it's between an odd power of 2 and an even one - for example, 2**13 < 16000 <= 2**14), the initial estimate is the exact square root of the power of two at the top of that range (bit length of 14 means x is 2**7, 128 == sqrt(16384)); if the bit length is odd (eg 2**8 < 300 <= 2**9), the initial estimate rounds the halving upward (bit length of 9 means x is 2**(9//2+1), 32 > sqrt(512)). So there's a guarantee that the initial estimate is no lower than the target number. ChrisA