Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!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.005 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'else:': 0.03; 'algorithm': 0.04; 'elif': 0.05; 'subject:missing': 0.07; 'worse': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'def': 0.12; 'suggest': 0.14; '24,': 0.16; 'expecting': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'loop.': 0.16; 'normally,': 0.16; 'rewriting': 0.16; 'sequence,': 0.16; "someone's": 0.16; 'elements': 0.16; 'subject: ?': 0.16; 'wrote:': 0.18; 'example': 0.22; 'cc:addr:python.org': 0.22; 'print': 0.22; 'diverse': 0.24; 'cc:2**0': 0.24; 'sort': 0.25; 'possibly': 0.26; 'header:In-Reply-To:1': 0.27; 'point': 0.28; '[1]': 0.29; 'am,': 0.29; 'message-id:@mail.gmail.com': 0.30; 'subject:what': 0.31; 'regular': 0.32; 'run': 0.32; 'running': 0.33; 'could': 0.34; 'display': 0.35; 'something': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'version': 0.36; 'doing': 0.36; 'should': 0.36; 'skip:[ 10': 0.38; 'till': 0.61; 'entire': 0.61; "you're": 0.61; 'reach': 0.63; 'such': 0.63; 'more': 0.64; 'note:': 0.66; 'mar': 0.68; 'to,': 0.72; '2015': 0.84; 'safer': 0.84; 'wanted,': 0.84; 'fibonacci': 0.91; 'inefficient': 0.91; 'to:none': 0.92 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=P8mrUFVJ8g7R5xTcVcF23foOV0jdWMO2m0qWeemUr/c=; b=guQCK5OZulN5g59UoszIo7V50x9BdHInhMEbLwtYigCloP2uCoUZXK0/TVkIkOvmVK gGn2/6LOGwmF4iTx7VPDfvuyB8l9+eerF9DQw1RwEz6zcs9il5R1rbNdQwQcwIbadTOu i+n1nvzz+HxzVXz25jU71lB5KE4J0QMSMkS7d/MVAWLXu3znkSvkxFtilp2QNpopQf7Y ePjaJIRsV2eaBvzjPi0DuQpRr/qW9pZuAcxbKgAxKr19T/L5BVA5+vU8YlXsfZxqI2C7 nGhiS9VZva1lwYmG5GT8qqvmnI32o0e2i0YLum9bHKG7I+7Fjw6Fcs4aU/i4/UvRnCpW 7NLw== MIME-Version: 1.0 X-Received: by 10.107.160.212 with SMTP id j203mr26044426ioe.43.1427127408069; Mon, 23 Mar 2015 09:16:48 -0700 (PDT) In-Reply-To: References: Date: Tue, 24 Mar 2015 03:16:48 +1100 Subject: Re: fibonacci series what Iam is missing ? From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.19 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: 42 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1427127417 news.xs4all.nl 2927 [2001:888:2000:d::a6]:60834 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:87834 On Tue, Mar 24, 2015 at 3:01 AM, 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 ? What you're doing is printing out the fifth Fibonacci number. So there are three things to note: 1) To display the entire sequence, you will need some sort of loop. 2) Your algorithm is about as hopelessly inefficient as it could possibly be, barring deliberate intent. [1] 3) You are running Python 2.x; unless you have a good reason not to, it's better to use Python 3. 4) You're running as root. Normally, something like this should be run as a regular user - it's safer that way. Err, *amongst* the things to note are such diverse elements as... etcetera, etcetera. Ahem. I would suggest rewriting this as a much more straight-forward loop; begin at the beginning, go on till you reach the point you wanted, then stop. ChrisA [1] Cue the demonstration of a worse version from someone's student?