Answer to Question #3371 in Python for mukunda
Write a Fibonacci program in python using recursive function and looping method.
1
2011-08-02T13:58:05-0400
def fibonacci_recursive (n):
if n == 0: return 0
elif n == 1: return 1
return fibonacci_recursive(n-1) + fibonacci_recursive(n-2)
def fibonacci_iterative (n):
if n == 0: return 0
elif n == 1: return 1
prelast, last = 0, 1
for i in range(n-1):
& new = prelast + last
& prelast, last = last, new
return new
print(fibonacci_recursive(7))
print(fibonacci_iterative(7))
Need a fast expert's response?
Submit order
and get a quick answer at the best price
for any assignment or question with DETAILED EXPLANATIONS!
Learn more about our help with Assignments:
Python
Comments
Leave a comment