'''
This program runs iterative and recursive versions of fibonacci, to compare
how they perform.
Author: acase@cs.nyu.edu
'''
import random
def fib_iter(n):
'''
Returns the n'th item in the fibonacci series (uses iteration)
''''
# First two cases
n_minus_one = 1
n_minus_two = 1
for i in range(2,n+1):
# Compute the next case
n = n_minus_one + n_minus_two
# Update the previous two terms
n_minus_two = n_minus_one
n_minus_one = n
return n
def fib_rec(n):
'''
Returns the n'th item in the fibonacci series (uses recursion)
''''
# Base cases:
if n == 0: return 1
if n == 1: return 1
# Recursive case:
return fib_rec(n-1) + fib_rec(n-2)
def main():
'''
Main program driver, sets up the GUI and starts drawing
'''
for i in range(40):
print()
print("Running fib_iter(" + str(i) + "):")
print(" Done. Answer:", fib_iter(i))
print("Running fib_rec(" + str(i) + "):")
print(" Done. Answer:", fib_rec(i))
# Run the program
main()