Answered step by step
Verified Expert Solution
Question
1 Approved Answer
The following program computes 2n : int power2(int n) { if (n==0) return 1; return power2 (n1)+ power 2(n1); } (a) Find a recurrence formula
The following program computes 2n : int power2(int n) \{ if (n==0) return 1; return power2 (n1)+ power 2(n1); \} (a) Find a recurrence formula as we learned in class. Find the runtime. What is the big problem with this function? (hint: We discussed something similar in class). (b) Introduce a small modification that makes the function run in linear time. Show why the runtime is linear. (c) (bonus) The following function also calculates 2n : int power2New(int n) \{ if (n==0) return 1; if (n%2==0){ int result = power2New (n/2); return result*result; \} else return 2*power 2 New (n1); \} Explanation: If n is even, then 2n=(22n)2, so we can calculate 22n recursively and square, cutting half of n in one move. Otherwise, we resort to the previous method. Show that the runtime of power2New is logarithmic in n. Hint: It's easy to show it when n is even, but sometimes n is odd... The trick is to show that the entire function is logarithmic nonetheless
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started