Question

ALGORITHM RecS(n) // Input: A nonnegative integer n ifn=0 return 0 else return RecS(n+ n n n Determine what this algorithm computes. You must justify your answer. made by this algorithm and solve it. You must justify your answer. same thing using for/while loop(s) developed in (3). You must justify your answer. 1) 2) Set up the initial condition and recurrence relation for the number of multiplications 3) Write the pseudocode for the non-recursive version of this algorithm, i.e., compute the 4) Find the number of multiplications made by the non-recursive version of this algorithm

0 0
Add a comment Improve this question Transcribed image text
Answer #1

1) The algorithm computes sum of cubes of each number from 1 to n

So RecS(n) = 1^3 + 2^3 + ... + n^3

2)

RecS(n) = 0 => for n=0

RecS(n) = RecS(n-1) + n^3 => for n!= 0

In each iteration, there are 2 multiplications, hence, there will be completely 2n multiplications.

3)

result = 0;

for(int i=1; i<=n; i++) {

result = result + i*i*i;

}

4) The non recursive version, also makes 2 multiplications in each loop iterations, hence for n iterations, there will be 2n multiplications.

Thanks!

Add a comment
Know the answer?
Add Answer to:
ALGORITHM RecS(n) // Input: A nonnegative integer n ifn=0 return 0 else return RecS(n+ n n...
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
  • Consider the following recursive algorithm for computing the sum of the first n cubes: S(n) =...

    Consider the following recursive algorithm for computing the sum of the first n cubes: S(n) = 13 + 23 + … + n3. (a) Set up a recurrence relation for the number of multiplications made by this algorithm. (b) Provide an initial condition for the recurrence relation you develop at the question (a). (c) Solve the recurrence relation of the question (a) and present the time complexity as described at the question number 1. Algorithm S n) Input: A positive...

  • Given algorithm- procedure factorial (n: nonnegative integer) if n = 0 then return 1 else return...

    Given algorithm- procedure factorial (n: nonnegative integer) if n = 0 then return 1 else return n*factorial(n-1) {output is n!} Trace the above algorithm when it is given n = 7 as input. That is, show all steps used by above algorithm to find 7!

  • Consider the problem of computing the power function pow(n,x) = n^x using only multiplications. The first...

    Consider the problem of computing the power function pow(n,x) = n^x using only multiplications. The first approach is to perform x multiplications ($n \cdot n \cdot n \cdot \ldots \cdot n$, x times). Find a better, recursive algorithm to solve this problem (by better, we mean one that uses fewer than $x$ multiplications). Write down the pseudocode for this new function, and then analyze the runtime of that recursive program by first writing out the recurrence relation $T(n, x)$ that...

  • Consider the problem of computing the power function pow(n,x) = n^x using only multiplications. The first approach is to...

    Consider the problem of computing the power function pow(n,x) = n^x using only multiplications. The first approach is to perform x multiplications ($n \cdot n \cdot n \cdot \ldots \cdot n$, x times). Find a better, recursive algorithm to solve this problem (by better, we mean one that uses fewer than $x$ multiplications). Write down the pseudocode for this new function, and then analyze the runtime of that recursive program by first writing out the recurrence relation $T(n, x)$ that...

  • Need an anylasis explaing the code: Let a and n be both nonnegative integers. Consider the...

    Need an anylasis explaing the code: Let a and n be both nonnegative integers. Consider the problem of calculating the nth power of a: an.  Present a brute-force algorithm for the calculation and show the number of multiplications performed overall.  Develop a recursive reduce-by-one algorithm for the calculation, establish and solve the recurrence relation for the number of multiplications performed overall.  Develop a transform-and-conquer algorithm for the calculation. Describe your idea used in the transformation. Evaluate your...

  • 3. 0 marks Suppose you have an algorithm which has the following recurrence relation for W(n),...

    3. 0 marks Suppose you have an algorithm which has the following recurrence relation for W(n), assuming n is a power of 2, i.e. assuming n-2, k20: for n for n- W()-2W(n/2)+2n+ 2 W(n)- Using back substitution and assuming n is a power of 2, ie. n-2, find an exact (ie non-recursive) formula for Win). Be sure to show your work and to simplify your final answer as much as possible. Note that you do NOT need to verify your...

  • The following algorithm (Rosen pg. 363) is a recursive version of linear search, which has access...

    The following algorithm (Rosen pg. 363) is a recursive version of linear search, which has access to a global list of distinct integers a_1, a_2,..., a_n. procedure search(i, j, x : i,j, x integers, 1 < i < j < n) if a_i = x then return i else if i = j then 4. return 0 else return search(i + 1, j, x) Prove that this algorithm correctly solves the searching problem when called with parameters i = 1...

  • Algorithm Riddle(A[0..n-1]) //Input: An array A[0..n-1] of real numbers if n=1 return A[0] else temp =...

    Algorithm Riddle(A[0..n-1]) //Input: An array A[0..n-1] of real numbers if n=1 return A[0] else temp = Riddle(A[0..n-2]) if temp<=A[n-1] return temp else return A[n-1] what does it compute? explain please

  • 3) [16 points totall Consider the following algorithm: int SillyCalc (int n) { int i; int Num, answer; if (n &lt...

    3) [16 points totall Consider the following algorithm: int SillyCalc (int n) { int i; int Num, answer; if (n < 4) 10; return n+ else f SillyCalc(Ln/4) answer Num Num 10 for (i-2; i<=n-1; i++) Num + = answer + answer; answer return answer } Do a worst case analysis of this algorithm, counting additions only (but not loop counter additions) as the basic operation counted, and assuming that n is a power of 2, i.e. that n- 2*...

  • Q4) [5 points] Consider the following two algorithms: ALGORITHM 1 Bin Rec(n) //Input: A positive decimal...

    Q4) [5 points] Consider the following two algorithms: ALGORITHM 1 Bin Rec(n) //Input: A positive decimal integer n llOutput: The number of binary digits in "'s binary representation if n1 return 1 else return BinRec(ln/2)) +1 ALGORITHM 2 Binary(n) tive decimal integer nt io 's binary representation //Output: The number of binary digits in i's binary representation count ←1 while n >1 do count ← count + 1 return count a. Analyze the two algorithms and find the efficiency for...

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
ADVERTISEMENT