Question: Big Oh for a recursive function. The following program computes 2 n : int power2(int n) { if (n==0) return 1; return power2(n-1)+power2(n-1); } Find

Big Oh for a recursive function. The following program computes 2n:

int power2(int n) { if (n==0) return 1; return power2(n-1)+power2(n-1); }
  1. Find a recurrence formula as we learned in class, i.e., T(n) in terms of T(n-1). Find the runtime. What is the big problem with this function, something you can see by just looking at this code? (hint: We discussed something similar in class).

  2. Introduce a small modification that makes the function run in linear time. Show why the runtime is linear.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!