Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #5574
| 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 | <ian.g.kelly@gmail.com> |
| 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 <ian.g.kelly@gmail.com> |
| Date | Tue, 17 May 2011 10:23:48 -0600 |
| Subject | Re: Faster Recursive Fibonacci Numbers |
| To | Python <python-list@python.org> |
| 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 <python-list.python.org> |
| List-Unsubscribe | <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.1678.1305649459.9059.python-list@python.org> (permalink) |
| 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 |
Show key headers only | View raw
On Tue, May 17, 2011 at 9:50 AM, RJB <rbotting@csusb.edu> 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. It was easy. Enjoy. And tell me how I can improve it! > > def fibo(n): > """A Faster recursive Fibonaci function > Use a formula from Knuth Vol 1 page 80, section 1.2.8: > If F[n] is the n'th Fibonaci number then > F[n+m] = F[m]*F[n+1] + F[m-1]*F[n]. > First set m = n+1 > F[ 2*n+1 ] = F[n+1]**2 + F[n]*2. > > Then put m = n in Knuth's formula, > F[ 2*n ] = F[n]*F[n+1] + F[n-1]* F[n], > and replace F[n+1] by F[n]+F[n-1], > F[ 2*n ] = F[n]*(F[n] + 2*F[n-1]). > """ > if n<=0: > return 0 > elif n<=2: > return 1 > elif n%2==0: > half=n//2 > f1=fibo(half) > f2=fibo(half-1) > return f1*(f1+2*f2) > else: > nearhalf=(n-1)//2 > f1=fibo(nearhalf+1) > f2=fibo(nearhalf) > return 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
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Faster Recursive Fibonacci Numbers RJB <rbotting@csusb.edu> - 2011-05-17 08:50 -0700
Re: Faster Recursive Fibonacci Numbers Ian Kelly <ian.g.kelly@gmail.com> - 2011-05-17 10:23 -0600
Re: Faster Recursive Fibonacci Numbers rusi <rustompmody@gmail.com> - 2011-05-17 09:25 -0700
Re: Faster Recursive Fibonacci Numbers rusi <rustompmody@gmail.com> - 2011-05-17 09:36 -0700
Re: Faster Recursive Fibonacci Numbers geremy condra <debatem1@gmail.com> - 2011-05-17 10:02 -0700
Re: Faster Recursive Fibonacci Numbers Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2011-05-17 20:19 +0300
Re: Faster Recursive Fibonacci Numbers geremy condra <debatem1@gmail.com> - 2011-05-17 11:56 -0700
Re: Faster Recursive Fibonacci Numbers Wolfram Hinderer <wolfram.hinderer@googlemail.com> - 2011-05-17 14:04 -0700
Re: Faster Recursive Fibonacci Numbers geremy condra <debatem1@gmail.com> - 2011-05-17 14:59 -0700
Re: Faster Recursive Fibonacci Numbers rusi <rustompmody@gmail.com> - 2011-05-17 21:43 -0700
Re: Faster Recursive Fibonacci Numbers Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-05-19 22:47 +0000
Re: Faster Recursive Fibonacci Numbers Chris Angelico <rosuav@gmail.com> - 2011-05-20 09:37 +1000
Re: Faster Recursive Fibonacci Numbers Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-05-20 05:58 +0000
Re: Faster Recursive Fibonacci Numbers Chris Angelico <rosuav@gmail.com> - 2011-05-20 16:06 +1000
Re: Faster Recursive Fibonacci Numbers harrismh777 <harrismh777@charter.net> - 2011-05-20 01:26 -0500
Re: Faster Recursive Fibonacci Numbers Chris Angelico <rosuav@gmail.com> - 2011-05-20 16:54 +1000
Re: Faster Recursive Fibonacci Numbers Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-05-20 17:07 +0000
Re: Faster Recursive Fibonacci Numbers geremy condra <debatem1@gmail.com> - 2011-05-20 11:43 -0700
Re: Faster Recursive Fibonacci Numbers Chris Angelico <rosuav@gmail.com> - 2011-05-21 10:44 +1000
Re: Faster Recursive Fibonacci Numbers Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2011-05-21 20:49 +1200
Re: Faster Recursive Fibonacci Numbers Ian Kelly <ian.g.kelly@gmail.com> - 2011-05-20 01:26 -0600
Re: Faster Recursive Fibonacci Numbers Ian Kelly <ian.g.kelly@gmail.com> - 2011-05-20 01:32 -0600
Re: Faster Recursive Fibonacci Numbers SigmundV <sigmundv@gmail.com> - 2011-05-20 03:53 -0700
Re: Faster Recursive Fibonacci Numbers geremy condra <debatem1@gmail.com> - 2011-05-19 17:03 -0700
Re: Faster Recursive Fibonacci Numbers Raymond Hettinger <python@rcn.com> - 2011-05-24 16:44 -0700
csiph-web