Hi,
I am trying to figure out how to print tracing information for Fibonacci numbers.
output =
Computing fib(4)
Leaving fib(4) returning 3
Can anyone steer me in the right direction?
This is what I have:
import math
def fib(n):
if n < 3:
return 1
while n >= 3:
x = n-1
y = n-2
print "Computing fib", n
print "fib", x, "+", "fib", y
print "Leaving fib", n
print "Return", fib(x) + fib(y)
return fib(n-1) + fib(n-2)
My output is looking like this:
>>>
>>> fib(4)
Computing fib 4
fib 3 + fib 2
Leaving fib 4
Return Computing fib 3
fib 2 + fib 1
Leaving fib 3
Return 2
3
Computing fib 3
fib 2 + fib 1
Leaving fib 3
Return 2
3
>>>
I cannot get my return of the Fibonacci number to work properly and when I ran this program with fib(10) it ran forever.
:mrgreen: Thanks!