Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed5.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.038 X-Spam-Evidence: '*H*': 0.92; '*S*': 0.00; 'kelly': 0.09; 'precision': 0.09; 'def': 0.12; 'subject:Fibonacci': 0.16; 'header:In-Reply- To:1': 0.21; 'received:209.85.161.46': 0.23; 'received:mail- fx0-f46.google.com': 0.23; 'received:209.85.161': 0.26; 'message- id:@mail.gmail.com': 0.28; 'to:addr:python-list': 0.33; 'received:google.com': 0.37; 'received:209.85': 0.37; 'subject:: ': 0.38; 'skip:s 20': 0.39; 'received:209': 0.39; 'to:addr:python.org': 0.39; 'believe': 0.66 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=7abFqa/vXZO2DpKNQSUxV39rG35yvEItfeKEtSRRT3o=; b=ET9hvoKXUzMK21UXl+7FRvH6K3b1fX6Uipivfo3UynxDIpwg0n6JHCjnLMDsIouzZ3 mR8hOz8wfgRfSUpF/UdQefYahi7gRj3A5OlIWnEthqMlPZU+NRYyg7IxUDrvCWJs1xsK 3Rc1Pq1GlWEertNlnOj+YY2PCRH6XxOGZVjQg= 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=dNSYP3olHYgTmtUUrVEQFFOcdFUrsylkX4qTYUvGmX/tSjHYHhKmaYYAi6cKxKPQtS sRctqx4Sr1kSBaj2gczEcCHoKrF/D+r9KMyc44yhV1Iz6Q4CbeNpctJJYNfbuUYLSUbL intM3rRi012Tg4AcmPJ7TQQ/6D7o0k6ltBRjM= MIME-Version: 1.0 In-Reply-To: References: <108ce447-10fa-4cf7-84ef-440ee18dbfd4@22g2000prx.googlegroups.com> <9d9c163b-14fd-4131-81bb-105c3b97c432@h36g2000pro.googlegroups.com> <4dd59e1e$0$29996$c3e8da3$5496439d@news.astraweb.com> <4dd602ec$0$29996$c3e8da3$5496439d@news.astraweb.com> From: Ian Kelly Date: Fri, 20 May 2011 01:32:52 -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: 11 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1305876803 news.xs4all.nl 49046 [::ffff:82.94.164.166]:47113 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:5836 2011/5/20 Ian Kelly : > def fib_decimal(n): > =A0 =A0with localcontext() as ctx: > =A0 =A0 =A0 =A0ctx.prec =3D n // 4 + 1 > =A0 =A0 =A0 =A0sqrt_5 =3D Decimal('5').sqrt() > =A0 =A0 =A0 =A0phi =3D (1 + sqrt_5) / 2 > =A0 =A0 =A0 =A0numerator =3D (phi ** n) - (1 - phi) ** n > =A0 =A0 =A0 =A0return int((numerator / sqrt_5).to_integral_value()) Note that I believe this is O(n) since the necessary precision grows linearly with n.