Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #5581

Re: Faster Recursive Fibonacci Numbers

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!xlned.com!feeder5.xlned.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <debatem1@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'python.': 0.05; 'arguments': 0.05; 'elif': 0.05; 'behavior.': 0.07; 'linear': 0.07; ':-)': 0.07; '"""': 0.09; 'function:': 0.09; 'indicates': 0.09; 'recursion': 0.09; 'somewhere': 0.11; 'c++': 0.12; 'def': 0.13; 'am,': 0.14; 'wrote:': 0.14; '"""a': 0.16; '(b,': 0.16; 'adjacent': 0.16; 'cant': 0.16; 'exponential': 0.16; 'fibonacci': 0.16; 'identities': 0.16; 'recursive': 0.16; 'rewrote': 0.16; 'simple:': 0.16; 'charset:iso-8859-7': 0.16; 'tue,': 0.20; 'cc:no real name:2**0': 0.20; 'cc:2**0': 0.20; 'discussion': 0.20; 'header:In-Reply-To:1': 0.22; 'cc:addr:python- list': 0.22; 'so.': 0.22; '\xa0if': 0.23; 'define': 0.26; 'there.': 0.26; 'url:mailman': 0.27; 'function': 0.27; 'message- id:@mail.gmail.com': 0.28; 'testing': 0.28; 'url:edu': 0.28; 'noticed': 0.29; 'problem': 0.29; 'rid': 0.29; 'cc:addr:python.org': 0.31; '17,': 0.31; 'trick': 0.31; 'idea': 0.32; "i've": 0.33; 'url:listinfo': 0.33; 'page': 0.33; 'couple': 0.35; 'put': 0.35; 'received:209.85.216.46': 0.35; 'received:mail- qw0-f46.google.com': 0.35; 'quite': 0.36; 'rather': 0.36; 'some': 0.37; 'should': 0.37; 'received:209.85': 0.37; 'url:python': 0.37; 'skip:- 10': 0.37; 'faster': 0.38; 'received:google.com': 0.38; 'but': 0.38; 'skip:" 20': 0.38; 'url:org': 0.38; 'it!': 0.39; 'set': 0.39; 'where': 0.39; 'received:209': 0.39; 'how': 0.39; 'skip:- 60': 0.39; 'behavior': 0.40; "it's": 0.40; 'header:Received:5': 0.40; 'vol': 0.60; 'url:10': 0.60; 'give': 0.61; '2011': 0.62; 'easy.': 0.68; 'double': 0.69; 'formula': 0.84; 'knuth': 0.84
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=2lG6AXpNwEyA7IJJDcvZbREzrCV/mhnZjEQRDoEtirQ=; b=xMKf1fbU3xCpWm1CypR+6MKXfIsuOXplKMyhaw6OiT20UafITK+7i/psRtXjH4ZO/f 2yokKwFZJTANgA/51BqgsHjjCNW0Gi6U3Hh1B1d9WvHVT8KW5xemS9knBKAEFs9wwE6D 2kGHDRAebLT/WXNMtqYThC5dnImIgTkK1ND40=
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=qvKJB0oQh6fEW8aU8fNSG6puIhHBVnndisps7lbhqqGdabVd6i7Z1UaZOCM8H01QED jqb+LVNaElu1RTPMNDvCPo32Nwz6/Usdcdl+bzDj6LpnGr4w97d3ZppfBBArA1xMlyeG QdVF2e17ZNGuMSlr4c/a5wciE8Jtxiy2uST/8=
MIME-Version 1.0
In-Reply-To <9d9c163b-14fd-4131-81bb-105c3b97c432@h36g2000pro.googlegroups.com>
References <108ce447-10fa-4cf7-84ef-440ee18dbfd4@22g2000prx.googlegroups.com> <9d9c163b-14fd-4131-81bb-105c3b97c432@h36g2000pro.googlegroups.com>
Date Tue, 17 May 2011 10:02:21 -0700
Subject Re: Faster Recursive Fibonacci Numbers
From geremy condra <debatem1@gmail.com>
To rusi <rustompmody@gmail.com>
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 <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.1680.1305651743.9059.python-list@python.org> (permalink)
Lines 83
NNTP-Posting-Host 82.94.164.166
X-Trace 1305651744 news.xs4all.nl 49039 [::ffff:82.94.164.166]:51691
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:5581

Show key headers only | View raw


On Tue, May 17, 2011 at 9:36 AM, rusi <rustompmody@gmail.com> wrote:
> On May 17, 8:50 pm, RJB <rbott...@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
>>
>> RJB the Lurkerhttp://www.csci.csusb.edu/dick/cs320/lab/10.html
>
> -------------------------------------------------------------
> Its an interesting problem and you are 75% there.
> You see the halving gives you logarithmic behavior and the double
> calls give exponential behavior.
>
> So how to get rid of double calls?  Its quite simple: Just define your
> function in terms of return pairs of adjacent pairs ie (fib(n), fib(n
> +1)) for some n rather then a single number fib(n)
>
> Here's a straightforward linear function:
>
> def fp(n):  #fibpair
>    if n==1:
>        return (1,1)
>    else:
>        a,b = fp(n-1)
>        return (b, a+b)
>
> def fib(n):
>    a,b = fp(n)
>    return a
>
> ---------------
> Now use this (pairing) idea with your (halving) identities and you
> should get a logarithmic algo.
>
> [If you cant do it ask again but yes its fun to work out so do
> try :-) ]
> --
> http://mail.python.org/mailman/listinfo/python-list
>

or O(1):

φ = (1 + sqrt(5)) / 2
def fib(n):
    numerator = (φ**n) - (1 - φ)**n
    denominator = sqrt(5)
    return round(numerator/denominator)

Testing indicates that it's faster somewhere around 7 or so.

Geremy Condra

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


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