mystery(n):
if n == 1 then
println("A");
else
mystery(n/2);
println("B");
mystery(n/2);
end if
ans)
base case:
if n == 1 then
println("A");
recursive case :
else
mystery(n/2);
println("B");
mystery(n/2);
computing time complexity:
Recurrence relation of the above recursive function :
T(n) = 2T(n/2) + 1, T(1) = 1
T(n) = 2T(n/2) + 1 ---- 1
T(n/2) = 2T(n/2^2) + 1 ---- 2
substitue 2 in 1
T(n) = 2^2T(n/2^2) + 2^1 + 2^0 ---- 3
T(n/2^2) = 2 T(n/2^3) + 1 ----- 4
substitue 4 in 3
T(n) = 2^3 T(n/2^3) + 2^2 + 2^1 + 2^0
.
.
.
.
T(n) = 2^k T(n/2^k) + (2^0 + 2^1 + 2^2 + .... + 2^(k-1))
assume 2^k = n ==> k = log n
So T(n) = 2^k T(1) + (2^0 + 2^1 + 2^2 + .... + 2^(k-1))
= 2^k + (2^0 + 2^1 + 2^2 + .... + 2^(k-1))
= n + (2^k - 1)
= n + n - 1
= 2n
T(n)= O(n)
For the following code, figure out the recurrences (base case and recursive step), calculate the running...
CHALLENGE ACTIVITY 6.4.2: Recursive function: Writing the recursive case. Write code to complete factorial_str()'s recursive case. Sample output with input: 5 5! = 5 * 4 * 3 * 2 * 1 = 120 1 test passed 4 6 All tests 1 passed 8 9 1 def factorial_str(fact_counter, fact_value): 2 output_string = 3 if fact_counter == 0: # Base case: 0! = 1 5 output_string += '1' elif fact counter == 1: # Base case: print 1 and result 7...
In a recursive solution, the base case terminates the recursive processing. In the below code, which of the following calls to recurse() would stop further recursive calls? #include <iostream> void recurse(int x, int y) { if (y > 0) { x++; y--; std::cout << x << " " << y << " "; recurse(x, y); std::cout << x << " " << y << std::endl; } } 1recurse(1, 2); 2recurse(1, 0); 3recurse(1, 1); 4recurse(0, 1);
This code is based on creating a recursive funciton. You cannot alter the code besides adding your solution in the particular area. Write code to complete PrintFactorial()'s recursive case. Sample output if userVal is 5: 5! = 5 * 4 * 3 * 2 * 1 = 120 #include void PrintFactorial(int factCounter, int factValue){ int nextCounter = 0; int nextValue = 0; if (factCounter == 0) { // Base case: 0! = 1 printf("1\n"); }...
The code for a recursive function 'mystery' appears below. Assume we passed the following array as x[]: int x[] = { 10, 20, 25, 25 }; How would we call mystery() so the return value is 1? int mystery(const int x[], int n, int mysteryValue) { int count = 0; if (n <= 0) return 0; else { if (x[n - 1] == mysteryValue) count = 1; return mystery(x, n - 1, mysteryValue) + count; } } 1mystery(x, 4, 0)...
Please code this in Java, thank you! (a) Implement a sublinear running time complexity recursive function in Java public static long long x, int n) to calculate X Note: In your function you can use only the basic arithmetic operators (+, -,, %, and /) (b) What is the running time complexity of your function? Justify (c) Give a number of multiplications used by your function to calculate x63.
1 2 3 The base case; reduce problem and general solution of the recursive algorithm of n! are: O A. O! =0 (n-1)! n(n-1)! OB.O!= 1 (n-1)! n(n-1)! OC. O!=0 (n-1)! n(n-1) OD.0! = 1 (n-1) n(n-1) To set up the table for a party, if we use n tables and set up one next another as the picture. How many seats are there when if one side of the table can place only one chair? When determine Four-Step Methodology...
Consider the following pseudo-code fragment. What is the value of mystery(2, 3)? BEGIN PROGRAM mystery(int a, int b) IF (b==1) RETURN a END IF ELSE RETURN a + mystery(a, b - 1) END ELSE END PROGRAM mystery 2 4 6 8 the program generates a run time error (infinite recursion) I have no idea
2. Consider the function george (int n) computed by the following recursive C++ code. int george (int n) assert (n >= 0) if (n < 2) return 1; else return 2*george ((n+1)/2)+2*george (n/2)+2 george((n-1)/2)+2*george (n/2-1); (c) Design a memoization algorithm to compute george(n) for any given n. You do not have to write C++ code. This algorithm should be much faster than the dynamic programming algorithm. What is its time complexity? 2. Consider the function george (int n) computed by...
For each of the following six program fragments: a. Give an analysis of the running time (Big-Oh will do). b. Implement the code in the language of your choice, and give the running time for several values of N. Pseudo Code Implementation Analysis of runtime time (Big-Oh) (1) sum = 0; for(i = 0; i < n; ++i) ++sum; (2) sum = 0; for(i = 0; i < n; ++i) for(j = 0; j<n; ++i) ++sum; (3) sum = 0;...
Calculate the running time for the following code fragment. Show your work. for (i:= 0; i < n; i++) { for (j:=1; j < n; j*=2) { counter := counter +1 } }