Table of contents
1.
Introduction
2.
What is Tribonacci Series?
3.
Tribonacci Series in Java Program
4.
 
5.
Find Nth Term of the Tribonacci Series
6.
Frequently Asked Questions
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: Dec 9, 2024
Easy

Tribonacci Series in Java

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

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 in Java

What is 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 the 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.

Tribonacci Series in Java Program

tribonacci series

 

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();  
 }  
} 
You can also try this code with Online Java Compiler
Run Code

 Output:

Enter Number of Terms:  12
Tribonacci Series:
0, 1, 1, 2, 4, 7, 13, 24, 44, 81, 149, 274
You can also try this code with Online Java Compiler
Run Code

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).

nth Term in Fibonacci series

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();  
     int answer =tribonacciTerm(nth);   
     System.out.println("The "+n+"th term of the Tribonacci series is: "+answer);  
  }  
}  
You can also try this code with Online Java Compiler
Run Code

Output:

Enter the nth term: 20
The 20th term of the Tribonacci series is: 35890
You can also try this code with Online Java Compiler
Run Code

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

Frequently Asked Questions

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 

The Tribonacci series in Java is a sequence where each term is the sum of the three preceding terms. We can calculate the Tribonacci series and the Nth term using loops and variables. The time complexity of the algorithm is O(N), and its space complexity is O(1). This sequence is closely related to the Fibonacci sequence and offers a unique pattern for numerical analysis.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.

Recommended Readings:

 

Live masterclass