Hexanacci Numbers
Last Updated :
25 May, 2021
The hexanacci number is a generalization of the Fibonacci number where each term is the sum of the six preceding terms. The first few Hexanacci numbers are as follows – 0, 0, 0, 0, 0, 1, 1, 2, 4, 8, 16, 32, 63, 125, 248, 492, 976, 1936, 3840…..
Nth Term of Hexanacci number is given by :
T(n) = T(n-1) + T(n-2) + T(n-3) + T(n-4) + T(n-5) + T(n-6)
with T(0) = T(1) = T(2) = T(3) = T(4) = 0, T(5) = 1
Find the Nth term of the Hexanacci number
Given a number N. The task is to find the Nth Hexanacci number.
Examples:
Input: N = 8
Output: 2
Input: N = 11
Output: 16
Naive Approach: The idea 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<bits/stdc++.h>
#include<iostream>
using namespace std;
int printhexaRec( int n)
{
if (n == 0 || n == 1 ||
n == 2 || n == 3 ||
n == 4 || n == 5)
return 0;
else if (n == 6)
return 1;
else
return (printhexaRec(n - 1) +
printhexaRec(n - 2) +
printhexaRec(n - 3) +
printhexaRec(n - 4) +
printhexaRec(n - 5) +
printhexaRec(n - 6));
}
int printhexa( int n)
{
cout << printhexaRec(n) << endl;
}
int main()
{
privatenthexa(n);
}
|
Java
import java.util.*;
class GFG{
static int printhexaRec( int n)
{
if (n == 0 || n == 1 ||
n == 2 || n == 3 ||
n == 4 || n == 5 )
return 0 ;
else if (n == 6 )
return 1 ;
else
return (printhexaRec(n - 1 ) +
printhexaRec(n - 2 ) +
printhexaRec(n - 3 ) +
printhexaRec(n - 4 ) +
printhexaRec(n - 5 ) +
printhexaRec(n - 6 ));
}
static void printhexa( int n)
{
System.out.print(printhexaRec(n) + "\n" );
}
public static void main(String[] args)
{
int n = 11 ;
printhexa(n);
}
}
|
Python3
def printhexaRec(n) :
if (n = = 0 or n = = 1 or \
n = = 2 or n = = 3 or \
n = = 4 or n = = 5 ):
return 0
elif (n = = 6 ):
return 1
else :
return (printhexaRec(n - 1 ) +
printhexaRec(n - 2 ) +
printhexaRec(n - 3 ) +
printhexaRec(n - 4 ) +
printhexaRec(n - 5 ) +
printhexaRec(n - 6 ))
def printhexa(n) :
print (printhexaRec(n))
n = 11
printhexa(n)
|
C#
using System;
class GFG{
static int printhexaRec( int n)
{
if (n == 0 || n == 1 ||
n == 2 || n == 3 ||
n == 4 || n == 5)
return 0;
else if (n == 6)
return 1;
else
return (printhexaRec(n - 1) +
printhexaRec(n - 2) +
printhexaRec(n - 3) +
printhexaRec(n - 4) +
printhexaRec(n - 5) +
printhexaRec(n - 6));
}
static void printhexa( int n)
{
Console.Write(printhexaRec(n) + "\n" );
}
public static void Main()
{
int n = 11;
printhexa(n);
}
}
|
Javascript
<script>
function printhexaRec(n)
{
if (n == 0 || n == 1 ||
n == 2 || n == 3 ||
n == 4 || n == 5)
return 0;
else if (n == 6)
return 1;
else
return (printhexaRec(n - 1) +
printhexaRec(n - 2) +
printhexaRec(n - 3) +
printhexaRec(n - 4) +
printhexaRec(n - 5) +
printhexaRec(n - 6));
}
function printhexa(n)
{
document.write(printhexaRec(n) + "</br>" );
}
let n = 11;
printhexa(n);
</script>
|
Efficient Approach: A better solution is to use Dynamic Programming The idea is to keep track of the last six terms of the to compute the last current term of the series. Initialize the last six terms of the series as:
T(0) = T(1) = T(2) = T(3) = T(4) = 0, T(5) = 1
Finally, compute the terms up to the Nth term of the series:
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void printhexa( int n)
{
if (n < 0)
return ;
int first = 0;
int second = 0;
int third = 0;
int fourth = 0;
int fifth = 0;
int sixth = 1;
int curr = 0;
if (n < 6)
cout << first << endl;
else if (n == 6)
cout << sixth << endl;
else
{
for ( int i = 6; i < n; i++)
{
curr = first + second + third +
fourth + fifth + sixth;
first = second;
second = third;
third = fourth;
fourth = fifth;
fifth = sixth;
sixth = curr;
}
}
cout << curr << endl;
}
int main()
{
int n = 11;
printhexa(n);
return 0;
}
|
Java
class GFG {
static void printhexa( int n)
{
if (n < 0 )
return ;
int first = 0 ;
int second = 0 ;
int third = 0 ;
int fourth = 0 ;
int fifth = 0 ;
int sixth = 1 ;
int curr = 0 ;
if (n < 6 )
System.out.println(first);
else if (n == 6 )
System.out.println(sixth);
else
{
for ( int i = 6 ; i < n; i++)
{
curr = first + second + third +
fourth + fifth + sixth;
first = second;
second = third;
third = fourth;
fourth = fifth;
fifth = sixth;
sixth = curr;
}
}
System.out.println(curr);
}
public static void main(String[] args)
{
int n = 11 ;
printhexa(n);
}
}
|
Python3
def printhexa(n) :
if (n < 0 ):
return
first = 0
second = 0
third = 0
fourth = 0
fifth = 0
sixth = 1
curr = 0
if (n < 6 ):
print (first)
elif (n = = 6 ):
print (sixth)
else :
for i in range ( 6 , n):
curr = first + second + third + \
fourth + fifth + sixth
first = second
second = third
third = fourth
fourth = fifth
fifth = sixth
sixth = curr
print (curr)
n = 11
printhexa(n)
|
C#
using System;
class GFG{
static void printhexa( int n)
{
if (n < 0)
return ;
int first = 0;
int second = 0;
int third = 0;
int fourth = 0;
int fifth = 0;
int sixth = 1;
int curr = 0;
if (n < 6)
Console.WriteLine(first);
else if (n == 6)
Console.WriteLine(sixth);
else
{
for ( int i = 6; i < n; i++)
{
curr = first + second + third +
fourth + fifth + sixth;
first = second;
second = third;
third = fourth;
fourth = fifth;
fifth = sixth;
sixth = curr;
}
}
Console.WriteLine(curr);
}
public static void Main(String[] args)
{
int n = 11;
printhexa(n);
}
}
|
Javascript
<script>
function printhexa(n)
{
if (n < 0)
return ;
let first = 0;
let second = 0;
let third = 0;
let fourth = 0;
let fifth = 0;
let sixth = 1;
let curr = 0;
if (n < 6)
document.write(first);
else if (n == 6)
document.write(sixth);
else
{
for (let i = 6; i < n; i++)
{
curr = first + second + third +
fourth + fifth + sixth;
first = second;
second = third;
third = fourth;
fourth = fifth;
fifth = sixth;
sixth = curr;
}
}
document.write(curr);
}
let n = 11;
printhexa(n);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...