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


Groups > comp.lang.python > #87833 > unrolled thread

Re: fibonacci series what Iam is missing ?

Started byDave Angel <davea@davea.name>
First post2015-03-23 12:16 -0400
Last post2015-03-23 12:16 -0400
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: fibonacci series what Iam is missing ? Dave Angel <davea@davea.name> - 2015-03-23 12:16 -0400

#87833 — Re: fibonacci series what Iam is missing ?

FromDave Angel <davea@davea.name>
Date2015-03-23 12:16 -0400
SubjectRe: fibonacci series what Iam is missing ?
Message-ID<mailman.71.1427127401.10327.python-list@python.org>
On 03/23/2015 12:01 PM, Ganesh Pal wrote:
> Hello team ,
>
>
> [root@localhost Python]# cat  fibonacci-Sequence-3.py
>
> ## Example 2: Using recursion
> def fib(n):
>      if n == 0:
>          return 0
>      elif n == 1:
>          return 1
>      else:
>          return fib(n-1) + fib(n-2)
> print fib(5)
>
> # python  fibonacci-Sequence-3.py
> 5
>
> what Iam I missing in the program , I was expecting 0,1,1,2,3 ?
>

You're missing the loop at top-level.  The function as written 
calculates a single value, by using recursion.  While it's true that the 
function also calculates the lower-numbered values, it doesn't print them.

Printing is (rightly) only done at the top-level, and that's where you'd 
need a loop.

An entirely separate question is whether you can gain performance by 
caching intermediate values.  For example, if you capture values in a 
list, you could potentially save a lot of time, at least for non-trivial 
values of n.

-- 
DaveA

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web