Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed4a.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.007 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'else:': 0.03; 'anyway.': 0.05; 'root': 0.05; 'binary': 0.07; 'already.': 0.09; 'root,': 0.09; 'subject:set': 0.09; 'def': 0.12; '"""return': 0.16; 'arithmetic.': 0.16; 'decimal.': 0.16; 'digits.': 0.16; 'distinct': 0.16; 'evaluating': 0.16; 'merely': 0.16; 'notation': 0.16; 'true:': 0.16; 'wrote:': 0.18; 'starts': 0.20; 'example': 0.22; 'fraction': 0.24; 'integer': 0.24; 'mon,': 0.24; 'looks': 0.24; 'this:': 0.26; 'gets': 0.27; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'chris': 0.29; 'expansion': 0.30; 'see,': 0.30; 'message-id:@mail.gmail.com': 0.30; 'code': 0.31; 'constant': 0.31; 'decimal': 0.31; 'mid': 0.31; 'subject:numbers': 0.31; 'subject:size': 0.31; 'probably': 0.32; 'checking': 0.33; 'subject:the': 0.34; 'could': 0.34; 'subject: (': 0.35; 'subject:with': 0.35; 'except': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'add': 0.35; 'yield': 0.36; 'doing': 0.36; 'two': 0.37; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'anything': 0.39; 'though,': 0.39; 'to:addr:python.org': 0.39; 'called': 0.40; 'space': 0.40; 'how': 0.40; 'continued': 0.60; 'number,': 0.60; 'break': 0.61; 'lower': 0.61; 'matter': 0.61; 'simple': 0.61; 'back': 0.62; 'sum': 0.64; 'more': 0.64; 'benefit': 0.68; 'mar': 0.68; 'square': 0.74; 'upper': 0.74; 'optimized,': 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 :content-type; bh=sLbpl0RF4YNfyhfG/51VHoVB3C3H7RBaCvDdCBSKw7w=; b=z+Myw24pG2VBUCGe2+UFIjCUOCQvvwq/pIv1QE7DTrj6RDyuzs9/+s8B6YWJvifqgh FYRMyLPEmcDW8qGwO2Q3oXmLkUt/6GlP4qm83Vuy37X8uP01R7wed0vXthpjIsSE7WKl LSJiVwdLUhwH8zvb+GBph3wFQoScWqYHbYDHFuzFZL+TdaFiAhLqjvX0tn1VaZR03l7+ KETM1Yi6e0y2FR7XS7qtJZdizoSII1503Y13lYEO4IRDWRtde/dJoTZWiIlSVe3V0SDl Hi+NwnR6UQGXVVliz1nOe0jB/IH+XZt2hy2tYAb6NY4m/jKyAWGbMGQL+agykQfh6I3A EBbw== X-Received: by 10.66.138.40 with SMTP id qn8mr5432347pab.154.1393932039220; Tue, 04 Mar 2014 03:20:39 -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:19:59 -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: 53 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1393932047 news.xs4all.nl 2835 [2001:888:2000:d::a6]:41187 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:67662 On Mon, Mar 3, 2014 at 11:35 PM, Chris Angelico wrote: > 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. The code for that looks like this: 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 def floor_sqrt(n): """Return the integer part of the square root of n.""" n = int(n) if n == 0: return 0 lower = 2 ** int(math.log(n, 2) // 2) upper = lower * 2 while upper - lower > 1: mid = (upper + lower) // 2 if n < mid * mid: upper = mid else: lower = mid return lower The floor_sqrt function is merely doing a simple binary search and could probably be optimized, but then it's only called once during initialization anyway. The meat of the loop, as you can see, is just a constant amount of integer arithmetic. If it were desired to halt once the continued fraction starts to repeat, that would just be a matter of checking whether the triple (m, d, a) has been seen already. Going back to your example of adding generated digits though, I don't know how to add two continued fractions together without evaluating them.