Generalized Tribonacci Sequence Description The tribonacci sequence is a general
ID: 3689487 • Letter: G
Question
Generalized Tribonacci Sequence
Description
The tribonacci sequence is a generalization of the fibonacci sequence, where the nthnth term in the sequence is given by the following recurrence relation:
T(n)=T(n1)+T(n2)+T(n3)
Specification
Create a class GeneralTribonacci. At a minimum, your class should have the following methods:
genTrib that takes four numbers: the first three numbers in the sequence, as well as an index nn. This should return the nthnth term in the given sequence.
If the first three numbers are given in non-ascending order, or if the index given is less than 0, this should throw a (runtime) SequenceStartException
NOTE The indexing should start from 0, so the first term in the sequence is the 0th term
A main method, that asks the user to input the beginning of the tribonacci sequence as well as an index n, then prints the nth term in the sequence.
Example Dialog
Note
You should experiment with the running time of your algorithm, in terms of the index.
E.g. how fast can it find the 10th term? How about the 40th?
Experiment further to see if you can find a faster algorithm with the same result.
Finally
Submit both GeneralTribonacci and SequenceStartException to the dropbox
Explanation / Answer
import java.util.Scanner;
public class Tribonacci
{
public int trib(int n)
{
if(n==1)
return 0;
else if(n==2)
return 1;
else if(n==3)
return 1;
else if(n>3)
return (trib(n-1)+trib(n-2)+trib(n-3));
else
return -1;
}
public static void main(String s[])
{
int res;
Scanner in=new Scanner(System.in);
Tribonacci ob=new Tribonacci();
System.out.println("Enter the limit");
int limit=in.nextInt();
for(int i=1;i<limit;i++)
{
res=ob.trib(i);
System.out.print(res+" ");
}
}
}
output:
I
I hope this will give you the required,
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.