Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder7.xlned.com!news.osn.de!diablo2.news.osn.de!news.tele.dk!news.tele.dk!small.news.tele.dk!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.033 X-Spam-Evidence: '*H*': 0.93; '*S*': 0.00; 'desired.': 0.07; 'root,': 0.09; 'subject:set': 0.09; 'worse': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'def': 0.12; 'decimal.': 0.16; 'iteration': 0.16; 'prec': 0.16; 'precision.': 0.16; 'striving': 0.16; 'zeros': 0.16; 'modification': 0.16; 'wrote:': 0.18; 'wed,': 0.18; 'producing': 0.19; '>>>': 0.22; 'import': 0.22; 'cc:addr:python.org': 0.22; 'space.': 0.24; 'cc:2**0': 0.24; 'gets': 0.27; 'header:In-Reply-To:1': 0.27; 'chris': 0.29; 'am,': 0.29; 'message-id:@mail.gmail.com': 0.30; 'gives': 0.31; 'assert': 0.31; 'constant': 0.31; 'decimal': 0.31; 'subject:numbers': 0.31; 'run': 0.32; 'quite': 0.32; 'becomes': 0.33; 'skip:# 10': 0.33; 'subject:the': 0.34; 'subject:with': 0.35; 'something': 0.35; 'requirement': 0.35; 'received:google.com': 0.35; 'really': 0.36; 'yield': 0.36; 'method': 0.36; 'url:org': 0.36; 'error.': 0.37; 'ends': 0.38; 'handle': 0.38; 'rather': 0.38; 'obtain': 0.39; 'space': 0.40; 'march': 0.61; 'simple': 0.61; 'first': 0.61; 'more': 0.64; 'situation': 0.65; 'to:addr:gmail.com': 0.65; 'believe': 0.68; 'mar': 0.68; 'obvious': 0.74; 'square': 0.74; 'bounded': 0.84; 'complexity': 0.84; 'eps': 0.84; 'oscar': 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=t/WYSHkqJxN1r6Jkv8a0fzgWP28CmKRf1YZxMjuEnYc=; b=lxBLDjbgKy6JEpkoAbhF97EOhIEckU9eDKyPsTWc+Nubf2WBKthJ53oKvOqfaCdHGH oWGYJNzm70Ch+M4/zO7XB3sma1cWl3RIxmtJxkElHFi6W2F50PwrvjJ1gS5iajcV1kyc LMpxcB4tHLfL/IYCsUe2w4SwWtj8aHE1PVw6RbnS3GkVR3/0XlaCUu9a9JaRBwoEgLjI 9XSgwBtprDWFU3UKF19nWzc9Q8RH6QUtx0MFuJvLCFABIiioDhtrCaqRwa2o1hUeXFRo WNa7N4kSvt0D9woiZii0MkoVUw7f/icTiXRJ3Cvfs/raIkNN5WSao2FijCSzJSTs5PBD 5tRg== X-Received: by 10.58.190.99 with SMTP id gp3mr1261789vec.32.1393966553566; Tue, 04 Mar 2014 12:55:53 -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 20:55:33 +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: 51 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1393966562 news.xs4all.nl 2873 [2001:888:2000:d::a6]:43211 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:67718 On 4 March 2014 19:58, Chris Angelico wrote: > On Wed, Mar 5, 2014 at 6:49 AM, Marko Rauhamaa wrote: >> Chris Angelico : >> >>> As far as I know, there's no simple way, in constant space and/or >>> time, to progressively yield more digits of a number's square root, >>> working in decimal. >> >> I don't know why the constant space/time requirement is crucial. Anyway, >> producing more digits simple: . >> >> I believe producing the nth digit is O(n) in time and space. > > The reason for striving for constant space/time is because the obvious > method (cut-and-try) is already O(n) for the nth digit, which means > it's quadratic on the number of digits desired. That gets pretty > nasty. 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)), To me the obvious method is Newton iteration which takes O(sqrt(N)) iterations to obtain N digits of precision. This brings the above complexity below quadratic: #!/usr/bin/env python from decimal import Decimal as D, localcontext def sqrt(y, prec=1000): '''Solve x**2 = y''' assert y > 0 eps = D(10) ** -(prec + 5) x = D(y) with localcontext() as ctx: ctx.prec = prec + 10 while x ** 2 - y > x * eps: x = (x + y/x) / 2 return x print(sqrt(2)) Some modification would be required to handle a situation where it ends in a run of nines or zeros if you really care about the exact digits rather than having a bounded error. Oscar