Write an iterative function to compute 2n given n greatertha
Solution
Hi, Please find my answer.
 a)
    // Iterative function to compute 2^n
    int power2(int n){
       int pow = 1;
        for(int i=1;i<=n; i++)
            pow = pow* 2;
       return pow;
    }
b)
    in foo, we are computing :
        int a = foo(n/2)
and returning a*a => foo(n/2)*foo(n/2)
   in bar, we are computing and returning :
        foo(n/2)*foo(n/2)
So, you can see, both function are returning same value
c)
In foo, we are computing : foo(n/2) only one time and multiplying the result of foo(n/2) to get foo(n/2)*foo(n/2)
   But in bar, we are computing foo(n/2) twice.
    Thats why foo is more efficient than bar
   Time complexity of foo: O(logn) => in ecan recursion we are dividing n by n
    Time complexity of bar: O(n) => T(n) = 2T(n/2) + 1
d)
    int foo(int n)
    {
    if( n == 0)
    return 1;
    int a = power(n/2);
    if (n%2 == 0)
    return a*a;
    else
    return 2*a*a;
    }

