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


Groups > comp.lang.python > #87833

Re: fibonacci series what Iam is missing ?

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <davea@davea.name>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.005
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'else:': 0.03; 'value,': 0.04; 'elif': 0.05; 'intermediate': 0.07; 'subject:missing': 0.07; 'python': 0.11; 'def': 0.12; 'caching': 0.16; 'calculates': 0.16; 'expecting': 0.16; 'loop.': 0.16; 'non-trivial': 0.16; 'subject: ?': 0.16; 'wrote:': 0.18; 'written': 0.21; 'example': 0.22; 'separate': 0.22; 'print': 0.22; 'header:User-Agent:1': 0.23; 'question': 0.24; 'least': 0.26; 'values': 0.27; 'header:In-Reply- To:1': 0.27; 'function': 0.29; "doesn't": 0.30; 'subject:what': 0.31; 'values.': 0.31; 'entirely': 0.33; 'could': 0.34; 'done': 0.36; 'example,': 0.37; 'performance': 0.37; 'skip:[ 10': 0.38; 'to:addr:python-list': 0.38; 'list,': 0.38; 'pm,': 0.38; 'to:addr:python.org': 0.39; "you're": 0.61; 'save': 0.62; 'charset:windows-1252': 0.65; 'received:74.208': 0.68; 'gain': 0.79; 'potentially': 0.81; 'received:74.208.4.194': 0.84; 'capture': 0.91
Date Mon, 23 Mar 2015 12:16:29 -0400
From Dave Angel <davea@davea.name>
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.5.0
MIME-Version 1.0
To python-list@python.org
Subject Re: fibonacci series what Iam is missing ?
References <CACT3xuVF-MOVQK5wSorRSAfpAQEXvS7QsqarACVZKXsQ0bxzpQ@mail.gmail.com>
In-Reply-To <CACT3xuVF-MOVQK5wSorRSAfpAQEXvS7QsqarACVZKXsQ0bxzpQ@mail.gmail.com>
Content-Type text/plain; charset=windows-1252; format=flowed
Content-Transfer-Encoding 7bit
X-Provags-ID V03:K0:XrZzCC4qHKZfSxz2uFTabmKDZhUO2c/0VgH1ux0qWatbkkXYHEo Hih2MlDfpj5huBPxhqwEl+0Wcv76UlBSPcsbd05wEU0yVie9rja18IwOpNgYxT4eyW/iEU9 m1YOSFMuDRwoUHjViJATwSgo9yqRwa5JRGd5XXLUqyuPwC6pOnHkd3y7l4itiHG0daSEnZG hCjgOE30SNAZ9FhVQ7PBA==
X-UI-Out-Filterresults notjunk:1;
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.19
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.71.1427127401.10327.python-list@python.org> (permalink)
Lines 36
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1427127401 news.xs4all.nl 2927 [2001:888:2000:d::a6]:60524
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:87833

Show key headers only | View raw


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

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

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

csiph-web