a) In this programming assignment, you will design in pseudo code and implement
ID: 3887514 • Letter: A
Question
a) In this programming assignment, you will design in pseudo code and implement in Java two recursive versions of Tetranacci calculators and experimentally compare their runtime performances. Tetranacci numbers are a more general version of Fibonacci numbers and start with four predetermined terms, each term afterwards being the sum of the preceding four terms. The first few Tetranacci numbers are:
0, 0, 0, 1, 1, 2, 4, 8, 15, 29, 56, 108, 208, 401, 773, 1490, …
For that, with each implemented version you will calculate Tetranacci(5), Tetranacci(10), etc. in increments of 5 up to Tetranacci (100) (or higher value if required for your timing measurement) and measure the corresponding run times (for instance, Tetranacci(10) returns 56). You can use Java’s built-in time function for this purpose. You should redirect the output of each program to an out.txt file. You should write about your observations on timing measurements in a separate text file. You are required to submit the two fully commented Java source files, the compiled executables, and the text files.
b) Briefly explain why the first algorithm is of exponential complexity and the second one is linear (more specifically, how the second algorithm resolves some specific bottleneck(s) of the first algorithm). You can write your answer in a separate file and submit it together with the other submissions.
c) Do any of the previous two algorithms use tail recursion? Why or why not? Explain your answer. If your answer is “No” then:
Can a tail-recursive version of Tetranacci calculator be designed?
i. If yes; write the corresponding pseudo code for that tail recursion algorithm and implement it in Java and repeat the same experiments as in part (a) above.
ii. If no, explain clearly why such tail-recursive algorithm is infeasible.
Explanation / Answer
// I have provided the code for recursive and linear approach of Tetranacci series
// (1)
public class Tetranacci {
// Recursive Approach
public static int tetranacci(int number) {
if ((number == 0) || (number == 1) || (number == 2)) // base cases
return 0;
else if(number == 3){
return 1;
}
// recursion step
return tetranacci(number - 1) + tetranacci(number - 2) + tetranacci(number-3) + tetranacci(number-4);
}
// Linear Approach
public static int linearTetranacci(int number){
int first = 0, second = 0, third = 0, fourth = 1, next = 0;
for(int i=0;i<=number;i++){
if(i<=2){
next = 0;
} else if(i==3){
next = 1;
}
else {
next = first + second + third + fourth;
first = second;
second = third;
third = fourth;
fourth = next;
}
}
return next;
}
public static void main(String[] args) {
System.out.println(tetranacci(10));
System.out.println(linearTetranacci(10));
}
}
/************************************************************************************************************/
(2) the first algorithm will be of exponential complexity. Because we are computing sub problems many times
for example consider
Tetranacci(6) = Tetranacci(5) + Tetranacci(4) + Tetranacci(3)+ Tetranacci(2);
now Tetranacci(5) can be written as Tetranacci(4) + Tetranacci(3)+ Tetranacci(2) + Tetranacci(1);
and Tetranacci(4) can be written as Tetranacci(3)+ Tetranacci(2) + Tetranacci(1) + Tetranacci(0);
So like that if you see we are computing Tetranacci(4), Tetranacci(3),Tetranacci(2) etc multiple times
so as the number n increases theses repeated computation also increases and gives exponential complexity.
Second Solution is obviously a linear solution as it holds a single for loop till number n . and doesn't grow exponentially and doesn't calculate repeatedly.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.