Question

In: Computer Science

Design in java a multiple recursive version of Tetranacci calculators. You will calculate Tetranacci(5), Tetranacci(10) and...

Design in java a multiple recursive version of Tetranacci calculators. You will calculate Tetranacci(5), Tetranacci(10) and measure the corresponding run times. (for instance, Tetranacci(10) returns 56). You can use Java’s built-in time function for finding the execution time.

Solutions

Expert Solution

Write following program in file Main.java

/*
multiple recursive version of Tetranacci calculators
The tetranacci numbers are a generalization of the Fibonacci numbers defined by the recurrence relation
T(n) = T(n-1) + T(n-2) + T(n-3) + T(n-4)

First few numbers are The first few Tetranacci numbers are:
0, 0, 0, 1, 1, 2, 4, 8, 15, 29, 56, 108, 208, 401, 773, 1490, …
So the base values of first 4 terms are T(1) = T(1) = T(2) = 0, T(3) = 2

*/

//Declare class
class TetranciiRecursive {
// find the nth term using recursive approach
public static int tetranacci(int n) {
// base cases
if (n == 0 || n == 1 || n == 2)
return 0;
// base case
if (n == 3)
return 1;
else
return tetranacci(n - 1) +
tetranacci(n - 2) +
tetranacci(n - 3) +
tetranacci(n - 4);
}
}

public class Main {
public static void main(String[] args) {
long startTime, endTime, totalTime;
//start the time
startTime = System.nanoTime();
System.out.println("Tetranacci(5) = " + TetranciiRecursive.tetranacci(5));
//end time
endTime = System.nanoTime();
//total time
totalTime = endTime - startTime;
//display total execution time of the recursive function
System.out.println("Execution time: " + (totalTime / 10000) + " seconds");
//start the time
startTime = System.nanoTime();
System.out.println("Tetranacci(10) = " + TetranciiRecursive.tetranacci(10));
//end time
endTime = System.nanoTime();
totalTime = endTime - startTime;
//display the time
System.out.println("Execution time: " + (totalTime / 10000) + " seconds");

}
}

Compile the java file and run it. to get the output as below


Tetranacci(5) = 2
Execution time: 4634 seconds   
Tetranacci(10) = 56
Execution time: 53 seconds


Related Solutions

Design in pseudo code a multiple recursive version of Tetranacci calculators. Tetranacci numbers are a more...
Design in pseudo code a multiple recursive version of Tetranacci calculators. 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, …
Design in pseudo code a linear recursive version of Tetranacci calculators. Tetranacci numbers are a more...
Design in pseudo code a linear recursive version of Tetranacci calculators. 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, …
Java String search Design and implement a recursive version of a binary search.  Instead of using a...
Java String search Design and implement a recursive version of a binary search.  Instead of using a loop to repeatedly check for the target value, use calls to a recursive method to check one value at a time.  If the value is not the target, refine the search space and call the method again.  The name to search for is entered by the user, as is the indexes that define the range of viable candidates can be entered by the user (that are...
design two algorithms, an iterative version and a recursive version , for finding the min and...
design two algorithms, an iterative version and a recursive version , for finding the min and max values in an array of n numbers A.) a iterative version B.) a recursive version derive the time efficiency functions (or time function in terms of n) for each, analyze each function, and compare and discuss the results of the analysis
Complete java program below. Complete non-recursive version nthFibonacciWithLoop() method. Complete recursive version nthFibonacciWithRecursion() method. public class...
Complete java program below. Complete non-recursive version nthFibonacciWithLoop() method. Complete recursive version nthFibonacciWithRecursion() method. public class Fibonacci { // Fib(N): N N = 0 or N = 1 // Fib(N-1) + Fib(N-2) N > 1 // For example, // Fib(0) = 0 // Fib(1) = 1 // Fib(2) = Fib(1) + Fib(0) = 1 + 0 = 1 // Fib(3) = Fib(2) + Fib(1) = Fib(2) + 1 = (Fib(1) + Fib(0)) + 1 = 1 + 0 + 1...
In java, Write a recursive function to calculate the sum of the nodes only on even...
In java, Write a recursive function to calculate the sum of the nodes only on even levels of the subtree. please do not add any parameters to do this function. private int sumEvenLevels(Node current){ //you can only pass in root. //code }
Learning objectives; File I/O practice, exceptions, binary search, recursion. Design and implement a recursive version of...
Learning objectives; File I/O practice, exceptions, binary search, recursion. Design and implement a recursive version of a binary search. Instead of using a loop to repeatedly check for the target value, use calls to a recursive method to check one value at a time. If the value is not the target, refine the search space and call the method again. The name to search for is entered by the user, as is the indexes that define the range of viable...
Parse string java code Write a recursive program that can calculate the value of a given...
Parse string java code Write a recursive program that can calculate the value of a given polynomial in a string, which is not more than the tenth order, for the given x. The polynomial will be given in the following format and should display the value of the polynomial for spaced-out x Using index of for -/+
5.) A sample of 5 different calculators is randomly selected from a group containing 10 that...
5.) A sample of 5 different calculators is randomly selected from a group containing 10 that are defective and 8 that have no defects. Assume that the sample is taken with replacement. What is the probability that at least one of the calculators is defective? Express your answer as a percentage rounded to the nearest hundredth. 6)At Sally's Hair Salon there are three hair stylists. 32% of the hair cuts are done by Chris, 35% are done by Karine, and...
you will create a program with Java to implement a simplified version of RSA cryptosystems. To...
you will create a program with Java to implement a simplified version of RSA cryptosystems. To complete this project, you may follow the steps listed below (demonstrated in Java code) to guide yourself through the difficulties. Step I Key-gen: distinguish a prime number (20 pts) The generation of RSA's public/private keys depends on finding two large prime numbers, thus our program should be able to tell if a given number is a prime number or not. For simplicity, we define...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT