Question

Given two integers x and n, where n is non-negative, efficiently calculate the value of power...

Given two integers x and n, where n is non-negative, efficiently calculate the value of power function pow(x, n). Example: pow (-2, 10) = 1024. How many recursive calls are you invoking ?

Homework Answers

Answer #1
Code:
--------
int pow(int x, int n)
{
   if (n == 0) {
      return 1;
   }
   else{
      int half = pow(x, n / 2);
      if (n % 2 == 0) {
         return half*half;
      }
      else {
         return x*half*half;
      }
   }
}



Recursive calls:
------------------
Recursive formula for the above pow() function is
T(n) = T(n/2) + 1
Solving Recursive formula
T(n) = T(n/2) + 1
     = T(n/4) + 1 + 1
     = T(n/8) + 1 + 1 + 1
     ......
     ......
     ......
     = T(n/n) + 1 + .... + 1 + 1 + 1 [log2(n) terms]
     = 1 + 1 + .... + 1 + 1 + 1 [log2(n) terms]
     = log2(n)
So, Number of recursive calls = log2(n)
Know the answer?
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
Similar Questions
Programming Langauge: Java •Given non-negative integers x and n, x taken to the nth power can...
Programming Langauge: Java •Given non-negative integers x and n, x taken to the nth power can be defined as: •x to the 0th power is 1 •x to the nth power can be obtained by multiplying x to the n-1th power with x •Write a long-valued method named power that accepts an two int parameters x and n (in that order) and recursively calculates and returns the value of x taken to the n'th power •For example, to find x4...
We are given n distinct balls and m distinct boxes. m and n are non-negative integers....
We are given n distinct balls and m distinct boxes. m and n are non-negative integers. Every ball must be placed into a box, but not every box must have a ball in it. Each box can hold any number of balls. Let's also assume that the order in which we put the balls into the boxes does matter. (Ex: assume we have 2 balls, a and b, and 3 boxes, 1 2 and 3. two distinct distributions would be...
1. You are given an array of integers, where different integers may have different number of...
1. You are given an array of integers, where different integers may have different number of digits, but the total number of digits over all the integers in the array is n. Show how to sort the array in O(n) time. 2. You are given an array of strings, where different strings may have different numbers of characters, but the total number of characters over all the strings is n. Show how to sort the strings in O(n) time. (Note...
Consider two non-negative numbers x and y such that x+y=10. What is the maximal value of...
Consider two non-negative numbers x and y such that x+y=10. What is the maximal value of the quantity x^2y^2?
write the flow chart and PsuedoCode 1. Make a peanut butter sandwich. 2. Calculate the value...
write the flow chart and PsuedoCode 1. Make a peanut butter sandwich. 2. Calculate the value of n! Do not use the factorial fn, use the actual recursive definition**. 3. Draw a deck of cards from a standard deck of 52 cards until the queen of hearts is drawn. How many cards did you draw? 4. Fibonacci Numbers. The Fibonacci Numbers are an interesting sequence of positive integers where each value is the sum of the previous two values. We...
this assignment is about solving problems using recursion. For each question, write a recursive function that...
this assignment is about solving problems using recursion. For each question, write a recursive function that solves the problem asked in that question. Your solutions don't have to be efficient, but they do have to use recursion in a non-trivial way. Put all your code in a file called a9.py. Write a recursive function is_palindrome(s) that returns True or False depending on whether s is a palindrome. This is equivalent to returning s == s[::-1]. Write a recursive function find_min(a)...
We are given an array A of size n containing n positive and negative integers (the...
We are given an array A of size n containing n positive and negative integers (the array is indexed starting from 0). Our goal is to find two indices i and j such that 0 ≤ i ≤ j ≤ n and Pk=j k=i A[k] is maximized. Input sequence: [2, -4, 1, 9, -6, 7, -3] Output: [1, 9, -6, 7] or i = 2 and j = 5 Input sequence: [1, 2, 3, 4, 5, 6, -3] Output: [1,...
Write a MASM program that computes the sum of the integers from 1 to N where...
Write a MASM program that computes the sum of the integers from 1 to N where N is a positive integer. Use the equal sign directive to define N. Save the sum in the EAX register. You must use loops. For example, 1 = 1 1 + 2 = 3 1 + 2 + 3 = 6 1 + 2 + 3 + 4 = 10 1 + 2 + 3 + 4 + 5 = 15 Language ( Assembly)...
Write a C function hwkOneA, that takes a long int x as well as two integers...
Write a C function hwkOneA, that takes a long int x as well as two integers n and m as arguments, and returns a long int. Here is the function declaration: long int hwkOneA (long int x, int n, int m); The function should swap nibble n and m of a long int x (64-bit integer). A nibble is a four-bit aggregation. For this problem, the index of the most significant nibble is 0, and the index of the least...
Describe an algorithm that, given a set S of n integers and another integer x, determines...
Describe an algorithm that, given a set S of n integers and another integer x, determines whether or not there exist two elements in S whose sum is exactly x. Your algorithm must be nlogn. Evaluate how long each step in the algorithm takes to demonstrate that it meets this requirement.
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT