×

Tetranacci Number in Java

This article mainly describes tetranacci number identification and the Java Program for Tetranacci numbers.

Tetranacci number

Tetranacci numbers and Fibonacci numbers are related. The key contrast is that a Tetranacci number depends upon the final four Tetranacci numbers, but a Fibonacci number depends exclusively on the final two Fibonacci numbers.

The Tetranacci numbers are shown as follows in mathematics:

T(0) = 0,

T(1) =1,

T(2) =1,

T(3) =2,

T(n) = T(n - 4) + T(n - 3) + T(n - 2) + T(n - 1), where n >= 4

hence

T(4) = T(0) + T(1) + T(2) + T(3) = 0 + 1 + 1 + 2 = 4

T(5) = T(1) + T(2) + T(3) + T(4) = 1 + 1 + 2 + 4 = 8

T(6) = T(2) + T(3) + T(4) + T(5) = 1 + 2 + 4 + 8 = 15

Java Program for Tetranacci number :

The program below will describe the tetranacci number using mathematical formulas.

Example:TetranacciExample.java

// this program is for finding the first 10 tetranacci numbers
import java.io.*;
import java.util.*;  
public class TetranacciExample   
{  
// this method for finding the n tetranacci numbers
int findTetranacci(int num)  
{  
// the array of the size 4 is declared 
// the next tetranacci number will depend upon the 
// the 4 numbers
int array[] = new int[4];  
// the array was initialized
// the first four numbers of the array   
// the mathematical formula is declared  
array[0] = 0;  
array[1] = 1;  
array[2] = 1;  
array[3] = 2;  
if(num <= 3)  
{  
return array[num];  
}  
// loop for finding the tetranacci numbers  
for(int i = 4; i <= num; i++)  
{  
// The remaining 4 components determine the present Tetranacci number. 
int tem = array[0] + array[1] + array[2] + array[3];  
array[0] = array[1];  
array[1] = array[2];  
array[2] = array[3];  
array[3] = tem;  
}  
// array[3] will consist of the tetranacci number
return array[3];  
}  
// main section
public static void main(String argvs[])  
{  
// an object is created for the class TetranacciExample   
TetranacciExample object = new TetranacciExample();  
System.out.println("The initial 10 Tetranacci numbers are: ");  
// loop for finding the tetranacci numbers for the given range 
for(int i = 0; i <= 9; i++)  
{  
int nof = object.findTetranacci(i);  
System.out.print(nof + " ");  
}  
}  
}  

Output

Tetranacci Number in Java

Tetranacci number: Recursive Approach

The following mathematical method also functions as a recursive formula to determine the additional Tetranacci numbers if we consider the values of the first four Tetranacci numbers to represent the base case. The next Tetranacci number is generated recursively using the following format above method.

Example: TetranacciRecursive.java

// This program is for finding the first 10 tetranacci numbers
 //by using the recursive approach
//package section
Import java.io.*;
Import java.util.*;
public class TetranacciRecursive   
{  
//  this method for finding the n tetranacci numbers
int findTetranacci(int num)  
{  
// the below mentioned 3 if conditions are for the    
// for finding the first four tetranacci numbers
// which has the base values 
if(num <= 1)  
{  
return num;  
}  
if(num == 2)  
{  
return 1;  
}  
if(num == 3)  
{  
return 2;  
}  
// for finding the next Tetranacci number, the previous 4 numbers are very mandatory
// this method is for the recursive approach 
return findTetranacci(num - 1) +   
       findTetranacci(num - 2) +   
       findTetranacci(num - 3) +  
       findTetranacci(num - 4);  
}  
//main section of the program  
public static void main(String argvs[])  
{  
// an object is created for the class TetranacciRecursive()    
TetranacciRecursive object = new TetranacciRecursive();  
System.out.println("The first 10 tetranacci numbers of the series are: ");  
// loop for finding the Tetranacci numbers  
for(int i = 0; i <= 9; i++)  
{  
int number = object.findTetranacci(i);  
System.out.print(number + " ");  
}  
}  
}  

Output

Tetranacci Number in Java

Finding the Nth Tetranacci number

The below program will describe finding the Nth Tetranacci number

Example:NthTetranacciNumber.java

// This program is for finding the nth tetranacci number
//import section
import java.io.*;
import java.util.*;
public class NthTetranacciNumber
{
// declaring the function for returning the nth tetranacci number
static int displayTetraRec(int num)
{
//the base case for finding the tetranacci number is declared using the if the condition
if (num == 0)
return 0;
// base condition
if (num == 1 || num == 2)
return 1;
// base condition
if (num == 3)
return 2;


else
return displayTetraRec(num- 1) +
displayTetraRec(num- 2) +
displayTetraRec(num- 3) +
displayTetraRec(num- 4);
}


// function for displaying the result of the nth tetranacci number
static void displayTetra(int num)
{
System.out.println(displayTetraRec(num) + " ");
}


// main code of the program
public static void main(String[] args)
{
int num= 15;
    displayTetra(num);
}
}

Output

Tetranacci Number in Java

Related Topics

Why to use Enum in Java?

In this article, you will be acknowledged about the Enum in java, its uses and mainly its purpose. Enum has various functionalities. Each of them will be discussed. Enum In a computer...

3 minutes read.

Java Database Connectivity with MySQL

In this tutorial, we will learn how to connect Database with MySQL in Java. 5 Steps to Connect to the Database in Java Load the driver (or) Register the driver classEstablish a...

4 minutes read.

Java Obfuscator

Obfuscation is the process of making something unclear or difficult to interpret. Obfuscators are being used in programming to protect the source code from hackers. In this article, we will...

8 minutes read.

How to Convert String to float in Java

How to Convert String to Float in java It is used if you want to perform mathematical operations on the string that contains float number. You can convert String to float...

3 minutes read.

Sliding Window Problem in Java

A sliding window is used in computer science and data science to process large datasets. It involves breaking the dataset into smaller chunks or windows and then processing it in...

6 minutes read.

Zig Zag star and Number Pattern in Java

We covered many Java pattern applications in the preceding part. We will write Java applications for zigzag star and number patterns in this part. Printing Zig Zag Number Pattern Steps Print one...

3 minutes read.

Applet Program in Java

Applet Program in Java An applet is a program that can be embedded in a web page. Applets programs are run by a web browser. It mainly works on the client-side....

3 minutes read.

Null Pointer Exception in Java

It is a runtime error exception. The null value is allocated to the object reference in this exception. We will explicitly throw this null pointer exception when the program wants...

3 minutes read.

How to remove last character from String in Java

In java, there are predominantly three classes connected with the string. The classes are String, StringBuilder, and StringBuffer class that gives techniques connected with string control. Eliminating the first and...

5 minutes read.

Difference between throw and throws in java

This article shows you the core difference between “throw” and “throws”keywords in Java programming language.The throw keyword tells Java you want another part of the code to deal withthe exception,...

2 minutes read.

Java Integer numberOfLeadingZeros() method

The numberOfLeadingZeros()  method of Java Integer class returns the total number of zero bits preceding the highest-order one-bit in the 2’s complement binary representation of the specified int value.  Syntax public static...

1 minute read.

Static Array in Java

In this tutorial, we will study static arrays in Java. An array is a data structure that is of great importance in any programming language. It is classified into two...

3 minutes read.

How to Convert int to long in Java

How to Convert int to long in Java When two variables of different types are involved in the single expression, Java compiler uses built-in library function to convert the variable to...

2 minutes read.

How to set timer in Java

In this article, you will be very well equipped with the knowledge to set timer in java. The timer in java can be set by using timer class provided by...

3 minutes read.

Tetranacci Number in Java

This article mainly describes tetranacci number identification and the Java Program for Tetranacci numbers. Tetranacci number Tetranacci numbers and Fibonacci numbers are related. The key contrast is that a Tetranacci number depends...

3 minutes read.

Java RMI

What is RMI in Java? A framework for developing distributed Java applications is provided by the RMI (Remote Method Invocation) API. An object can call methods on an object running in...

4 minutes read.

How to compare dates in Java

Introduction: In Java, dates can be compared using a similar interface's compareTo() technique. This method returns 'zero' if each date is the same, returns a rate "more than 0" if...

6 minutes read.

How to Convert Decimal to Binary in Java

How to Convert Decimal to Binary in Java There are two methods to convert Decimal to Binary. Using toBinaryString() method Using user-defined logic Using Integer.toBinaryString() The toBinaryString() is a static method of Integer...

2 minutes read.

Java Single Linkedlist

Like arrays, linked lists are a type of linear data structure. Unlike arrays, which store each element in the same location, linked lists employ pointers to connect the elements together. In...

25 minutes read.

String Coding Interview Questions in Java

What is String in Java?In Java, a String is a Class that is defined in the java.lang package. It isn't a basic data type like int or long. Character Strings...

5 minutes read.