Question: Ex. 1. Tracing and understanding recursion Consider the following recursive function, that we can assume to be called with non-negative numbers: int mystery(int n, int

Ex. 1. Tracing and understanding recursion Consider the following recursive function, that we can assume to be called with non-negative numbers: int mystery(int n, int m) { if (n == m) return n; else if (n == 0) return m; else if (m == 0) return n; else return mystery(m, n % m); } a. Tracing the execution Trace the execution ofthe function calls mystery( 363 , 55) , mystery( 126 , 49) , and mystery( 81 , 37) How many calls do each of them make until they return the answer? What is the value of the parameters in each of the calls? Which base case do they reach? b. Solve the mystery What does the function actually do? Can you connect it to a known algorithm for solving this problem? Ex. 2. Recursion Tree Consider the following recursive function, for which again, we can assume the parameters to be non-negative numbers. This function computes the combinations of k out of n objects using Pascal's triangle formula. int combinations(int k, int n) { if (k > n) return 0; else if (n == k || == 0) return 1; else return combinations(kl, nl) + combinations(k, nl); } a. Draw the recursion tree for computing combinations ( 4 , 7 ). b. How many nodes does the runtime stack contain at the most for the function call above, not including the main? c. How many repeated function calls can you obseive in the tree? Give an example. Is this an indication that the function is inefficient
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
