Tribonacci series in Java
The Fibonacci series and the Tribonacci sequence are linked. The Fibonacci sequence, each element summates the three preceding terms, is expanded into the Tribonacci series in Java. Through specific examples, we will understand the Tribonacci Series and the Nth element of the Tribonacci Sequence in Java in this article.
Tribonacci series
An integer sequence known as a Tribonacci series has fourth terms that are the sum of the terms before them.
The basic form of a Tribonacci series is as follows:
a(nth) equals a(n-1)st, a(n-2)st, and a(n-3)st
This is the Tribonacci Series' general logic.
Here,
a(0) =0, a(1) = 1, a(2) = 1
In other words, the summation of the three phrases before each entry in the series. The Tribonacci series' initial entries are as follows:
0, 1, 1, 2, 4, 7, 13, 24, 44, 81, 149, 274, 504, 927, 1705, 3136, 5768, 10609, 19513, 35890, 66012, 121415, 223317, 410744, 755476, 1389537, 2555757, 4700770, .. and so on.
Java program for Tribonacci series
The very first three terms in the Tribonacci series are initiated. We add the previous three terms from the fourth term to generate the following term.
Let's put the reasoning into a Java program.
Tribonacci.java
import java.util.*;
public class Tribonacci
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println(" Enter the Length of series is : ");
int len = sc.nextInt();
int t1 = 1, t2 = 0, t3 = 1;
int t4 = t1 + t2 + t3;
System.out.println(" Tribonacci Series till the given length is given below : ");
System.out.print(t1 + "\t" + t2 +"\t" +t3);
for(int i = 4; i <= len; i ++)
{
System.out.print("\t" + t4);
t1 = t2;
t2 = t3;
t3 = t4;
t4 = t1 + t2 + t3;
}
System.out.println();
}
}
Output
Enter the Length of series is : 9
Tribonacci Series till the given Length is given below :
0 1 1 2 4 7 13 24 44
In this scenario, the initial terms have been set to 1, 0, and 1 as t1, t2, and t3, correspondingly. Then the loop starts. To enable the addition of the final three integers and printing of their sum each time, the fourth term is defined as "t4," the preceding term is displayed, and all of the terms are then switched with the following elements. This continues to function until 'len' items are generated, which is the end of the series.
Java program to find the nth element of the Tribonacci series
NthnumTribonacci.java
import java.util.*;
public class NthnumTribonacci
{
public static int tribonacciTerm(int len)
{
if (len < 3)
return len;
// establishing the Tribonnaci series' initial three terms
int t1 = 0, t2 = 1, t3 = 1, t4;
// In the while loop, the operator --> is not allowed. — and > are two distinct
//operators. The code for the conditional decrements n by one before returning
// n's initial value, which has not been decremented, and using the > operator,
// it compares the original value to 3.
//We can express it more precisely as while ((n—) > 3)
while (n-- > 3)
{
t4 = t1 + t2 + t3;
t1 = t2;
t2 = t3;
t3 = t4;
}
return c;
}
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System. out.print(" Enter the Nth num required of series is : ");
// Takes an integer denoting the nth number of the series
int len = sc.nextInt();
int res = tribonacciTerm(len);
System.out.println("The " + len + "th term of the Tribonacci Sequence is : " + ans);
}
}
Output
Enter the Nth num required of series is : 20
The 20th term of the Tribonacci Sequence is : 35890
In this case, --> is not a valid operator within the while loop. — and > are two distinct operators. The conditional code reduces n by one while returning the original number—not the reduced value—and then compares the initial price to 3 using the > operator. If we want to be more explicit, we can say while ((n—) > 3). The rest of it is identical to Java's Tribonacci Series.