943,605 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 13095
  • Python RSS
Sep 20th, 2007
0

Fibonacci Sequence

Expand Post »
I've been asked to write a program that computes the nth Fibonacci number where n is a value input by the user. For example, if n = 6, then the result is 8.
This is what I have so far:

def main():
print "This program will compute nth Fibonacci"
print "number where n is a value input by the user."

x = input("Enter the nth Fibonacci number: ")
s = 1

for i in range(x):
s = s + x

s = s + 1

print "The result is:", s

main()

I believe I'm missing a variable and I can't figure how to get the previous number to add to the next number.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
MakingMoney is offline Offline
6 posts
since Sep 2007
Sep 20th, 2007
1

Re: Fibonacci Sequence

Preliminary tip: encase your code in [ code="Python ] and [/code] tags (no spaces on the first tag) so that it'll look pretty.


This is a classic case of recursion:

fib(n) = fib(n-1) + fib(n-2)

The best way to compute it is therefore recursively:

Python Syntax (Toggle Plain Text)
  1. >>> def fib(n):
  2. if n <= 0:
  3. return 0
  4. elif n == 1:
  5. return 1
  6. else:
  7. return fib(n-1) + fib(n-2)

This isn't terribly efficient, and perhaps you were supposed to do it with a loop?

Jeff
Reputation Points: 92
Solved Threads: 156
Practically a Master Poster
jrcagle is offline Offline
608 posts
since Jul 2006
Sep 20th, 2007
0

Re: Fibonacci Sequence

Try looking in the code snippet section of Daniweb to get an idea. There is one in there.
Featured Poster
Reputation Points: 533
Solved Threads: 53
Senior Poster
jasimp is offline Offline
3,593 posts
since Aug 2007
Sep 20th, 2007
0

Re: Fibonacci Sequence

If you want to access or print all the numbers, append them to a list
test_list = [1, 2] ## Fibs can be started with any 2 numbers
test_list.append( test_list[-1]+test_list[-2] )
print test_list
Last edited by woooee; Sep 20th, 2007 at 10:06 pm.
Reputation Points: 741
Solved Threads: 691
Nearly a Posting Maven
woooee is offline Offline
2,302 posts
since Dec 2006
Sep 20th, 2007
0

Re: Fibonacci Sequence

For Python code on Daniweb:
Please use the [code=python] and [/code] tag pair to enclose your python code.
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Mar 13th, 2011
0
Re: Fibonacci Sequence
The cool and simplest program to get fibonacci series is

Python Syntax (Toggle Plain Text)
  1. n=input('enter the value of n')
  2. a=1
  3. b=2
  4. i=0
  5. print '1'
  6. print '2'
  7. for i in range (0,n-2):
  8. c=a+b
  9. print c
  10. a=b
  11. b=c

compiled by__ Ranjith127 & Varun100
Last edited by Ezzaral; Mar 13th, 2011 at 2:26 pm. Reason: Added code tags. Please use them to format all code that you post.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
varun100 is offline Offline
1 posts
since Mar 2011
Nov 19th, 2011
0

py

This is what I would do:
python Syntax (Toggle Plain Text)
  1. # Filename: fib.py
  2. #!usr/bin/pyton
  3. a = int(input('Enter length: '))
  4. x = 0
  5. y = 1
  6. for i in range(a):
  7. print(x)
  8. print(y)
  9. x = x + y
  10. y = x + y
This is in python 3.1.3
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Christopher A. is offline Offline
1 posts
since Nov 2011
Nov 19th, 2011
0
Re: Fibonacci Sequence
This is very old thread, if you have questions or want critique of your code, start your own fresh thread. Also the OP of this thread wanted to produce single value for nth fibonacci number. That can be made for example like this:
Python Syntax (Toggle Plain Text)
  1. def fib(n):
  2. prev = [0, 1]
  3. while n>1:
  4. prev = prev[-1], sum(prev)
  5. n -= 1
  6. return prev[n]
Featured Poster
Reputation Points: 690
Solved Threads: 748
Industrious Poster
pyTony is offline Offline
4,201 posts
since Apr 2010

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: write strings
Next Thread in Python Forum Timeline: Running a subprocess and waiting for it to return.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC