Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed2a.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.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'else:': 0.03; 'broken': 0.04; 'represents': 0.05; 'root': 0.05; '(b)': 0.07; 'none:': 0.07; 'suppose': 0.07; 'method,': 0.09; 'root,': 0.09; 'subject:set': 0.09; 'terms,': 0.09; 'cc:addr:python-list': 0.11; 'def': 0.12; 'decimal.': 0.16; 'digits.': 0.16; 'distinct': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'non- integers': 0.16; 'notation': 0.16; 'prev': 0.16; 'true:': 0.16; 'y):': 0.16; 'wrote:': 0.18; 'bit': 0.19; 'written': 0.21; '>>>': 0.22; 'cc:addr:python.org': 0.22; '(a)': 0.24; "aren't": 0.24; 'fraction': 0.24; 'integer': 0.24; 'cc:2**0': 0.24; 'nearly': 0.26; 'least': 0.26; 'gets': 0.27; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'wondering': 0.29; 'chris': 0.29; 'raise': 0.29; 'expansion': 0.30; 'is?': 0.30; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; "skip:' 10": 0.31; 'that.': 0.31; 'constant': 0.31; "d'aprano": 0.31; 'decimal': 0.31; 'steven': 0.31; 'subject:numbers': 0.31; 'subject:size': 0.31; 'way?': 0.31; 'figure': 0.32; 'subject:the': 0.34; 'could': 0.34; 'subject: (': 0.35; 'subject:with': 0.35; "can't": 0.35; 'except': 0.35; 'skip:s 30': 0.35; 'case,': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'representing': 0.36; 'sequence': 0.36; 'yield': 0.36; "didn't": 0.36; 'possible': 0.36; 'two': 0.37; 'easily': 0.37; 'represent': 0.38; 'skip:o 20': 0.38; 'pm,': 0.38; 'anything': 0.39; 'enough': 0.39; 'space': 0.40; 'even': 0.60; 'easy': 0.60; 'continued': 0.60; 'number,': 0.60; 'identify': 0.61; 'numbers': 0.61; 'effective': 0.61; 'entire': 0.61; 'simple': 0.61; "you're": 0.61; 'kind': 0.63; 'skip:n 10': 0.64; 'sum': 0.64; 'more': 0.64; 'effectively': 0.66; 'series': 0.66; 'benefit': 0.68; 'mar': 0.68; 'square': 0.74; 'roots.': 0.84; 'step,': 0.84; 'to:none': 0.92; 'directly.': 0.95 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:content-transfer-encoding; bh=/vbdOnmEtJ49JZVkc1c73D95HgtsjxbtRcW5121bGms=; b=PEg9WE91lcxVzLhXXx6b68Hd2s4QEzMiJvU+c9Gumx6dKfG7eAgi0hflnWmEBG8k7p M05REI+t5JLg41xKM+Nx/G9ta9qQfHq7TBSJEskgbX2fMd1goyhYME0B9GcK16BNqmog OD/2wrBdJaTs5k6tnRHztHgsuIAxIVvlorI8R5TbjYfUQ7xfyvMUfZ54UU8liGrAeQN6 e9kSfolMhyv5htKfJ5QYJJ+IG6oUJeS+MIzIX2WFV758vJmyDW7cLx6yQmKAmxi+9ncA i+RAnKb3EJIcWtk05422j3xHmjLGPzJYRvlQLxT6+3UHMNc1laFtLA9ki7U0Fhd2EFpA k8VQ== MIME-Version: 1.0 X-Received: by 10.66.118.71 with SMTP id kk7mr4171516pab.14.1393914912869; Mon, 03 Mar 2014 22:35:12 -0800 (PST) In-Reply-To: <53156a42$0$2923$c3e8da3$76491128@news.astraweb.com> 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> Date: Tue, 4 Mar 2014 17:35:12 +1100 Subject: Re: Working with the set of real numbers (was: Finding size of Variable) From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable 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: 84 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1393914922 news.xs4all.nl 2977 [2001:888:2000:d::a6]:39912 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:67651 On Tue, Mar 4, 2014 at 4:53 PM, Steven D'Aprano wrote= : > On Tue, 04 Mar 2014 14:46:25 +1100, Chris Angelico wrote: > >> That's neat, didn't know that. Is there an efficient way to figure out, >> for any integer N, what its sqrt's CF sequence is? And what about the >> square roots of non-integers - can you represent =E2=88=9A=CF=80 that wa= y? I suspect, >> though I can't prove, that there will be numbers that can't be >> represented even with an infinite series - or at least numbers whose >> series can't be easily calculated. > > Every irrational number can be written as a > continued fraction with an infinite number of terms, just as every > irrational number can be written as a decimal number with an infinite > number of digits. It's easy enough to have that kind of expansion, I'm wondering if it's possible to identify it directly. To render the decimal expansion of a square root by the cut-and-try method, you effectively keep dividing until you find that you're "close enough"; that means you (a) have to keep the entire number around for each step, and (b) need to do a few steps to find that the digits aren't changing. But if you can take a CF (finite or infinite) and do an O(n) transformation on it to produce that number's square root, then you have an effective means of representing square roots. Suppose I make a generator function that represents a fraction: def one_third(): while True: yield 3 def one_seventh(): while True: yield 1; yield 4; yield 2; yield 8; yield 5; yield 7 I could then make a generator that returns the sum of those two: def add_without_carry(x, y): whiile True: yield next(x)+next(y) Okay, that's broken for nearly any case, but with a bit more sophistication= : def add(x, y): prev=3DNone nines=3D0 while True: xx,yy=3Dnext(x),next(y) tot=3Dxx+yy if tot=3D=3D9: nines+=3D1 continue if tot>9: if prev is None: raise OverflowError("exceeds 1.0") yield prev+1 tot-=3D10 for _ in range(nines): yield 0 nines=3D0 else: if prev is not None: yield prev prev=3Dtot def show(n): return ''.join(str(_) for _ in itertools.islice(n,20)) >>> show(add(one_third(),one_seventh())) '47619047619047619047' >>> show(add(add(add(one_seventh(),one_seventh()),add(one_seventh(),one_sev= enth())),add(one_seventh(),one_seventh()))) '85714285714285714285' In constant space, that will produce the sum of two infinite sequences of digits. (And it's constant time, too, except when it gets a stream of nines. Adding three thirds together will produce an infinite loop as it waits to see if there'll be anything that triggers an infinite cascade of carries.) Now, if there's a way to do that for square rooting a number, then the CF notation has a distinct benefit over the decimal expansion used here. 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. ChrisA