Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.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.046 X-Spam-Evidence: '*H*': 0.91; '*S*': 0.00; 'subject:set': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'def': 0.12; 'benjamin': 0.16; 'cc:name:python list': 0.16; 'iteration': 0.16; 'prec': 0.16; 'precision.': 0.16; 'wrote:': 0.18; 'import': 0.22; 'cc:addr:python.org': 0.22; 'cc:2**0': 0.24; 'header:In-Reply- To:1': 0.27; 'message-id:@mail.gmail.com': 0.30; 'url:wiki': 0.31; 'assert': 0.31; 'decimal': 0.31; 'subject:numbers': 0.31; 'url:wikipedia': 0.31; 'skip:# 10': 0.33; 'subject:the': 0.34; 'subject:with': 0.35; 'received:google.com': 0.35; 'done': 0.36; 'method': 0.36; 'url:org': 0.36; 'should': 0.36; 'obtain': 0.39; 'march': 0.61; 'total': 0.65; 'believe': 0.68; 'obvious': 0.74; 'complexity': 0.84; 'eps': 0.84; 'oscar': 0.84 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:content-transfer-encoding; bh=3sYQzsb+aQez3f8j+XjJ1zXXrYJ6RY+fDZHiSKZe41g=; b=Xobpc4lCfeuC1h6HCo/ueDh5/8V15mmH1povO6jKr4u1AgtJq6+iBnRcgPA7PwonM2 yCnYwzDUxH1A9qnAX99ryLILuWjLnvLV/4ezAC1YXDUJS8IkNaguO/ZQxvXmI2zIh71Y x1bH0IiLBsAQQySKKv2h+EFyf8AyY7ZvOv3eeP4U+q1EVXNc5d89KJ3gPkx8DiGi+rTn ag+2ROnXOtjFY0MlyO6B+WKzp1IDn7Qf4m/lWHmJSOHWaPSMCul5ycCXEX9IKhj3eLy6 GMg+LALcMATpyWgAlbjH2CwixSdFbED6fhPzMdUgEPYOOkv8BRIQtFbNcN7ML977tt1J fhHg== X-Received: by 10.220.159.4 with SMTP id h4mr1561091vcx.1.1393970944712; Tue, 04 Mar 2014 14:09:04 -0800 (PST) MIME-Version: 1.0 In-Reply-To: <87y50pn08t.fsf@elektro.pacujo.net> 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> <87y50pn08t.fsf@elektro.pacujo.net> From: Oscar Benjamin Date: Tue, 4 Mar 2014 22:08:44 +0000 Subject: Re: Working with the set of real numbers To: Marko Rauhamaa Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Cc: Python List 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: 34 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1393970947 news.xs4all.nl 2970 [2001:888:2000:d::a6]:45254 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:67739 On 4 March 2014 21:05, Marko Rauhamaa wrote: > Oscar Benjamin : > >> 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=3D1000): >> '''Solve x**2 =3D y''' >> assert y > 0 >> eps =3D D(10) ** -(prec + 5) >> x =3D D(y) >> with localcontext() as ctx: >> ctx.prec =3D prec + 10 >> while x ** 2 - y > x * eps: >> x =3D (x + y/x) / 2 >> return x >> >> print(sqrt(2)) > > At a quick glance, I believe x ** 2 is O(N=B2) and so the total complexit= y > should be O(N ** 2.5). x**2 is just a multiplication which can be done in better than O(N**2): http://en.wikipedia.org/wiki/Multiplication_algorithm#Fast_multiplication_a= lgorithms_for_large_inputs Oscar