Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!feeder3.cambriumusenet.nl!feed.tweaknews.nl!194.109.133.85.MISMATCH!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.012 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'python.': 0.05; 'arguments': 0.05; '"""': 0.09; 'recursion': 0.09; 'c++': 0.12; 'def': 0.13; 'am,': 0.14; 'wrote:': 0.14; 'fibonacci': 0.16; 'recursive': 0.16; 'rewrote': 0.16; 'algorithm': 0.16; 'tue,': 0.20; 'discussion': 0.20; 'cheers,': 0.20; 'header:In-Reply-To:1': 0.22; '\xa0if': 0.23; 'received:209.85.161.46': 0.26; 'received :mail-fx0-f46.google.com': 0.26; 'function': 0.27; 'message- id:@mail.gmail.com': 0.28; 'looks': 0.28; 'thanks': 0.29; 'noticed': 0.29; 'received:209.85.161': 0.29; 'discussed': 0.29; '17,': 0.31; 'trick': 0.31; 'to:addr:python-list': 0.32; "i've": 0.33; 'page': 0.33; 'there': 0.35; 'couple': 0.35; 'put': 0.35; 'some': 0.37; 'received:209.85': 0.37; 'faster': 0.38; 'received:google.com': 0.38; 'but': 0.38; 'skip:" 20': 0.38; '8bit%:6': 0.39; 'it!': 0.39; 'set': 0.39; 'to:addr:python.org': 0.39; 'where': 0.39; 'received:209': 0.39; 'how': 0.39; 'header:Received:5': 0.40; 'might': 0.40; 'vol': 0.60; '2011': 0.62; 'easy.': 0.68; 'formula': 0.84; 'knuth': 0.84; 'look.': 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:from:date :message-id:subject:to:content-type:content-transfer-encoding; bh=YwEj+ffQri7PZ5PYvWGATlRxZBLaCWxWGUsrRkjCN30=; b=kzoXx1n+LAlBhp2s26ygCktpTo0yBx+mV4Qz/4ohHBILTbqDcw2O6SlQIDCpccewjG tUe+sJZ/Dq6rj9uiAHdggscSC7102xCCceShxVDPrBXRHAdNX6PiUiJOh2fbSTO1v3vU EaEISQ7SnelNcbKEp4ZQbiYq03IwASKdv80tg= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type:content-transfer-encoding; b=NSC4cat6WzQ8s0Rs6OkprRm8q5viPOV4e8TKGfgDLb1Z20DkuKZ2oLg204hsVgaKng uDr0Uwj8mnHdyoVkB5Kb5nIx8Ha7t9IZWJn+h0A3ckrTxitrhpMCpmMcKvsqSdj4tK9j Gi+xrmHSl7N1K7/vM4nPdl60VDBJvoOqcvpEA= MIME-Version: 1.0 In-Reply-To: <108ce447-10fa-4cf7-84ef-440ee18dbfd4@22g2000prx.googlegroups.com> References: <108ce447-10fa-4cf7-84ef-440ee18dbfd4@22g2000prx.googlegroups.com> From: Ian Kelly Date: Tue, 17 May 2011 10:23:48 -0600 Subject: Re: Faster Recursive Fibonacci Numbers To: Python Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable 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: 42 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1305649459 news.xs4all.nl 49183 [::ffff:82.94.164.166]:40595 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:5574 On Tue, May 17, 2011 at 9:50 AM, RJB wrote: > I noticed some discussion of recursion..... the trick is to find a > formula where the arguments are divided, not decremented. > I've had a "divide-and-conquer" recursion for the Fibonacci numbers > for a couple of years in C++ but just for fun rewrote it > in Python. =A0It was easy. =A0Enjoy. =A0And tell me how I can improve it! > > def fibo(n): > =A0 =A0 =A0 =A0"""A Faster recursive Fibonaci function > Use a formula from Knuth Vol 1 page 80, section 1.2.8: > =A0 =A0 =A0 =A0 =A0 If F[n] is the n'th Fibonaci number then > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 F[n+m] =3D F[m]*F[n+1] + F[m-1]*F[n]. > =A0First set m =3D n+1 > =A0 F[ 2*n+1 ] =3D F[n+1]**2 + F[n]*2. > > =A0Then put m =3D n in Knuth's formula, > =A0 =A0 =A0 =A0 =A0 F[ 2*n ] =3D F[n]*F[n+1] + F[n-1]* F[n], > =A0 and replace F[n+1] by F[n]+F[n-1], > =A0 =A0 =A0 =A0 =A0 F[ 2*n ] =3D F[n]*(F[n] + 2*F[n-1]). > """ > =A0 =A0 =A0 =A0if n<=3D0: > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0return 0 > =A0 =A0 =A0 =A0elif n<=3D2: > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0return 1 > =A0 =A0 =A0 =A0elif n%2=3D=3D0: > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0half=3Dn//2 > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0f1=3Dfibo(half) > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0f2=3Dfibo(half-1) > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0return f1*(f1+2*f2) > =A0 =A0 =A0 =A0else: > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0nearhalf=3D(n-1)//2 > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0f1=3Dfibo(nearhalf+1) > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0f2=3Dfibo(nearhalf) > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0return f1*f1 + f2*f2 Thanks for posting! Actually, it looks like this is the same O(n) algorithm that rusi posted. There was also a O(log n) algorithm discussed that is based on vector math. You might want to take a look. Cheers, Ian