I cannot get my return of the Fibonacci number to work properly and when I ran this program with fib(10) it ran forever.
'Ran forever' because you are calculating using the same number over and over. Look at your output. If you want to compute a fib you first have to start with two numbers. The sequence 1, 1, 2, 3, 5 actually starts with zero and one. You can start with any two numbers you want, or with one number and number-1, so if we start with 10 and 10-1=9, store those two numbers in a list, pass the list to the function which adds them together, then element[0] = element[1], and element[1] = the sum, and continue by returning the updated list. If you still can't figure it out, post back. When testing a loop, it is a good idea to put in a counter and if it is greater than some number, say 1000, exit with an error message.
Edit: I'm now wondering if you want to calculate the fibs used to calculate a number. I would think that mpossible as it can be any two numbers in combination. For example 10=9+1 or 8+2, etc. If you can explain what you want to do in words or have some math, please post back.
woooee
Nearly a Posting Maven
2,454 posts since Dec 2006
Reputation Points: 777
Solved Threads: 714
Recursion usually means something that calls itself. The following is a link to a sort that uses recursion. Note that in the sort there is a way to tell if the item has already been tested/done. Recursion is used for things like sorts and walking a directory to get files from subdirectories.
http://www.faqts.com/knowledge_base/view.phtml/aid/4491
##Recursion to compute a factorial
def factorial(n) :
if n < 2 :
return 1
else :
return ( n * factorial(n-1) )
woooee
Nearly a Posting Maven
2,454 posts since Dec 2006
Reputation Points: 777
Solved Threads: 714
A function calling itself, or recursion, is like a loop. As with any loop you have to give it an exit condition, or it will keep going. So, watch out that your test print statements don't ruin your exit condition or call the function itself.
Just out of curiosity I put in the only test print statement I could think of into the standard recursive Fibonacci Number function. Since the function calls itself twice each time around, it makes for a surprising number of time expensive function calls, and is as inefficient as the government!
[php]def fib(n):
print "Doing fib(%d)" % n # test!!!
if n < 2:
return n
else:
return fib(n - 1) + fib(n - 2)
print fib(10)
[/php]For fib(30) the recursive function is 75,000 times slower than the usual nonrecursive function ...
[php]def fib(n):
(current, previous) = (0, 1)
while n > 0:
(current, previous) = (previous, current + previous)
n -= 1
return current
[/php]
vegaseat
DaniWeb's Hypocrite
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
Let's take fib(7)
fib(7) = 13 = 8 + 5 (curr/ prev)
fib(6) = 8 = 5 + 3 (curr/ prev)
fib(5) = 5 = 3 + 2 (curr/ prev )
I am realizing I need to store some things here but I'm not sure what or how, right now.
1 1 2 3 5 8 13
Perhaps append a fib sequence to a list until the value is greater than the input number? That should give you the info. If you want to use recursion, you can use it to calculate the fib list. Personally, I would use a while loop, but whatever.
woooee
Nearly a Posting Maven
2,454 posts since Dec 2006
Reputation Points: 777
Solved Threads: 714
The outside functions on the far left hand side = n-1 each time around the loop
The inside function on the far L.H. side = n-2 each time around loop
If n=list element >= input number, then n-1 and n-2 = element numbers that make up the fib, or am I also confused?
woooee
Nearly a Posting Maven
2,454 posts since Dec 2006
Reputation Points: 777
Solved Threads: 714