Fibonacci Sequence

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Sep 2007
Posts: 6
Reputation: MakingMoney is an unknown quantity at this point 
Solved Threads: 0
MakingMoney MakingMoney is offline Offline
Newbie Poster

Fibonacci Sequence

 
0
  #1
Sep 20th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 608
Reputation: jrcagle is on a distinguished road 
Solved Threads: 150
jrcagle jrcagle is offline Offline
Practically a Master Poster

Re: Fibonacci Sequence

 
1
  #2
Sep 20th, 2007
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:

  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
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 3,584
Reputation: jasimp has a spectacular aura about jasimp has a spectacular aura about jasimp has a spectacular aura about 
Solved Threads: 52
Featured Poster
jasimp's Avatar
jasimp jasimp is offline Offline
Senior Poster

Re: Fibonacci Sequence

 
0
  #3
Sep 20th, 2007
Try looking in the code snippet section of Daniweb to get an idea. There is one in there.
"Argyou not with the hand you are dealt in cards or life." ---- Wizard and Glass
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,065
Reputation: woooee is a jewel in the rough woooee is a jewel in the rough woooee is a jewel in the rough 
Solved Threads: 299
woooee woooee is offline Offline
Veteran Poster

Re: Fibonacci Sequence

 
0
  #4
Sep 20th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,135
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 946
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Fibonacci Sequence

 
0
  #5
Sep 20th, 2007
For Python code on Daniweb:
Please use the [code=python] and [/code] tag pair to enclose your python code.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 4913 | Replies: 4
Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC