1.
Introduction
2.
Tribonacci Series
3.
Tribonacci Series in Java Program
4.

5.
Find Nth Term of the Tribonacci Series
6.
6.1.
What Exactly is the Tribonacci Series in Java?
6.2.
Which series is related to the Tribonacci series in Java?
6.3.
Write General form of Tribonacci series in Java?
6.4.
What are the first ten terms of the Tribonacci Series in Java?
6.5.
What is the 10th term of the Tribonacci Series in Java?
7.
Conclusion
Last Updated: Mar 27, 2024
Easy

# Tribonacci Series in Java

Create a resume that lands you SDE interviews at MAANG
Speaker
Anubhav Sinha
SDE-2 @
12 Jun, 2024 @ 01:30 PM

## Introduction

The Tribonacci sequence is related to the Fibonacci sequence. The Tribonacci sequence in Java is an extension of the Fibonacci sequence in which each term is the sum of the three previous terms. In this blog, we will learn about the Tribonacci Series and the Nth term of the Tribonacci Series in Java with the help of some examples.

## Tribonacci Series

A Tribonacci sequence or series is an integer sequence in which each fourth term is the sum of the previous three terms.

Following is General form of the Tribonacci sequence:

a(nth) = a(n-1)th + a(n-2)th + a(n-3)th

This is the general Logical of Tribonacci Series

Here,

a(0) =0, a(1) = 1, a(2) = 1

In other words, each term in the series is the sum of the three preceding terms. The first few entries in the Tribonacci series 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, 8646064, 15902591, 29249425, 53798080, 98950096, 181997601, 334745777…. So on.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

## Tribonacci Series in Java Program

Let's put the logic into a Java program.

``````import java.util.*;
public class TribonacciSeries
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);

System.out.print("Enter Number of terms: ");
int n=sc.nextInt();
int x = 1, y = 0, z = 1;
int w = x + y + z;

System.out.println("Tribonacci Series: ");
System.out.print(x+y+z);
for(int i=4; i<=n; i++)
{
System.out.print(+w);
x=y;
y=z;
z=w;
w=x+y+z;
}
System.out.println();
}
} ``````

Output:

``````Enter Number of Terms:  12
Tribonacci Series:
0, 1, 1, 2, 4, 7, 13, 24, 44, 81, 149, 274``````

Explanation:- The initial terms in this case have been initialized with 1,0 and 1 as x, y, and z, respectively. The loop then runs. When we get to the fourth term, it is declared as 'w,' the previous term is printed, and all the terms are swapped with the next elements so that the last three integers can be added and their sum is printed each time. This works until 'n' terms are reached and the entire series is generated.

Time Complexity

The time complexity of F(n) can be calculated by counting the number of times its most expensive operation will be executed for n inputs. The operation with the highest runtime cost in this algorithm is addition.

In the above Example the Time complexity and Space Complexity are:

Time Complexity: O(N)

Because we are only iterating each element once from N = 4 to N = N, the time complexity will be O (N)

Auxiliary Space: O(1)

Because we are simply utilising a variable to hold the response, the space complexity of the preceding code is O(1).

## Find Nth Term of the Tribonacci Series

``````import java.util.*;
public class nthTribonacciTerm
{
public static int tribonacciTerm(int n)
{
if (n < 3)
return n;
int x = 0, y = 1, z = 1, w;
while (n-- > 3)
{
w = x + y + z;
x = y;
y = z;
z = w;
}
return z;
}
public static void main(String args[])
{
Scanner scan = new Scanner(System.in);
System.out.print("Enter the nth term: ");
int n=scan.nextInt();
System.out.println("The "+n+"th term of the Tribonacci series is: "+answer);
}
}  ``````

Output:

``````Enter the nth term: 20
The 20th term of the Tribonacci series is: 35890``````

Practice it on online java compiler for better understanding.

Explanation: In the while loop --> is not an operator. These are two separate operators, -- and >. The conditional code decrements n by 1 while returning the original number, not the decremented value, and then uses the > operator to compare the original value to 3. We may express it more precisely as while ((n—) > 3). Rest it is the same as the Tribonacci Series in Java.

In the above Example the Time complexity and Space Complexity are:

Time Complexity: O(N)

Because we are iterating till the N-th tribonacci number, the time complexity of the following code is O(n). Here, n is the supplied integer for which the N-th tribonacci number must be calculated.

Auxiliary Space: O(1)

Because we are simply utilising a variable to hold the response, the space complexity of the preceding code is O(1).

Must Read Type Conversion in Java

### What Exactly is the Tribonacci Series in Java?

A Tribonacci sequence or series is an integer sequence in which each fourth term is the sum of the previous three terms.

### Which series is related to the Tribonacci series in Java?

The Tribonacci series is related to the Fibonacci series.

### Write General form of Tribonacci series in Java?

General form of the Tribonacci sequence is:

a(nth) = a(n-1)th + a(n-2)th + a(n-3)th

Here,

a(0) =0, a(1) = 1, a(2) = 1

### What are the first ten terms of the Tribonacci Series in Java?

Tribonacci Series for the first 10 terms are:

0, 1, 1, 2, 4, 7, 13, 24, 44, 81

### What is the 10th term of the Tribonacci Series in Java?

The 10th term of the Tribonacci series is: 81

## Conclusion

In this article, we learned about the Tribonacci Series in Java, which includes The Sequence and the Nth term in the Tribonacci Series in Java.

After reading about the Tribonacci Series in Java, are you not feeling excited to read/explore more articles on the topic of Ruby? Don't worry; Coding Ninjas has you covered. To learn, see Fibonacci series in JavaGroupLayout in Java, and BorderLayout in Java.