Question

What is the recurrence of the power2() function? What is its running time? Is it faster...

What is the recurrence of the power2() function? What is its running time? Is it faster than the iterative way?

int power(int base, int n)
{
        int result = 1;
        for (int i = 1; i <= n; i++)
        {
                result *= base;
        }
        return result;
}

int power2(int base, int n)
{
        int result;

        if (n == 0)
                return 1;

        if (n % 2 == 0) // 3^10
        {
                result = power2(base, n / 2); // 3^5
                return result * result; // 3^5 * 3^5 
        }
        else // 3^11
        {
                result = power2(base, n / 2); // 3^5
                return result * result * base; // 3^5 * 3^5 * 3
        }
}

0 0
Add a comment Improve this question Transcribed image text
Know the answer?
Add Answer to:
What is the recurrence of the power2() function? What is its running time? Is it faster...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a recurrence relation describing the worst case running time of each of the following algorithms,...

    Write a recurrence relation describing the worst case running time of each of the following algorithms, and determine the asymptotic complexity of the function defined by the recurrence relation. Justify your solution by using substitution or a recursion tree. You may NOT use the Master Theorem. Simplify your answers, expressing them in a form such as O(nk) or (nklog n) whenever possible. If the algorithm takes exponential time, then just give an exponential lower bound using the 2 notation. function...

  • Write a recurrence relation describing the worst-case running time of each of the following algor...

    Write a recurrence relation describing the worst-case running time of each of the following algorithms and determine the asymptotic complexity of the function defined by the recurrence relation. Justify your solution by using substitution or a recursion tree. You may NOT use the Master Theorem. 上午1:46 3月21日周四 令52%. " 5. endfor 6. return (r); function func4(A, n) *Aarray of n integers */ 1. if n s 20 then return (A[n]); 4. while (i < n/2) do 7. endwhile 8. x...

  • For each of the following problems write a recurrence relation describing the running time of eac...

    For each of the following problems write a recurrence relation describing the running time of each of the following algorithms and determine the asymptotic complexity of the function defined by the recurrence relation. Justify your solution using substitution and carefully computing lower and upper bounds for the sums. Simplify and express your answer as Θ(n k ) or Θ(n k (log n)) wherever possible. If the algorithm takes exponential time, then just give exponential lower bounds. 5. func5 (A,n) /*...

  • Given these methods: METHOD math1: public int math1( int n ) { if (n <= 1) { return 1; } // if...

    Given these methods: METHOD math1: public int math1( int n ) { if (n <= 1) { return 1; } // if else { return ( n * 2 ) + math1( n-1 ); } // else } // math1 METHOD math2: public int math2( int n ) { if (n <= 1) { return 1; } // if else { return n + math1( n ) * math2( n/2 ); } // else } // math2 (a) Set up...

  • PLEASE write in C Language, ( you can use array, recursive function, Pointers, Dynamic call by re...

    PLEASE write in C Language, ( you can use array, recursive function, Pointers, Dynamic call by refernce, Structure) a) 70 marks] Write a program that Sorts a given integer array via Selection Sort obtained by rotation around 3. Searches for a key point in the rotated array in O(logn) time, where the rotation Rotates the sorted array around a random point, e.g., 1 2 345->34512 is point is known in advance. ts above by first finding the unknown rotation point...

  • In Java Language Write a recurrence equation expressing the time complexity of the following algorithm. Explain...

    In Java Language Write a recurrence equation expressing the time complexity of the following algorithm. Explain your answer. Assume that n is a power of 2. Algorithm rec(n) Input: Integer value n ≥ 0         if n = 0 then return 1         else {                       c ← 0                        For i ← 0 to n−1 do c ← c + i                        c ← c + rec(n/2)                        return c                 }       

  • Compute the recurrence relation, T(n), for the following function, solve it, and give a e bound....

    Compute the recurrence relation, T(n), for the following function, solve it, and give a e bound. Justify your answer public static double myPower(double r, int n) if (n1){ return 1 } else if (n % 2 == 0) { double tmp myPower (r, n/2); return tmp tmp; } else{ myPower (r, (n 1)/2); return }

  • Consider the merge sort algorithm. (a) Write a recurrence relation for running time function for the...

    Consider the merge sort algorithm. (a) Write a recurrence relation for running time function for the merge sort. (b) Use two methods to solve the recurrence relation. (c) What is the best, worst and average running time of the merge sort algorithm? Justify your answer.

  • (5 pts) What is the ouput of the following function F, for the call int i...

    (5 pts) What is the ouput of the following function F, for the call int i = F(3)? int F(int n) { int result; if (n > 20) return 1; else { result = F(2*n) * 2; cout << result << " "; return result; } }

  • For each of the following six program fragments: a. Give an analysis of the running time...

    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;...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT