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!
ALGORITHM RecS(n) // Input: A nonnegative integer n ifn=0 return 0 else return RecS(n+ n 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 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 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 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...
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...
Pseudocode Algorithm Sub(n) if n = 1 or n = 2 return n - 1 else return Sub(n - 1) + Sub(n - 1) Let Sub(n) be the number of "substraction operation" as the basic operation on input of size n. what is the recurrence for Sub(n). please show working.
Recursive definition for factorial: a0 = 1, an = n * an-1 procedure factorial(n: nonnegative integer) if n = 0 then return 1 else return n * factorial( n - 1 ) Trace the execution of the factorial algorithm described above for input 7. Track the number of times factorial is invoked (with the first invocation with input 7 as invocation 0) and the value returned by each invocation.
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 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 = Riddle(A[0..n-2]) if temp<=A[n-1] return temp else return A[n-1] what does it compute? explain please