Introduction to Programming using Python 1st Edition

Published by Pearson
ISBN 10: 0132747189
ISBN 13: 978-0-13274-718-9

Chapter 15 - Recursion - Programming Exercises - Page 523: 15.7

Answer

code

Work Step by Step

# 15.7 (Fibonacci series) Modify Listing 15.2 so that the program finds the number of # times the fib function is called. (Hint: Use a global variable and increment it # every time the function is called.) counter = 0 def fib(index): global counter counter += 1 if index == 0: # Base case return 0 elif index == 1: # Base case return 1 else: # Reduction and recursive calls return fib(index - 1) + fib(index - 2) index = eval(input("Enter an index for a Fibonacci number: ")) fib(index) print("The Fibonacci function is called", counter, "times")
Update this answer!

You can help us out by revising, improving and updating this answer.

Update this answer

After you claim an answer you’ll have 24 hours to send in a draft. An editor will review the submission and either publish your submission or provide feedback.