Tetranacci Numbers
Last Updated :
15 Jan, 2023
The tetranacci numbers are a generalization of the Fibonacci numbers defined by the recurrence relation
T(n) = T(n-1) + T(n-2) + T(n-3) + T(n-4)
with T(0)=0, T(1)=1, T(2)=1, T(3)=2,
For n>=4. They represent the n=4 case of the Fibonacci n-step numbers. The first few terms for n=0, 1, … are 0, 1, 1, 2, 4, 8, 15, 29, 56, 108, 208, …
Given a number N. The task is to find the N-th tetranacci number.
Examples:
Input: 5
Output: 4
Input: 9
Output: 108
A naive approach is to follow the recurrence for finding the number and use recursion to solve it.
Below is the implementation of the above approach.
C++
#include <iostream>
using namespace std;
int printTetraRec( int n)
{
if (n == 0)
return 0;
if (n == 1 || n == 2)
return 1;
if (n == 3)
return 2;
else
return printTetraRec(n - 1) + printTetraRec(n - 2)
+ printTetraRec(n - 3) + printTetraRec(n - 4);
}
void printTetra( int n)
{
cout << printTetraRec(n) << " " ;
}
int main()
{
int n = 10;
printTetra(n);
return 0;
}
|
Java
class GFG
{
static int printTetraRec( int n)
{
if (n == 0 )
return 0 ;
if (n == 1 || n == 2 )
return 1 ;
if (n == 3 )
return 2 ;
else
return printTetraRec(n - 1 ) +
printTetraRec(n - 2 ) +
printTetraRec(n - 3 ) +
printTetraRec(n - 4 );
}
static void printTetra( int n)
{
System.out.println(printTetraRec(n) + " " );
}
public static void main(String[] args)
{
int n = 10 ;
printTetra(n);
}
}
|
Python3
def printTetraRec(n):
if (n = = 0 ):
return 0 ;
if (n = = 1 or n = = 2 ):
return 1 ;
if (n = = 3 ):
return 2 ;
else :
return (printTetraRec(n - 1 ) +
printTetraRec(n - 2 ) +
printTetraRec(n - 3 ) +
printTetraRec(n - 4 ));
def printTetra(n):
print (printTetraRec(n), end = " " );
n = 10 ;
printTetra(n);
|
C#
class GFG
{
static int printTetraRec( int n)
{
if (n == 0)
return 0;
if (n == 1 || n == 2)
return 1;
if (n == 3)
return 2;
else
return printTetraRec(n - 1) +
printTetraRec(n - 2) +
printTetraRec(n - 3) +
printTetraRec(n - 4);
}
static void printTetra( int n)
{
System.Console.WriteLine(
printTetraRec(n) + " " );
}
static void Main()
{
int n = 10;
printTetra(n);
}
}
|
PHP
<?php
function printTetraRec( $n )
{
if ( $n == 0)
return 0;
if ( $n == 1 || $n == 2)
return 1;
if ( $n == 3)
return 2;
else
return printTetraRec( $n - 1) +
printTetraRec( $n - 2) +
printTetraRec( $n - 3) +
printTetraRec( $n - 4);
}
function printTetra( $n )
{
echo printTetraRec( $n ) . " " ;
}
$n = 10;
printTetra( $n );
?>
|
Javascript
<script>
function printTetraRec(n)
{
if (n == 0)
return 0;
if (n == 1 || n == 2)
return 1;
if (n == 3)
return 2;
else
return printTetraRec(n - 1) +
printTetraRec(n - 2) +
printTetraRec(n - 3) +
printTetraRec(n - 4);
}
function printTetra(n)
{
document.write(printTetraRec(n) + " " + "</br>" );
}
let n = 10;
printTetra(n);
</script>
|
Time Complexity: O(4N)
Auxiliary Space: O(4N), The extra space is used due to the recursion call stack.
A better solution is to use Dynamic Programming (memoization) as there are multiple overlaps.
Given below is the recursive tree for N=10.
rec(10)
/ / \ \
rec(9) rec(8) rec(7) rec(6)
/ / \ \
rec(8) rec(7) rec(6) rec(5)
In the above partial recursion tree, rec(8), rec(7), rec(6) have been solved twice. In drawing the complete recursion tree, it has been observed that there are many subproblems that are solved again and again. So this problem has overlapping Substructure property and recomputation of same subproblems can be avoided by either using Memoization or Tabulation.
Below is the implementation of the above approach.
C++
#include <iostream>
using namespace std;
int printTetra( int n)
{
int dp[n + 5];
dp[0] = 0;
dp[1] = dp[2] = 1;
dp[3] = 2;
for ( int i = 4; i <= n; i++)
dp[i] = dp[i - 1] + dp[i - 2] +
dp[i - 3] + dp[i - 4];
cout << dp[n];
}
int main()
{
int n = 10;
printTetra(n);
return 0;
}
|
Java
class GFG{
static void printTetra( int n)
{
int [] dp= new int [n + 5 ];
dp[ 0 ] = 0 ;
dp[ 1 ] = dp[ 2 ] = 1 ;
dp[ 3 ] = 2 ;
for ( int i = 4 ; i <= n; i++)
dp[i] = dp[i - 1 ] + dp[i - 2 ] +
dp[i - 3 ] + dp[i - 4 ];
System.out.print(dp[n]);
}
public static void main(String[] args)
{
int n = 10 ;
printTetra(n);
}
}
|
Python3
def printTetra(n):
dp = [ 0 ] * (n + 5 );
dp[ 0 ] = 0 ;
dp[ 1 ] = 1 ;
dp[ 2 ] = 1 ;
dp[ 3 ] = 2 ;
for i in range ( 4 , n + 1 ):
dp[i] = (dp[i - 1 ] + dp[i - 2 ] +
dp[i - 3 ] + dp[i - 4 ]);
print (dp[n]);
n = 10 ;
printTetra(n);
|
C#
class GFG{
static void printTetra( int n)
{
int [] dp= new int [n + 5];
dp[0] = 0;
dp[1] = dp[2] = 1;
dp[3] = 2;
for ( int i = 4; i <= n; i++)
dp[i] = dp[i - 1] + dp[i - 2] +
dp[i - 3] + dp[i - 4];
System.Console.WriteLine(dp[n]);
}
static void Main()
{
int n = 10;
printTetra(n);
}
}
|
PHP
<?php
function printTetra( $n )
{
$dp = array_fill (0, $n + 5, 0);
$dp [0] = 0;
$dp [1] = $dp [2] = 1;
$dp [3] = 2;
for ( $i = 4; $i <= $n ; $i ++)
$dp [ $i ] = $dp [ $i - 1] + $dp [ $i - 2] +
$dp [ $i - 3] + $dp [ $i - 4];
echo $dp [ $n ];
}
$n = 10;
printTetra( $n );
?>
|
Javascript
<script>
function printTetra(n)
{
let dp= new Array(n + 5);
dp[0] = 0;
dp[1] = dp[2] = 1;
dp[3] = 2;
for (let i = 4; i <= n; i++)
dp[i] = dp[i - 1] + dp[i - 2] +
dp[i - 3] + dp[i - 4];
document.write(dp[n]);
}
let n = 10;
printTetra(n);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
The time complexity above is linear, but it requires extra space. Space used can be optimized in the above solution by using four variables to keep track of the previous four numbers.
Below is the implementation of the above approach.
C++
#include <iostream>
using namespace std;
void printTetra( int n)
{
if (n < 0)
return ;
int first = 0, second = 1;
int third = 1, fourth = 2;
int curr;
if (n == 0)
cout << first;
else if (n == 1 || n == 2)
cout << second;
else if (n == 3)
cout << fourth;
else {
for ( int i = 4; i <= n; i++) {
curr = first + second + third + fourth;
first = second;
second = third;
third = fourth;
fourth = curr;
}
cout << curr;
}
}
int main()
{
int n = 10;
printTetra(n);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
import java.lang.*;
class GFG{
static void printTetra( int n)
{
if (n < 0 )
return ;
int first = 0 , second = 1 ;
int third = 1 , fourth = 2 ;
int curr = 0 ;
if (n == 0 )
System.out.print(first);
else if (n == 1 || n == 2 )
System.out.print(second);
else if (n == 3 )
System.out.print(fourth);
else
{
for ( int i = 4 ; i <= n; i++)
{
curr = first + second + third + fourth;
first = second;
second = third;
third = fourth;
fourth = curr;
}
System.out.print(curr);
}
}
public static void main(String[] args)
{
int n = 10 ;
printTetra(n);
}
}
|
Python3
def printTetra(n):
if (n < 0 ):
return ;
first = 0 ;
second = 1 ;
third = 1 ;
fourth = 2 ;
curr = 0 ;
if (n = = 0 ):
print (first);
elif (n = = 1 or n = = 2 ):
print (second);
elif (n = = 3 ):
print (fourth);
else :
for i in range ( 4 , n + 1 ):
curr = first + second + third + fourth;
first = second;
second = third;
third = fourth;
fourth = curr;
print (curr);
n = 10 ;
printTetra(n);
|
C#
using System;
class GFG{
static void printTetra( int n)
{
if (n < 0)
return ;
int first = 0, second = 1;
int third = 1, fourth = 2;
int curr = 0;
if (n == 0)
Console.Write(first);
else if (n == 1 || n == 2)
Console.Write(second);
else if (n == 3)
Console.Write(fourth);
else
{
for ( int i = 4; i <= n; i++)
{
curr = first + second + third + fourth;
first = second;
second = third;
third = fourth;
fourth = curr;
}
Console.Write(curr);
}
}
static public void Main ()
{
int n = 10;
printTetra(n);
}
}
|
PHP
<?php
function printTetra( $n )
{
if ( $n < 0)
return ;
$first = 0;
$second = 1;
$third = 1;
$fourth = 2;
$curr ;
if ( $n == 0)
echo $first ;
else if ( $n == 1 || $n == 2)
echo $second ;
else if ( $n == 3)
echo $fourth ;
else
{
for ( $i = 4; $i <= $n ; $i ++)
{
$curr = $first + $second +
$third + $fourth ;
$first = $second ;
$second = $third ;
$third = $fourth ;
$fourth = $curr ;
}
echo $curr ;
}
}
$n = 10;
printTetra( $n );
?>
|
Javascript
<script>
function printTetra(n)
{
if (n < 0)
return ;
var first = 0, second = 1;
var third = 1, fourth = 2;
var curr;
if (n == 0)
cout << first;
else if (n == 1 || n == 2)
cout << second;
else if (n == 3)
cout << fourth;
else {
for ( var i = 4; i <= n; i++) {
curr = first + second + third + fourth;
first = second;
second = third;
third = fourth;
fourth = curr;
}
document.write( curr);
}
}
var n = 10;
printTetra(n);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...