Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.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.021 X-Spam-Evidence: '*H*': 0.96; '*S*': 0.00; 'algorithm': 0.04; 'modified': 0.07; 'plenty': 0.07; 'calculating': 0.09; 'subject:set': 0.09; 'worse': 0.09; 'cc:addr:python-list': 0.11; 'def': 0.12; '0.2': 0.16; 'benjamin': 0.16; 'digits.': 0.16; 'doubles': 0.16; 'force.': 0.16; 'iteration': 0.16; 'wrote:': 0.18; 'wed,': 0.18; 'basically': 0.19; 'cc:addr:python.org': 0.22; 'error': 0.23; 'cc:2**0': 0.24; 'header:In-Reply-To:1': 0.27; 'correct': 0.29; 'chris': 0.29; 'am,': 0.29; 'generally': 0.29; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'gives': 0.31; 'code': 0.31; 'url:wiki': 0.31; 'decimal': 0.31; 'subject:numbers': 0.31; 'url:wikipedia': 0.31; 'quite': 0.32; 'becomes': 0.33; 'subject:the': 0.34; 'subject:with': 0.35; 'something': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'really': 0.36; 'earth': 0.36; 'url:org': 0.36; 'problems': 0.38; 'anything': 0.39; 'does': 0.39; 'how': 0.40; 'march': 0.61; 'simple': 0.61; 'first': 0.61; 'show': 0.63; 'such': 0.63; 'more': 0.64; 'talking': 0.65; 'to:addr:gmail.com': 0.65; 'here': 0.66; 'mar': 0.68; 'square': 0.74; 'more:': 0.84; 'oscar': 0.84; 'roots.': 0.84; 'reasoning': 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 :cc:content-type; bh=Fu/b3BWEDxmmMYC02ZOUTi8izACBBhNimdKYEsHOMSo=; b=MEynD763hsy1rXcFM9mGclbzEIuVmdojkPC+ajyMlwPbiMI32JOd77Y/zpsNwT/0nH MONuGHpRfi9bKK3rq31feYNal2VLTztO3Dw4pmaiSkw4O+ROHySwDxz7gWBKFgucglV7 BxRCOhewVcSzYpUB5lf8wgQLNWDgl9F/O0zVIsXo8HptI80aD26BvQbFV1Y9VvFct33G 95VRyctV4Sp1KDCdp3uw2bv6lYjz8TsKpg++kQO1cRnOq2iaBgMdAsFukBooWgE1lZqr Vt67EbnUkT1J/bpTEdf1uiLiS3Rh3+Hg5SRg4gknUIlPbyUI3ZOOOqaEb51640g/7JEy N+tw== X-Received: by 10.220.161.8 with SMTP id p8mr1576432vcx.4.1393970557019; Tue, 04 Mar 2014 14:02:37 -0800 (PST) MIME-Version: 1.0 In-Reply-To: References: <8e4c1ab1-e65d-483f-ad9d-6933ae2052c3@googlegroups.com> <85r478bv99.fsf_-_@benfinney.id.au> <53153e66$0$24931$e4fe514c@dreader36.news.xs4all.nl> <59dd57ad-39b0-4c71-a58e-b4ae6517b385@googlegroups.com> <53156a42$0$2923$c3e8da3$76491128@news.astraweb.com> <87iortoic0.fsf@elektro.pacujo.net> From: Oscar Benjamin Date: Tue, 4 Mar 2014 22:02:15 +0000 Subject: Re: Working with the set of real numbers To: Chris Angelico Content-Type: text/plain; charset=ISO-8859-1 Cc: "python-list@python.org" 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: 60 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1393970560 news.xs4all.nl 2860 [2001:888:2000:d::a6]:36758 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:67737 On 4 March 2014 21:18, Chris Angelico wrote: > On Wed, Mar 5, 2014 at 7:55 AM, Oscar Benjamin > wrote: >> I don't quite follow your reasoning here. By "cut-and-try" do you mean >> bisection? If so it gives the first N decimal digits in N*log2(10) >> iterations. However each iteration requires a multiply and when the >> number of digits N becomes large the multiplication is worse than >> linear. So the result is something like N**2 log(N)log(log(N)), > > By "cut and try" I'm talking about the really REALLY simple algorithm > for calculating square roots. It's basically brute force. > > epsilon = 0.0001 > def sqrt(n): > guess1, guess2 = 1, n > while abs(guess1-guess2) > epsilon: > guess1 = n/guess2 > guess2 = (guess1 + guess2)/2 > return guess1 That's the exact same algorithm I showed! How on earth would you call that brute force? > It's generally going to take roughly O(n*n) time to generate n digits, > give or take. It does not take O(n*n) time. This is Newton iteration and for well-behaved problems such as this it generates more than n digits after n iterations. I modified my code to show the error (x**2 - y) at each iteration: $ python3.3 root.py 2 0.2 0.007 0.000006 5E-12 3E-24 8E-49 8E-98 8E-196 9E-392 1E-783 The number of correct digits doubles at each iteration so after n iterations you have 2**n digits (I misstated this as n**2 before). This means that it takes log(N) iterations to get N digits. See here for more: http://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Babylonian_method See also the section below that: http://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Digit-by-digit_calculation > That's the baseline against which anything else can be > compared. There are plenty of better ways to calculate them. Such as? Oscar