Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!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.021 X-Spam-Evidence: '*H*': 0.96; '*S*': 0.00; 'event,': 0.07; 'linear': 0.07; 'indicates': 0.09; 'pm,': 0.11; 'somewhere': 0.11; 'def': 0.13; ';-)': 0.14; 'am,': 0.14; 'wrote:': 0.14; 'ratio': 0.16; 'suggested.': 0.16; 'tue,': 0.20; 'writes:': 0.20; 'cc:no real name:2**0': 0.20; 'cc:2**0': 0.20; 'header:In-Reply-To:1': 0.22; 'cc:addr:python-list': 0.22; 'so.': 0.22; 'values': 0.23; 'message-id:@mail.gmail.com': 0.28; 'testing': 0.28; 'cc:addr:python.org': 0.31; '17,': 0.31; 'decimal': 0.31; 'does': 0.31; 'bit': 0.33; 'module': 0.33; 'using': 0.34; 'point': 0.35; 'received:209.85.216.46': 0.35; 'received:mail- qw0-f46.google.com': 0.35; 'quite': 0.36; 'think': 0.36; 'received:209.85': 0.37; 'faster': 0.38; 'received:google.com': 0.38; 'earlier': 0.39; 'larger': 0.39; 'ok,': 0.39; 'though,': 0.39; 'could': 0.39; 'received:209': 0.39; 'add': 0.39; 'would': 0.40; "it's": 0.40; 'header:Received:5': 0.40; 'bottom': 0.60; 'you.': 0.61; '2011': 0.62; 'golden': 0.68; 'tiny': 0.69; 'funny': 0.77; '10:19': 0.84; 'further,': 0.93 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:in-reply-to:references:date :message-id:subject:from:to:cc:content-type :content-transfer-encoding; bh=PZV70/y6ji/BwiXTG4XG1nS64K/YZDZWWUR7XwjiBrk=; b=LQ1E/AhEruBjp1KSc2ea6hg8Aqtc8tuDQdZ9arkLiPrD106GVnnVuf3hOojFlvqK5h 4W59Gs0ajhD7xB2jrv9ZI/3IOleug3GkKlx0jkTJRGMOMFBb9Lhnt80o1Mw6x8YzsMbt 3yxTTIbbAUoZWFJQ4ZTiKr8WJTTwJiTmYNV7Q= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type:content-transfer-encoding; b=gETE+JDCMWHgut8Vx5KqwUGm9+bJaMD6VosgjfTcFRnwY8AMMxbqRanVoRefSm95uC DJL1pOPk1e9XjKmb9F9j74O/wJ58er8bCGyS4bi/o/Jl8o0DrSd/13RpCrF0FCcNY36N vcNYRGIENuCf6gWzUV2mq/WSm/GrGJ4a9Vcks= MIME-Version: 1.0 In-Reply-To: <80ba3b5e-34c1-420a-aade-9c1996d0239e@u26g2000vby.googlegroups.com> References: <108ce447-10fa-4cf7-84ef-440ee18dbfd4@22g2000prx.googlegroups.com> <9d9c163b-14fd-4131-81bb-105c3b97c432@h36g2000pro.googlegroups.com> <80ba3b5e-34c1-420a-aade-9c1996d0239e@u26g2000vby.googlegroups.com> Date: Tue, 17 May 2011 14:59:06 -0700 Subject: Re: Faster Recursive Fibonacci Numbers From: geremy condra To: Wolfram Hinderer Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 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: 39 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1305669555 news.xs4all.nl 49179 [::ffff:82.94.164.166]:49623 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:5623 On Tue, May 17, 2011 at 2:04 PM, Wolfram Hinderer wrote: > On 17 Mai, 20:56, geremy condra wrote: >> On Tue, May 17, 2011 at 10:19 AM, Jussi Piitulainen >> >> wrote: >> > geremy condra writes: >> >> >> or O(1): >> >> >> =F6 =3D (1 + sqrt(5)) / 2 >> >> def fib(n): >> >> =A0 =A0 numerator =3D (=F6**n) - (1 - =F6)**n >> >> =A0 =A0 denominator =3D sqrt(5) >> >> =A0 =A0 return round(numerator/denominator) >> >> >> Testing indicates that it's faster somewhere around 7 or so. >> >> > And increasingly inaccurate from 71 on. >> >> Yup. That's floating point for you. For larger values you could just >> add a linear search at the bottom using the 5f**2 +/- 4 rule, which >> would still be quite fast out to about 10 times that. The decimal >> module gets you a tiny bit further, and after that it's time to just >> use Dijkstra's, like rusi suggested. In any event, I still think this >> formulation is the most fun ;). > > I think you can write it even more funny > > def fib(n): > =A0 =A0return round(((.5 + .5 * 5 ** .5) ** n - =A0(.5 - .5 * 5 ** .5) ** > n) * 5 ** -.5) > > ;-) Ok, that's amusing. It does hide the interaction with the golden ratio though, which is what I find so fascinating about the earlier one. Geremy Condra