THIS IS A JAVA PROBLEM. LAB 1b: Triple Call Counter [ TripleCallCounter.java ] 1
ID: 3887519 • Letter: T
Question
THIS IS A JAVA PROBLEM.
LAB 1b: Triple Call Counter [ TripleCallCounter.java ] 15 points. The following recursive equation: with the following base case conditions: T(O) 0 T(1) = 1 has a programming solution that works very similar to the structure of the solveTowers) algorithm from 1a. It too has three recursive calls, and a base case that performs a single command when it is used. The actual solution to this recursive equation is the found in the following way: T(3) =T(3-1)+T(1) + T(3-1)=T(2) + T(1) + T(2) = 3 + 1 + 3 T(4) =T(4-1)+T(1) + T(4-1)=T(3) + T(1) + T(3) = 7 + 1 + 7 T(5)-T(5-1) + T(1) + T(5- 1)-T(4) + T(1) +T(4) = 15 + 1 + 15Explanation / Answer
Please find my answer.
Please let me know in case of any issue.
public class TripleCallCounter {
static int solveTripleCounter(int n) {
if(n < 2)
return n;
int nMinus1 = solveTripleCounter(n-1); // T(n-1)
return 2*nMinus1 + 1; // T(n-1) + T(n-1) + T(1)
}
public static void main(String[] args) {
System.out.println(solveTripleCounter(4));
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.