Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!selfless.tophat.at!newsfeed.xs4all.nl!newsfeed6.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.010 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'event,': 0.07; 'linear': 0.07; 'received:mail-qy0-f174.google.com': 0.07; 'indicates': 0.09; 'somewhere': 0.11; 'def': 0.13; 'am,': 0.14; 'wrote:': 0.14; 'received:209.85.216.174': 0.16; 'suggested.': 0.16; 'charset:iso-8859-7': 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; 'bit': 0.33; 'module': 0.33; 'using': 0.34; 'point': 0.35; 'quite': 0.36; 'think': 0.36; 'received:209.85': 0.37; 'faster': 0.38; 'received:google.com': 0.38; 'larger': 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; 'tiny': 0.69; '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=EI11uYtEyD64NRYzoTJ9qiUUy3oybcK9thWel/vtnfU=; b=ogrWL4W70l8T7muiW/krUxXg7WnZT5d19A32exu9RXWHR1g0/aA+/k83CR12ka9Tpd /eHF+EUCKExkcnquG+PieElEGS/9o7oMJsmjpSXzeYyUHGorgKowPKGZnyniduhD/xCE 149+16+6HRf5hDmfGSH2e5gFrLBuo2CiV4MHc= 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=CunDzNpZmYM9LG+Lld0T2PEygEVjlaZpxGtXefr3/W/W96uO1a34mWDbRCIYicB+I4 hKDQlpz8VA4XR+F2SrK56mEmkiBCaiUtInWCPqjboQfWV8NqqmYct/Fx+UrHP0zbJwce syYAJ32x893krN1RPFZYmOgbF1MpI1RRmgFWc= MIME-Version: 1.0 In-Reply-To: References: <108ce447-10fa-4cf7-84ef-440ee18dbfd4@22g2000prx.googlegroups.com> <9d9c163b-14fd-4131-81bb-105c3b97c432@h36g2000pro.googlegroups.com> Date: Tue, 17 May 2011 11:56:33 -0700 Subject: Re: Faster Recursive Fibonacci Numbers From: geremy condra To: Jussi Piitulainen Content-Type: text/plain; charset=ISO-8859-7 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: 24 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1305658597 news.xs4all.nl 49183 [::ffff:82.94.164.166]:34330 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:5595 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 ;). Geremy Condra