| | |
Fibonacci Sequence
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Sep 2007
Posts: 6
Reputation:
Solved Threads: 0
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.
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.
•
•
Join Date: Jul 2006
Posts: 608
Reputation:
Solved Threads: 150
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:
This isn't terribly efficient, and perhaps you were supposed to do it with a loop?
Jeff
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)
>>> def fib(n): if n <= 0: return 0 elif n == 1: return 1 else: return fib(n-1) + fib(n-2)
This isn't terribly efficient, and perhaps you were supposed to do it with a loop?
Jeff
![]() |
Similar Threads
- C++ Fibonacci program help (C++)
- Fibonacci Sequence (VB.NET)
- I need help writing a program using Fibonacci sequence (C++)
- Having problem generating Fibonacci sequence (C)
Other Threads in the Python Forum
- Previous Thread: Using styles in PyGTK
- Next Thread: 3D objects like curve, sphere etc..
Views: 4913 | Replies: 4
| Thread Tools | Search this Thread |
Tag cloud for Python
abrupt apache application argv beginner binary calculator character code command cursor cx-freeze development dictionary dynamic error event examples excel file float format ftp function google gui hints homework ideas import input java keyboard launcher line linux list lists loop microphone mouse movingimageswithpygame newb number numbers obexftp output parsing path permissions phonebook port prime program programming projects py2exe pygame pyglet pyqt pysimplewizard python random recursion recursive refresh return reverse scrolledtext session shebang signal simple sprite ssh string strings table terminal text thread threading time tkinter tlapse trick tuple tutorial ubuntu unicode unit urllib urllib2 valueerror variable verify voip windows wordgame wxpython






