Question

In: Computer Science

To get some practice with recursion. You can do all this in one driver program. Write...

To get some practice with recursion.

You can do all this in one driver program.

Write a recursive method to compute the result of the Fibonacci sequence:

Fibonacci(N) = Fibonacci(N -1) + Fibonacci(N-2)

N == 0 is 0

N == 1 is 1

Testing: Display the result for Fibonacci(N) and the number of function calls for N = 2, 5 and 10.

Solutions

Expert Solution

#Code executed in Python 3

#Enter the input variable
N=int(input('Enter the number:'))
#Function to compute Fibonacci sequence:0,1,1,2,3,5,8,13,21,34,55,89,...
def fib(N):
#Base case Fibonacci(0) = 0
if N==0:
return 0
#Base case Fibonacci(1) = 1
elif N==1:
return 1
#Recursively finding the next value
elif N > 1:
return fib(N-1)+fib(N-2)
#In case N is less than 0
else:
print('Invalid Input')
  
#Printing the result of the sequence
print('Result of Fibonacci sequence:',fib(N))
#General term for computing number of function calls
Number_of_calls=2*fib(N)-1
#Printing the number of function calls
print('Number of function calls:',Number_of_calls)

Code snapshot:-

Output snapshot:-


Related Solutions

Recursion practice Write a Java program with functions for each of the following problems. Each problem...
Recursion practice Write a Java program with functions for each of the following problems. Each problem should be solved by writing a recursive function. Your final program should not have any loops in it. All of your solutions should be in a single .java file. The main function of the file should demonstrate each of your solutions, by running several tests and producing the corresponding outputs. Write a recursive method to 1. calculate power of a give number 2. multiply...
THIS IS ALL ON PYTHON Recursion Practice Recall from lecture that a recursive function is one...
THIS IS ALL ON PYTHON Recursion Practice Recall from lecture that a recursive function is one that calls on itself. Recursive functions have two parts, the base case which stops the recursion and the recursive step where the function calls upon itself. When designing recursive algorithms, your goal for each step should be to work towards the base case. More abstractly, each recursive call decreases the size of the problem, working ever closer to some trivial case with a defined...
Write a program in python that implements quicksort, first using recursion and then without recursion.
Write a program in python that implements quicksort, first using recursion and then without recursion.
In C++, Do some research to find the definitions of single recursion, binary recursion, and multiple...
In C++, Do some research to find the definitions of single recursion, binary recursion, and multiple recursion. What kinds of problems lend themselves to these different forms of recursion? Please be as detailed as possible. Thank you!
An excellent example of a program that can be greatly simplified by the use of recursion...
An excellent example of a program that can be greatly simplified by the use of recursion is the Chapter 4 case study, escaping a maze. As already explained, in each maze cell the mouse stores on the maze stack up to four cells neighboring the cell in which it is currently located. The cells put on the stack are the ones that should be investigated after reaching a dead end. It does the same for each visited cell. Write a...
***For some practice on this assignment, you could visit your textbook’s online site and do one...
***For some practice on this assignment, you could visit your textbook’s online site and do one of the Chapter 13 exercises (this is from an earlier edition of the textbook, so the Chapter numbers do not align)! Understanding pricing theory is all fine and good. But you also have to be able to "crank the numbers". IMPORTANT NOTE: for the following case, assume that any change in costs, once made, continues from that point and then throughout the rest of...
java 2D array / recursion explain every method You are requested to write a Java program...
java 2D array / recursion explain every method You are requested to write a Java program of a simple Memory Management Unit. The program should allow the following: 1. The user can create a process asking for memory. The program will return a process ID if the requested memory can be allocated. It will also print the allocated Base and Limit. 2. The user can delete a process by specifying a process ID. The program should do that and free...
some restaurants offer “all you can eat” meals. how is this practice related to diminishing marginal...
some restaurants offer “all you can eat” meals. how is this practice related to diminishing marginal utility? what restrictions must the restaurant impose on the customer in order to make a profit?
How can you disseminate scholarship in your practice (not money you get for school)?
How can you disseminate scholarship in your practice (not money you get for school)?
Write a program in Python to practice evaluating some logical expressions and determine whether the expressions...
Write a program in Python to practice evaluating some logical expressions and determine whether the expressions are equivalent. Read in a 0 or 1 integer value for each of the following variables: A, B, C, and D. Use an if…else statement to change the 0 and 1 integer values into True and False Boolean values. Booleans in Python have to have this capitalization (True/False, not true/false or TRUE/FALSE) and shouldn’t contain double quotes (which makes them a String instead of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT