ALGORITHM RecS(n) // Input: A nonnegative integer n ifn=0 return 0 else return R
ID: 3600172 • Letter: A
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 algorithmExplanation / Answer
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!
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.