×

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.


Related Topics

How to Print array in Java?

A Java array is a data structure that allows us to hold components of the same data type. An array's items are kept in a single memory region. As a...

6 minutes read.

Find next greater number with same set of digits in Java

It contains a number (num). Finding the smallest number that has the same number of elements as num that is also larger than num is the task at hand. If...

8 minutes read.

ArrayDeque in Java

ArrayDeque The ArrayDeque is one of the essential concepts in java to implement the deque interface. It will allow us to apply a resizable array to implement the Deque interface. This...

8 minutes read.

Java Boolean compare() method

The compare() method of Java Boolean class compares the specified Boolean values and returns a positive 1 or negative 1 or zero integer value based on the result. Syntax public static int...

2 minutes read.

Java Set to List

In this article, you will be acknowledged about how the process of conversion from Set or HashSet to LinkedList happens and what are the possible ways involved in conversion process. First...

4 minutes read.

Java Math.multiplyExact() method in Java

Java has an inbuilt math function called Math.multiplyExact() that returns the sum of the parameters. If the result exceeds an integer, an exception is thrown. There is no need to...

2 minutes read.

Heart Pattern in Java

Heart Pattern is yet another intricate pattern program, however, due to its complexity, interviewers hardly ever inquire about it. Method for Printing the Heart Number Pattern Put the value of the total row...

2 minutes read.

Client Server Program in Java

Client Server Program in Java The client and server are the two main components of socket programming. The client is a computer/node that request for the service and the server is...

7 minutes read.

Types of Garbage Collector in Java

Garbage collection is a Java feature that offers automatic memory management. The JVM is in charge of it. The programmer does not have to handle object creation and deallocation. We...

3 minutes read.

Java Numbers

We use primitive data types like byte, int, long, double, etc to work with the numbers, When we need objects, we use wrapper class like Integer, Double, Long, Byte, etc....

2 minutes read.

Features of Java

The objective of the Java programming language is to provide portability, security, and simplicity. In spite of this, Java has a number of features that makes it a popular language...

5 minutes read.

Annotations in Java

Annotations in Java Java Annotations are metadata about the source code. They do not have any direct effect on the execution of the java program. Annotations in Java were introduced in...

4 minutes read.

Java Abstraction

Java Abstraction Abstraction is an advanced feature of Java to make it transparent. The main motive behind the abstraction is to deal with ideas, not with events. Abstraction is a process...

4 minutes read.

Find the Frequency of Each Element in the Array in Java

We may count the occurrence of each element in the array of items. Maintaining one array to store the counts of each array element is one strategy for solving this issue....

3 minutes read.

Package Program in Java

Package Program in Java The package program in Java helps us to understand the significance of packages in Java. Packages are mainly used to group together similar classes, interfaces and sub-packages....

6 minutes read.

Interchange Diagonal elements in Java

In this article, you will be very well acknowledged about what is a matrix with an example. You will also be acknowledged about how to interchange the diagonal elements in...

4 minutes read.

Java Integer class

The Integer class wraps a primitive int type value in an object. Its object contains only a single field whose type is int. Methods: The java.lang.Integer class provides several different methods for...

4 minutes read.

Frugal number in Java

A frugal number is a positive integer with base b that has more digits than the number of prime factors it can be factored into (including exponents greater than 1)....

3 minutes read.

How to Create Singleton Class in Java

In this tutorial, we will discuss the singleton class and how to create it. Introduction Java is a purely object-oriented programming language. It consists of classes and objects. But Singleton is one...

4 minutes read.

Why String in Immutable in Java?

Why String in Immutable in Java Immutable means unchangeable or unmodifiable.  Strings in Java are immutable, it means once a string is created, it cannot be modified or changed. Any change...

2 minutes read.