×

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

Replace character in string Java

Characters in Java In the package of Java language, there is a container class called Character. A single field of type char is contained in a Character object. For manipulating characters,...

4 minutes read.

String Pool in Java

String Pool in Java: String is one of the most important discussed topics in Java. There are a lot of concepts related to the String and one of them is...

5 minutes read.

Creating a Custom Generic Class in Java

To indicate parameter types when creating generic classes, we utilize the <> symbol. The syntax used to generate objects of a generic class is as follows. // To create an instance...

4 minutes read.

How to create a linked list in Java

Introduction: The linked listing is one type of linear statistics shaped like an array. Not like arrays, linked listing factors aren't stored in a contiguous place. The elements have linked...

3 minutes read.

Java Math with Methods and Examples

Java Math class contains various methods for performing math operations like min(), max(), avg() and various trigonometric functions like sin(), cos(), tan() etc. Methods: The java.lang.Math class contains various methods for performing...

5 minutes read.

How to declare string array in Java

Introduction Array is a data structure with a fixed size, which allows us to store elements of similar type. Data of primitive types like int, char, float, string, etc. can be...

2 minutes read.

Java Xml Parser

In this article, we acknowledge you about what is a XML parser in Java, how is it going to work and what is the purpose of it. Also, the article discusses...

6 minutes read.

java.lang.NumberFormatException for Input String

java.lang.NumberFormatException for Input String The exception java.lang.NumberFormatException for input string occurs when we try to convert a string into a number format. For example, if someone converts the string “Tutorial & Example”...

3 minutes read.

Java Set Interface

We use set when we don't want to allow duplicate entries. All Set implementations do not allow duplicates. HashSet: This class stores its elements in hash tables. It uses the hashCode()...

3 minutes read.

Convert JSON to Map in Java

JACKSON and GSON libraries are two extremely capable JSON-related libraries offered by Java. To efficiently work with the returned JSON data, we frequently need to convert JSON responses into a...

6 minutes read.

Bedrock vs Java

The popularity of Minecraft, a sandbox video game, has skyrocketed. The scope, level of complexity, and variety of gameplay in this game are enormous, and user-generated content has helped to...

4 minutes read.

MOOD Factors to Assess a Java Program

In this tutorial, we will comprehendthe meaning of mood factors in Java. For the development of any software system,the quality of anapplication is important. It is more important to maintain large-scale...

4 minutes read.

Anagram Program in Java using String

Anagram Program in Java Anagrams are those words that are generated by rearrangement of the letters of another phrase or word. Usually, while doing the rearrangement, the original letters are used...

8 minutes read.

Hogben Numbers in Java

In this section, we will discover what the Hogben number is and develop Java programs that compute it. Java coding interviews and academic exams typically involve questions about the Hogben...

3 minutes read.

Functional Interface in Java 8

A brief introduction to Interface in Java: Interfaces in Java are basically the blue print of classes. Before the appearance of Java 8, it was only possible to declare one or...

11 minutes read.

Java Math getExponent() Method

The getExponent() method of Math class returns the unbiased exponent of the argument. Syntax: public static int getExponent (double d) Parameters: The parameter ‘d’ represents the double value. Return Value: The getExponent () method returns the...

1 minute 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.

How to take Multiple String Input in Java using Scanner class

Scanner class is a class which takes the multiple input data or the single input data through an objects and methods. The Scanner class will be available in the java.util...

3 minutes read.

Sort Elements by Frequency in Java

To sort the elements in Java by using frequency, we need an input array. We should create a function that sorts the elements in an array by using their frequencies...

3 minutes read.

Java Thread Dump Analyzer

Thread: A thread is a PC program that is stacked into the PC's memory and is under execution. It tends to be executed by a processor or a bunch of processors....

15 minutes read.