Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!selfless.tophat.at!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.023 X-Spam-Evidence: '*H*': 0.95; '*S*': 0.00; 'subject:error': 0.12; 'def': 0.13; 'am,': 0.14; 'binary': 0.14; 'wrote:': 0.14; '1))': 0.16; '2))': 0.16; 'approximated': 0.16; 'exponential': 0.16; 'nodes': 0.16; 'yup,': 0.16; 'subject:was': 0.16; 'tree': 0.20; 'header:In-Reply-To:1': 0.22; 'calls.': 0.23; '\xa0if': 0.23; 'thus': 0.24; 'guess': 0.26; 'received:209.85.161.46': 0.26; 'received:mail-fx0-f46.google.com': 0.26; 'message- id:@mail.gmail.com': 0.28; "doesn't": 0.28; 'received:209.85.161': 0.29; 'sat,': 0.29; 'assuming': 0.29; 'algo': 0.31; 'to:addr :python-list': 0.32; 'actually': 0.34; 'case': 0.37; 'received:209.85': 0.37; 'received:google.com': 0.38; 'but': 0.38; 'to:addr:python.org': 0.39; 'subject: (': 0.39; 'received:209': 0.39; 'how': 0.39; 'would': 0.40; 'header:Received:5': 0.40; 'exact': 0.60; '2011': 0.62; 'strange': 0.65; 'double': 0.69; '11:24': 0.84; 'complexity': 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=YFGg3AT3sM9h10IcJcLfVZUIbi+MBXJ0mE3gxOMZ4Yo=; b=Zh6dEnQC+X8dZx1mX9TJJ++VnIm649VghilW/3X0Zhz+EK/6/DJmx4BqyA1aibhLE9 hLsyoBL71BFQbrfhA9v0JE/8EWP6zSYjW1TXbk/6gRGCawp0xlRKXblXv/d5jrHMrOL/ 4RZxmhaUEuUljP4xrbcDc4scRqylIvwfvdoy8= 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=n1WGE0R4maYag1M9zxnZusZTBLEZlQtWfMHCWRA2JhslaeES+3vS4SkeWiJl9a3iba TPC+ZNW2Xcrsa8DUL1IigpA8VDNvYo50ckUH1gKa5j3AwuNSTTFF48KDSmbJSgT4BAZq IAja8WYM44jnffpEuFEQehIoiMG//lgHWAaJg= MIME-Version: 1.0 In-Reply-To: <71093cb7-3eb4-47f7-8936-2591cdaf4688@l2g2000prg.googlegroups.com> References: <0HFvp.18698$vT3.2042@newsfe06.iad> <6828b506-1e18-4bfa-85c0-caa830daf98a@j13g2000pro.googlegroups.com> <4dcb0943$0$81482$e4fe514c@news.xs4all.nl> <71093cb7-3eb4-47f7-8936-2591cdaf4688@l2g2000prg.googlegroups.com> From: Ian Kelly Date: Sat, 14 May 2011 15:19:55 -0600 Subject: Re: Recursion or iteration (was Fibonacci series recursion error) 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: 18 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1305408027 news.xs4all.nl 81481 [::ffff:82.94.164.166]:34430 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:5388 On Sat, May 14, 2011 at 11:24 AM, rusi wrote: > def fib(n): > =A0 =A0if n=3D=3D1 or n=3D=3D2: > =A0 =A0 =A0 =A0return 1 > =A0 =A0elif even(n): > =A0 =A0 =A0 =A0return sq(fib (n//2)) + 2 * fib(n//2) * fib(n//2 - 1) > =A0 =A0else: > =A0 =A0 =A0 =A0return sq(fib (n//2 + 1)) + sq(fib(n // 2)) > > This is a strange algo =A0-- logarithmic because it halves the n, > exponential because of the double (triple) calls. =A0[I cannot say I > know how to work out its exact complexity but I would guess its about > linear] Yup, linear. Assuming you optimize the even case so that it doesn't actually call fib(n//2) twice, the call tree can be approximated as a balanced binary tree with height log(n). The total number of nodes in the tree is thus O(2 ** log(n)) =3D O(n).