F(5) = 5 * F(3) = 5 * 3 * F(1) = 5 * 3 * 2 = 5 * 6 = 30 so, F(5) is 30 runtime complexity is O(n)
T2(N) = Θ(N') Which algorithm is the least preferred one? a) Algorithm 1 b) Algorithm 2...
3. Recursive Program (6 points) Consider the following recursive function for n 1: Algorithm 1 int recurseFunc(int n) If n 0, return 1. If n 1, return 1 while i< n do while j <n do print("hi") j 1 end while i i 1 end while int a recurse Func(n/9); int b recurse Func (n/9) int c recurse Func (n/9) return a b c (1) Set up a runtime recurrence for the runtime T n) of this algorithm. (2) Solve...
Given algorithm- procedure factorial (n: nonnegative integer) if n = 0 then return 1 else return n*factorial(n-1) {output is n!} Trace the above algorithm when it is given n = 7 as input. That is, show all steps used by above algorithm to find 7!
1(5 pts): For each code fragment below, give the complexity of the algorithm (O or Θ). Give the tightest possible upper bound as the input size variable increases. The input size variable in these questions is exclusively n. Complexity Code public static int recursiveFunction (int n)f f( n <= 0 ) return 0; return recursiveFunction (n - 1) 1; for(int i 0i <n; i+) j=0; for ( int j k=0; i; k < < j++) for (int j; m <...
How to prove G(n)=n+1 in this algorithm? 1. if (n 0) 2. return 1 3. else if (n1) f 4. return 2 5. else if (n 2) 6. return 3 7. else if (n3) t 8. return 4 else f 9. int OGnew int[n 11 10. G[O]1 12. G[2]3 13. G[3]4 14. int i:-4 15. while (i<n) t 16. if (i mod 20) else ( 20. return G[n] 1. if (n 0) 2. return 1 3. else if (n1) f...
3) [16 points totall Consider the following algorithm: int SillyCalc (int n) { int i; int Num, answer; if (n < 4) 10; return n+ else f SillyCalc(Ln/4) answer Num Num 10 for (i-2; i<=n-1; i++) Num + = answer + answer; answer return answer } Do a worst case analysis of this algorithm, counting additions only (but not loop counter additions) as the basic operation counted, and assuming that n is a power of 2, i.e. that n- 2*...
Consider the following algorithm BAR that gets a positive integer and works as follows: BAR(int n): - if (n <=0 ), return 0; - else, return (n - BAR(BAR(n-1))); (A.) What will be the output of BAR(3)? (B.) Write an algorithm that has the same functionality as BAR, so that the runtime of BAR(n) is O(n)?
please also explain how the answer came about if possible b) Base 7. Master Theorem (3 points) Consider the following recursive function for n >0 case: a| c) Recursive case: an Algorithm 1 int recFunc(int n) //Base Case if n <= 2 then return n; end if / /Recursive Case: while i< n do print("Hello!") end while int a 2*recFunc(n/2); return a; Find the runtime of the above recursive function using the master theorem
b) Consider the following code. public static int f(int n) if (n == 1) return 0; else if (n % 2 == 0). return g(n/2); else return g(n+1); public static int g(int n) int r = n % 3; if (r == 0) return f(n/3); else if (r == 1) return f(n+2); else return f(2 * n); // (HERE) public static void main(String[] args) { int x = 3; System.out.println(f(x)); (1) (5 points) Draw the call stack as it would...
Please give me a divide and conquer algorithm that has runtime better than O(n^2) along with justification. Also please do a runtime analysis on this algorithm. Please DONT copy and paste other's solution.THANKS 3. Give the best algorithm you can to convert an n digit number base 10 into binary. Here, we are counting operations on single digits as single steps, not arithmetic operations. You can use any of the multiplication algorithms we described in class.)
What is the runtime of each method? Give answer in Θ(big Theta) notation as a function of n, give a brief explanation. A. public static int method1(int n){ int mid = n/2; for (int i = mid; i >= 0; i--) System.out.println(i); for (int i = mid + 1; i <= n; i++) System.out.println(i); return mid; } B. public static int method2(int n){ for (int i = n; i >= 0; i / 3){ System.out.println(i ); } return mid; }...