Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed3.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.089 X-Spam-Evidence: '*H*': 0.82; '*S*': 0.00; 'root': 0.05; 'subject:set': 0.09; 'def': 0.12; 'true:': 0.16; 'unnecessary.': 0.16; 'wrote:': 0.18; 'sorry,': 0.24; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; 'message-id:@mail.gmail.com': 0.30; 'subject:numbers': 0.31; 'subject:size': 0.31; 'totally': 0.33; 'subject:the': 0.34; 'subject: (': 0.35; 'subject:with': 0.35; 'received:google.com': 0.35; 'yield': 0.36; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'continued': 0.60; 'ian': 0.60; 'break': 0.61; 'more': 0.64; 'mar': 0.68; 'business': 0.70; 'square': 0.74 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 :content-type; bh=w8NhCP/1BQAYsHQl0a+X6hwBT5gwyXLsuIHubUsjClE=; b=f0uZI+Wsc0d60z6AGivwtg2FIXo4qF2cRMP2Sr88WoNovC5+C+KbdeMlooNU8UnZTo wcRrgYdDAa9bVhkm8QtNALVf8cJebZpn7ZcOZjonSElRgamjw3LtRF7QN7GdubMIB4LI Oj6y470uvJC/Sp18v+y6sOuOpkvD+MonIhHC6H2jhNs5ZjKobEV+rrHC/DX2lQQM1P4c fEkZHbMWyzpV96MQpUECvtxn0cts7tE2WrdB+qBe1M6WskNYA0vmdSLxzCBXYbAjYsFu mFnrCpsi3mQYtUBl1HNF58cQEFrSiodr0kA8EkTTChCvHqpctAb/kWAziLcHhpm/Jmpm dxxQ== X-Received: by 10.67.13.134 with SMTP id ey6mr5419150pad.44.1393932224540; Tue, 04 Mar 2014 03:23:44 -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> From: Ian Kelly Date: Tue, 4 Mar 2014 04:23:04 -0700 Subject: Re: Working with the set of real numbers (was: Finding size of Variable) To: Python Content-Type: text/plain; charset=ISO-8859-1 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: 29 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1393932233 news.xs4all.nl 2881 [2001:888:2000:d::a6]:44207 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:67663 On Tue, Mar 4, 2014 at 4:19 AM, Ian Kelly wrote: > def cf_sqrt(n): > """Yield the terms of the square root of n as a continued fraction.""" > m = 0 > d = 1 > a = a0 = floor_sqrt(n) > while True: > yield a > next_m = d * a - m > next_d = (n - next_m * next_m) // d > if next_d == 0: > break > next_a = (a0 + next_m) // next_d > m, d, a = next_m, next_d, next_a Sorry, all that "next" business is totally unnecessary. More simply: def cf_sqrt(n): """Yield the terms of the square root of n as a continued fraction.""" m = 0 d = 1 a = a0 = floor_sqrt(n) while True: yield a m = d * a - m d = (n - m * m) // d if d == 0: break a = (a0 + m) // d