Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!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.016 X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'identical': 0.07; 'raised': 0.07; 'oh,': 0.09; 'this:': 0.11; 'subject:error': 0.12; 'am,': 0.14; 'wrote:': 0.14; '[and': 0.16; 'fibonacci': 0.16; 'recursive': 0.16; 'row': 0.16; 'tighter': 0.16; 'case.': 0.16; 'subject:was': 0.16; 'describes': 0.19; 'occurred': 0.19; 'header :In-Reply-To:1': 0.22; 'pair': 0.23; 'version': 0.25; 'received:209.85.161.46': 0.26; 'received:mail- fx0-f46.google.com': 0.26; 'message-id:@mail.gmail.com': 0.28; 'received:209.85.161': 0.29; 'fri,': 0.29; 'operating': 0.30; 'probably': 0.30; 'this.': 0.30; 'to:addr:python-list': 0.32; 'actually': 0.34; 'starting': 0.34; 'change,': 0.35; 'think': 0.36; 'case': 0.37; 'received:209.85': 0.37; 'sequence': 0.38; 'received:google.com': 0.38; 'but': 0.38; 'not,': 0.39; 'to:addr:python.org': 0.39; 'subject: (': 0.39; 'received:209': 0.39; 'sets': 0.40; 'would': 0.40; "it's": 0.40; 'header:Received:5': 0.40; '13,': 0.60; 'happen': 0.61; '2011': 0.62; 'works,': 0.69; 'viewed': 0.77; '2x2': 0.84; 'matrix': 0.84; 'nice!': 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; bh=voI9eDwsqvLEu/3CshzzkV/E372EKKf99CcKc97dr28=; b=Iw1tYHOwB9uT3ctV+K0p+5KPMBBdy3ys2vWCjCH9hkRmj+bWmyDBXgm1JfSOqsO/jh QLoBk2nyY0T4+iw3+qj1SG6qNJKvNn21an33TMy2nTdHEAn+oYOxMQTvNJJs4P103CkU KiRb0nuaysbfeEf5zrBqPuQz5MOLDx2vvODlI= 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; b=u2pkOLoQ1S9bHZmkkz+KIrZBNZJGFSaLeTX31dgz+r2SXUKJV7DTXkAOTEYoRE9uKk bgkBroOsCz30m89lSHHmoYyxi8lXkaByOpP/OvwCQRO3i6uUrnVHS/GDam9ur2hElN9i lRbnCoUsjQcJm3foOa1dHTj4VDMmAACSMOJis= MIME-Version: 1.0 In-Reply-To: References: <0HFvp.18698$vT3.2042@newsfe06.iad> <6828b506-1e18-4bfa-85c0-caa830daf98a@j13g2000pro.googlegroups.com> <4dcb0943$0$81482$e4fe514c@news.xs4all.nl> From: Ian Kelly Date: Fri, 13 May 2011 07:55:51 -0600 Subject: Re: Recursion or iteration (was Fibonacci series recursion error) To: Python Content-Type: text/plain; charset=ISO-8859-1 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: 28 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1305294983 news.xs4all.nl 41103 [::ffff:82.94.164.166]:41611 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:5303 On Fri, May 13, 2011 at 5:11 AM, rusi wrote: > The tightest way I knew so far was this: > The 2x2 matrix > 0 1 > 1 1 > raised to the nth power gives the nth fibonacci number. [And then use > a logarithmic matrix mult] > Your version is probably tighter than this. Oh, nice! I did it this way once: V = [0 1] M = [0 1] [1 1] fib(n) = (V * M ** n)[0] Since I viewed M as operating on V, it never occurred to me that multiplying by V is actually unnecessary, but it is obvious in retrospect. I think it's just a fortuitous coincidence that it works, since V sets up the base case and M describes the recursive case. For a FIbonacci sequence starting with any other pair of numbers, V would change, but M would not, and so V would no longer happen to be identical to the top row of M. Ian