It is pretty easy to compute the Nth Fibonacci number using a recursive function. In fact,...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
It is pretty easy to compute the Nth Fibonacci number using a recursive function. In fact, mathematicians express the formula to do that recursively: Better (O F(n) = 1 , n = 0 , n = 1 (F(n-1) + F(n-2), n >1 this is a recurrence relation that says, for the function: if n = 0 return 0 if n = 1 return 1 else return fib(n-1) + fib(n-2) this looks like great pseudocode for a recursive implementation in python. Implement it as a function in your fib.py program. (You can call that function anything you want - but use that name when you recurse.) Remember to use a == to check for equivalence. A single = is for assignment in python! Trace it to check it by drawing activation records for each function call and deleting them on each function return, just as you saw in class. Test it by calling the function with the variable n you had read in with your earlier code, and printing that result. For example, when n is 4, your program should print 3; when 5 it should print 5. Once your program is working, you should right-size your error checking procedure. If you followed the lab exactly, it won't let you enter a value greater than 5, although your function will surely compute a higher fibonacci number. Experiment with your program to identify the highest number your code can compute without exceeding the maximum recursion depth. (HINT: you'll find that for a large number, your program runs so slowly it doesn't seem like it is doing anything. If you get tired of waiting, you can hit Ctrl-C to stop program execution.) Add a print statement in your recursive function like print ("Computing fibonacci ("+str (n)+") ") at the beginning of your recursive function to output what it is doing as it runs. The problem is that it is doing too much recursion - as written, this function recomputes every smaller number in the sequence every time it computes a new number. Don't change your first recursive function, but instead add a better one. Use the same name as before prefix the function name with fast - if your function was called fibonacci before, call this new function fastfibonacci. Implement it using this code: def fastfibonacci (n, memo-()): if n in memo: return memo [n] if n <= 2: else: memo [n] = fastfibonacci (n - 1, memo) + fastfi return memo [n] return 1 There are a couple of new things happening in this code. First. memo is an optional parameter: Here b is predefined and hence is optional. def func (a, b=1098): return a+b 0 Python3 print (func (2, 2)) #this 1 is represented as 'a' in the function and # function uses the default value of b print(func (1)) To learn more about optional parameters, follow this link. Because the parameter is optional you can omit it and call your new function with only It is pretty easy to compute the Nth Fibonacci number using a recursive function. In fact, mathematicians express the formula to do that recursively: Better (O F(n) = 1 , n = 0 , n = 1 (F(n-1) + F(n-2), n >1 this is a recurrence relation that says, for the function: if n = 0 return 0 if n = 1 return 1 else return fib(n-1) + fib(n-2) this looks like great pseudocode for a recursive implementation in python. Implement it as a function in your fib.py program. (You can call that function anything you want - but use that name when you recurse.) Remember to use a == to check for equivalence. A single = is for assignment in python! Trace it to check it by drawing activation records for each function call and deleting them on each function return, just as you saw in class. Test it by calling the function with the variable n you had read in with your earlier code, and printing that result. For example, when n is 4, your program should print 3; when 5 it should print 5. Once your program is working, you should right-size your error checking procedure. If you followed the lab exactly, it won't let you enter a value greater than 5, although your function will surely compute a higher fibonacci number. Experiment with your program to identify the highest number your code can compute without exceeding the maximum recursion depth. (HINT: you'll find that for a large number, your program runs so slowly it doesn't seem like it is doing anything. If you get tired of waiting, you can hit Ctrl-C to stop program execution.) Add a print statement in your recursive function like print ("Computing fibonacci ("+str (n)+") ") at the beginning of your recursive function to output what it is doing as it runs. The problem is that it is doing too much recursion - as written, this function recomputes every smaller number in the sequence every time it computes a new number. Don't change your first recursive function, but instead add a better one. Use the same name as before prefix the function name with fast - if your function was called fibonacci before, call this new function fastfibonacci. Implement it using this code: def fastfibonacci (n, memo-()): if n in memo: return memo [n] if n <= 2: else: memo [n] = fastfibonacci (n - 1, memo) + fastfi return memo [n] return 1 There are a couple of new things happening in this code. First. memo is an optional parameter: Here b is predefined and hence is optional. def func (a, b=1098): return a+b 0 Python3 print (func (2, 2)) #this 1 is represented as 'a' in the function and # function uses the default value of b print(func (1)) To learn more about optional parameters, follow this link. Because the parameter is optional you can omit it and call your new function with only
Expert Answer:
Answer rating: 100% (QA)
Answer def fibn if n 0 return 0 elif n 1 return 1 else return fib... View the full answer
Related Book For
Posted Date:
Students also viewed these programming questions
-
AH -110.2 kJ Given: 4 NO2(g) + O2(g) 2 N2O5(9) find AH for 3 N205(g) 6 NO2(g) + 1.5 02(g). Select one: O A. 220.4 kJ B. 165.3 kJ C. -165.3 kJ O D. -220.4 kJ
-
During all of 2015, Mr. and Mrs. Clay lived with their four children (all are under the age of 17). They provided over one-half of the support for each child. Mr. and Mrs. Clay file jointly for 2015....
-
Planning is one of the most important management functions in any business. A front office managers first step in planning should involve determine the departments goals. Planning also includes...
-
In the same study cited in Example 1, another trial was conducted with 75 women in China given a 100-Yuan bill, while another 75 women in China were given 100 Yuan in the form of smaller bills (a...
-
(a) If f(x) = x sin x, find f (x). (b) Check to see that your answer to part (a) is reasonable by graphing both f and f for 0 < x < 2.
-
1. Explain how the graph of the sine function can be used to graph y = csc(x) For the following, sketch at least two periods of the graph. Identify the period, phase shift, and describe the location...
-
Construct a bar graph for each of the following (assume the independent variable is group and the dependent variable is time): a. Group \(\mathrm{A}(N=5, M=4.00, s=1.58)\); Group B \((N=5, M=6.00,...
-
On January 1, 2015, Port Company acquires 8,000 shares of Solvo Company by issuing 10,000 of its common stock shares with a par value of $10 per share and a fair value of $70 per share. The price...
-
Essay about Singapore profiles such as GDP per person and GDP per capita growth rate and how to interpret them? and include brief about the Singapore economics.
-
Beth R. Jordan lives at 2322 Skyview Road, Mesa, AZ 85201. She is a tax accountant with Mesa Manufacturing Company, 1203 Western Avenue, Mesa, AZ 85201 (employer identification number 11-1111111)....
-
Department W had 2400 units, 1/3 completed at the beginning of the period. 16,000 units were transferred to Department X from Department W during the period, and 1800 units were one-half completed at...
-
If the federal government wanted to tax a good and suppliers were strong lobbyists, but consumers were not, would government prefer supply or demand to be more inelastic? Why?
-
Demonstrate the welfare loss of: (LO7-2) a. A restriction on output when supply is perfectly elastic. b. A tax t placed on suppliers. c. A subsidy s given to suppliers. d. A restriction on output...
-
In which case would the shortage resulting from a price ceiling be greater: when supply is inelastic or elastic? Explain your answer.
-
State three reasons for a potentially beneficial role of government intervention.
-
God sees all individuals as equal, and that what one does to the least of Gods children, one does to all. How does that approach to thinking about issues fit with the economic analysis that focuses...
-
The number of iterations in apriori ___________ Select one: a. decreases with increase in size of the maximum frequentset b. decreases with the increase in size of the data c. increases with the size...
-
Accounting policies and practices that are most important to the portrayal of the companys financial condition and results, and require managements most difficult, subjective, or complex judgments...
-
Let R = [a, b] Ã [c, d] be a two-dimensional rectangle and f: R R be bounded. a) Prove that for X = or X = L. b) Prove that if f is integrable on R, then c) Compute the two iterated integrals...
-
Suppose that T (R"; R'") for some n, m N. a) Find the matrix representative of T if T(x, y, z, w) = (0, x + y, x - z, x + y + w). b) Find the matrix representative of T if T(x, y, z) = x - y + z....
-
Let F, G: R3 R3 and f : R3 R be differentiable. Prove the following analogues of the Sum and Product Rules for the "derivatives" curl and divergence. a) (F + G) = ( F) + ( G) b) (fF) = f( F)...
-
(a) Assuming that the total number of microstates accessible to a given statistical system is \(\Omega\), show that the entropy of the system, as given by equation (3.3.13), is maximum when all...
-
What trends are occurring in outpatient and long-term care?
-
What are some different types of hospitals, and what trends are occurring in the hospital industry?
Study smarter with the SolutionInn App