Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'else:': 0.03; 'closer': 0.07; 'prints': 0.07; 'script,': 0.07; '>>>>': 0.09; 'all).': 0.09; 'recursion': 0.09; 'pm,': 0.11; '>>>': 0.12; 'exception': 0.12; 'subject:error': 0.12; 'def': 0.13; 'wrote:': 0.14; 'branching': 0.16; 'depth.': 0.16; 'head,': 0.16; 'rachel': 0.16; 'stack': 0.16; 'cc:no real name:2**0': 0.20; 'cc:2**0': 0.20; 'header:In-Reply-To:1': 0.22; 'cc:addr:python-list': 0.22; 'mon,': 0.22; '(not': 0.24; '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; 'cc:addr:python.org': 0.31; 'depth': 0.31; 'import': 0.32; 'called': 0.32; 'another': 0.32; '...': 0.32; 'execution': 0.33; 'there': 0.35; 'print': 0.35; 'forces': 0.35; 'case': 0.37; 'received:209.85': 0.37; 'other,': 0.38; 'received:google.com': 0.38; 'but': 0.38; 'received:209': 0.39; 'similar': 0.40; 'header:Received:5': 0.40; 'factor': 0.60; '2011': 0.62; 'maximum': 0.62; 'ever': 0.65; 'believe': 0.66; 'double': 0.69; 'speed,': 0.84; 'to:addr:charter.net': 0.84; 'look.': 0.93; 'on...': 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:cc:content-type:content-transfer-encoding; bh=0C4+EHrIh/2l45IpOYD+FRObGdluuw6RACsjjqJfrnk=; b=cbBc3HeRlX0Rgw5cI7IIuendfofFnHzTV4+lmNCR5Qnl06RVzKGS5OopUePz0c8rFB QE7L1x1IiA9yIGOAtvXi3dkNnqVGbbb5rgy/bjQG72PF1cXwbkclvQ+EAQnqI2tyhovC VFPskKyDERBuMHZyZXneOHDK2NOmIAO1LqG7o= 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 :cc:content-type:content-transfer-encoding; b=MHZ6qEIECSjq0kQ/E5l2cRg1W3YyW+BmzynB0ABd4wArV+s8kf3bnAwrRcnXT4Er6u FJhidq4Ach/OHMmI/kximrzVESbO6CwmRJpgIgTjVfyJX9Pgs4188xP8GbjO49koGCAI 1du+w1Z44GDm4IsGpfR9JYBdrthta+iJS5eBs= MIME-Version: 1.0 In-Reply-To: <0HFvp.18698$vT3.2042@newsfe06.iad> References: <0HFvp.18698$vT3.2042@newsfe06.iad> From: Ian Kelly Date: Mon, 2 May 2011 16:22:35 -0600 Subject: Re: Fibonacci series recursion error To: harrismh777 Content-Type: text/plain; charset=ISO-8859-1 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 54 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1304375554 news.xs4all.nl 41114 [::ffff:82.94.164.166]:38021 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:4507 On Mon, May 2, 2011 at 3:50 PM, harrismh777 wrote= : > Thomas Rachel wrote: >>> >>> ... because each recursion level 'return' calls fib() twice, and each o= f >>> those calls fib() twice, and you get the point... >> >> yes - but they are called one after the other, so the "twice" call >> counts only for execution speed, not for recursion depth. > >>>> def fib(n): >>>> =A0 =A0 if n =3D=3D 1: >>>> =A0 =A0 =A0 =A0 return(n) >>>> =A0 =A0 else: >>>> =A0 =A0 =A0 =A0 return (fib(n-1)+fib(n-2)) > > They *are not* called one after the other in the sense that there is ever > only one level of recursion with each call (not at all). Take a closer lo= ok. > Each call to fib() forces a double head, and each of those forces another > double head (now four), and so on... The branching factor has nothing to do with the maximum stack depth. If you don't believe me, consider this little script: import inspect def maxstack(n): if n <=3D 0: return len(inspect.stack()) else: return max(maxstack(n-1), maxstack(n-2)) print maxstack(15) This prints "17". Now consider this script, which is similar but singly-recursive: import inspect def maxstack(n): if n <=3D 0: return len(inspect.stack()) else: return maxstack(n-1) print maxstack(15) This also prints "17". Note: they're the same. > =A0the recursion depth exception of the > OP testifies against your theory. The OP's recursion depth exception was because a malformed base case made the recursion infinite. It had nothing to do with the branching factor.