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


Groups > comp.lang.python > #5835

Re: Faster Recursive Fibonacci Numbers

Path csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed5.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.006
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'else:': 0.03; 'precision': 0.09; 'pm,': 0.10; 'def': 0.12; 'wrote:': 0.14; 'subject:Fibonacci': 0.16; 'sure,': 0.16; 'math': 0.16; 'cheers,': 0.19; 'header:In-Reply-To:1': 0.21; 'seems': 0.21; 'thu,': 0.22; 'so.': 0.22; 'memory': 0.22; 'received:209.85.161.46': 0.23; 'received:mail-fx0-f46.google.com': 0.23; 'charset:iso-8859-7': 0.25; 'function': 0.25; 'received:209.85.161': 0.26; 'message- id:@mail.gmail.com': 0.28; 'beyond': 0.28; 'import': 0.29; 'least': 0.30; 'decimal': 0.30; 'steven': 0.32; 'to:addr:python- list': 0.33; "d'aprano": 0.35; 'using': 0.35; 'received:google.com': 0.37; 'received:209.85': 0.37; 'subject:: ': 0.38; 'some': 0.38; 'skip:s 20': 0.39; 'received:209': 0.39; 'to:addr:python.org': 0.39; 'become': 0.72; '11:58': 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:from:date :message-id:subject:to:content-type:content-transfer-encoding; bh=7xftggW/5BWWybofzK4DA378fY6n5oYa14iLiOkBOxs=; b=EvI6SCxTcNpBGCBy1+9Yv7qfjaX3u70V4a0IWMfFm9BcZScqKlMjGQTQuiidrmg1er hGkPGa9TyO/MEOeGMBEf3QF8RVKjJU4Ac2s00F/Kg3m2inGfMeYc8Fz6P9r8dWWun06B Srak/UP2QMfT60/I71y6MCDFmfjLKtgdpWdfg=
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=czi2JqcXWfXo4Fr3AfvfCgSO72LmguLkIXIlbl0y7teUsIt25Ds+H+KvMuam9a1Jiv 2kPQYPCn63OJTAFV1OarF4zyBYXKt39j1hxL9gJ/5QF11RHaYEgeqTDzr3tUHamSDqcM 1EsOFThG7jKAVhH4d4C5IGYn7YTXsz18jX3eY=
MIME-Version 1.0
In-Reply-To <4dd602ec$0$29996$c3e8da3$5496439d@news.astraweb.com>
References <108ce447-10fa-4cf7-84ef-440ee18dbfd4@22g2000prx.googlegroups.com> <9d9c163b-14fd-4131-81bb-105c3b97c432@h36g2000pro.googlegroups.com> <mailman.1680.1305651743.9059.python-list@python.org> <4dd59e1e$0$29996$c3e8da3$5496439d@news.astraweb.com> <mailman.1806.1305848281.9059.python-list@python.org> <4dd602ec$0$29996$c3e8da3$5496439d@news.astraweb.com>
From Ian Kelly <ian.g.kelly@gmail.com>
Date Fri, 20 May 2011 01:26:49 -0600
Subject Re: Faster Recursive Fibonacci Numbers
To Python <python-list@python.org>
Content-Type text/plain; charset=ISO-8859-7
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.1829.1305876440.9059.python-list@python.org> (permalink)
Lines 38
NNTP-Posting-Host 82.94.164.166
X-Trace 1305876441 news.xs4all.nl 49038 [::ffff:82.94.164.166]:55646
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:5835

Show key headers only | View raw


On Thu, May 19, 2011 at 11:58 PM, Steven D'Aprano
<steve+comp.lang.python@pearwood.info> wrote:
> Sure, which is why the above fib() function will become increasing
> inaccurate beyond some given n, by memory about n=71 or so. Er, at least
> the fib() function that *was* above until you deleted most of it :)
>
> If you want an *accurate* fib() function using exponentiation of φ, you
> need arbitrary precision non-integers.

This seems to work for arbitrary n, although I only tested it out to
about n=2000.

from decimal import Decimal, localcontext
from math import sqrt

def fib(n):
    if n <= 70:
        return fib_float(n)
    else:
        return fib_decimal(n)

phi_float = (1 + sqrt(5)) / 2
def fib_float(n):
    numerator = (phi_float ** n) - (1 - phi_float) ** n
    denominator = sqrt(5)
    return int(round(numerator / denominator))

def fib_decimal(n):
    with localcontext() as ctx:
        ctx.prec = n // 4 + 1
        sqrt_5 = Decimal('5').sqrt()
        phi = (1 + sqrt_5) / 2
        numerator = (phi ** n) - (1 - phi) ** n
        return int((numerator / sqrt_5).to_integral_value())

Cheers,
Ian

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