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...
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.
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...
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...
***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...
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...
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)?
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?
Write the class RecursiveProbs, with the methods listed below. Write all the methods using recursion, not...
Write the class RecursiveProbs, with the methods listed below. Write all the methods using recursion, not loops. You may use JDK String methods like substring() and length(), but do not use the JDK methods to avoid coding the algorithms assigned. For example, don't use String.reverse(). public boolean recursiveContains(char c, String s) returns true if the String contains the char, otherwise returns false. Here is the code for this method, which you should use as an example to see how to...
Write a driver to get a String input from keyboard and if the input string has...
Write a driver to get a String input from keyboard and if the input string has less than 10 characters, throw a StringTooShortException. public class StringTooShortException extends Exception {     //-----------------------------------------------------------------     // Sets up the exception object with a particular message.     //-----------------------------------------------------------------     public StringTooShortException()     {         super("String does not have enough characters");     } }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT