Question
3. (In java) Write a RECURSIVE method called sequence that takes a single int parameter (n) and returns the int value of the nth element
3. (In java) Write a RECURSIVE method called sequence that takes a single int parameter (n) and returns the int value of the nth element of the sequence S = 2, 4, 6, 12, 22, 40, 74, 136, 250, 460,
Where S is defined by the recursive formula:
For n >= 0 S(0) = 2; // Base case 1 S(1) = 4; // Base case 2 S(2) = 6; // Base case 3 S(N) = 2 * ( S(N-1)/2 + S(N-2)/2 + S(N-3)/2)
4. Given the sequence, S2 = 1, 2, 4, 5, 7, 8, 10, 11, 13, 14,
( In java) Write a RECURSIVE method called sequence2 that takes a single int parameter (n) and returns the int value of the nth element of the sequence S2. You will need to determine any base cases and a recursive case that describes the listed sequence.
Use the following code to test your answer to questions 3 and 4 the output should print the two sequences given (S & S2):
public class TestSequences {
public static void main(String[] args) {
for(int i = 0; i < 10; i++) {
System.out.print(sequence(i) + " "); // 2, 4, 6, 12, 22, 40, 74, 136, 250, 460
}
System.out.println();
for(int i = 0; i < 10; i++) {
System.out.print(sequence2(i) + " "); // 1, 2, 4, 5, 7, 8, 10, 11, 13, 14
}
}
// *** Your method for sequence here ***
// *** Your method for sequences2 here ***
} // end of TestSequences class
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started