The Pentanacci series is a generalization of the Fibonacci sequence where each term is the sum of the five preceding terms. The first few Pentanacci numbers are as follows – 0, 0, 0, 0, 1, 1, 2, 4, 8, 16, 31, 61, 120, 236, 464, 912, 1793, 3525, 6930, 13624, 26784, 52656, 103519…..
Nth Term of Pentanacci number is given by:
T(n) = T(n-1) + T(n-2) + T(n-3) + T(n-4) + T(n-5)
with T(0) = T(1) = T(2) = T(3) = 0, T(4) = 1
Find the Nth Pentanacci number
Given a number N. The task is to find the N-th Pentanacci number.
Examples:
Input: N = 7
Output: 2
Input: N = 10
Output: 16
Naive Approach: The idea is to follow the recurrence for finding the number and use recursion to solve it.
Recurrence relation:
T(n) = T(n-1) + T(n-2) + T(n-3) + T(n-4) + T(n-5)
Below is the implementation of the above approach:
C++14
#include<bits/stdc++.h>
using namespace std;
int printpentaRec( int n)
{
if (n == 0 || n == 1 ||
n == 2 || n == 3)
return 0;
else if (n == 4 || n == 5)
return 1;
else
return (printpentaRec(n - 1) +
printpentaRec(n - 2) +
printpentaRec(n - 3)+
printpentaRec(n - 4)+
printpentaRec(n - 5));
}
void printPenta( int n)
{
cout << printpentaRec(n) << "\n" ;
}
int main()
{
int n = 10;
printPenta(n);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int printpentaRec( int n)
{
if (n == 0 || n == 1 ||
n == 2 || n == 3 )
return 0 ;
else if (n == 4 || n == 5 )
return 1 ;
else
return (printpentaRec(n - 1 ) +
printpentaRec(n - 2 ) +
printpentaRec(n - 3 ) +
printpentaRec(n - 4 ) +
printpentaRec(n - 5 ));
}
static void printPenta( int n)
{
System.out.print(printpentaRec(n) + "\n" );
}
public static void main(String[] args)
{
int n = 10 ;
printPenta(n);
}
}
|
Python3
def printpentaRec(n) :
if (n = = 0 or n = = 1 or \
n = = 2 or n = = 3 ):
return 0
elif (n = = 4 or n = = 5 ):
return 1
else :
return (printpentaRec(n - 1 ) +
printpentaRec(n - 2 ) +
printpentaRec(n - 3 ) +
printpentaRec(n - 4 ) +
printpentaRec(n - 5 ))
def printPenta(n) :
print (printpentaRec(n))
n = 10
printPenta(n)
|
C#
using System;
class GFG{
static int printpentaRec( int n)
{
if (n == 0 || n == 1 ||
n == 2 || n == 3)
return 0;
else if (n == 4 || n == 5)
return 1;
else
return (printpentaRec(n - 1) +
printpentaRec(n - 2) +
printpentaRec(n - 3) +
printpentaRec(n - 4) +
printpentaRec(n - 5));
}
static void printPenta( int n)
{
Console.WriteLine(printpentaRec(n));
}
static void Main()
{
int n = 10;
printPenta(n);
}
}
|
Javascript
<script>
function printpentaRec(n)
{
if (n == 0 || n == 1 ||
n == 2 || n == 3)
return 0;
else if (n == 4 || n == 5)
return 1;
else
return (printpentaRec(n - 1) +
printpentaRec(n - 2) +
printpentaRec(n - 3)+
printpentaRec(n - 4)+
printpentaRec(n - 5));
}
function printPenta(n)
{
document.write(printpentaRec(n) + "</br>" );
}
let n = 10;
printPenta(n);
</script>
|
Efficient Approach: The idea is to use Dynamic Programming to solve this problem. That is memoization of the solution in four variables for the last four terms such that the same subproblem is not computed again and again.
Below is the implementation of the above approach:
C++14
#include<bits/stdc++.h>
using namespace std;
void printpenta( int n)
{
if (n < 0)
return ;
int first = 0;
int second = 0;
int third = 0;
int fourth = 0;
int fifth = 1;
int curr = 0;
if (n == 0 || n == 1 ||
n == 2 || n == 3)
cout << first << "\n" ;
else if (n == 5)
cout << fifth << "\n" ;
else
{
for ( int i = 5; i < n; i++)
{
curr = first + second +
third + fourth + fifth;
first = second;
second = third;
third = fourth;
fourth = fifth;
fifth = curr;
}
cout << curr << "\n" ;
}
}
int main()
{
int n = 10;
printpenta(n);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void printpenta( int n)
{
if (n < 0 )
return ;
int first = 0 ;
int second = 0 ;
int third = 0 ;
int fourth = 0 ;
int fifth = 1 ;
int curr = 0 ;
if (n == 0 || n == 1 ||
n == 2 || n == 3 )
System.out.print(first + "\n" );
else if (n == 5 )
System.out.print(fifth + "\n" );
else
{
for ( int i = 5 ; i < n; i++)
{
curr = first + second +
third + fourth + fifth;
first = second;
second = third;
third = fourth;
fourth = fifth;
fifth = curr;
}
System.out.print(curr + "\n" );
}
}
public static void main(String[] args)
{
int n = 10 ;
printpenta(n);
}
}
|
Python3
def printpenta(n) :
if (n < 0 ):
return
first = 0
second = 0
third = 0
fourth = 0
fifth = 1
curr = 0
if (n = = 0 or n = = 1 or \
n = = 2 or n = = 3 ):
print (first)
elif (n = = 5 ):
print (fifth)
else :
for i in range ( 5 , n):
curr = first + second + \
third + fourth + fifth
first = second
second = third
third = fourth
fourth = fifth
fifth = curr
print (curr)
n = 10
printpenta(n)
|
C#
using System;
class GFG{
static void printpenta( int n)
{
if (n < 0)
return ;
int first = 0;
int second = 0;
int third = 0;
int fourth = 0;
int fifth = 1;
int curr = 0;
if (n == 0 || n == 1 ||
n == 2 || n == 3)
Console.Write(first + "\n" );
else if (n == 5)
Console.Write(fifth + "\n" );
else
{
for ( int i = 5; i < n; i++)
{
curr = first + second +
third + fourth + fifth;
first = second;
second = third;
third = fourth;
fourth = fifth;
fifth = curr;
}
Console.Write(curr + "\n" );
}
}
public static void Main(String[] args)
{
int n = 10;
printpenta(n);
}
}
|
Javascript
<script>
function printpenta(n)
{
if (n < 0)
return ;
let first = 0;
let second = 0;
let third = 0;
let fourth = 0;
let fifth = 1;
let curr = 0;
if (n == 0 || n == 1 ||
n == 2 || n == 3)
document.write(first + "</br>" );
else if (n == 5)
document.write(fifth + "</br>" );
else
{
for (let i = 5; i < n; i++)
{
curr = first + second +
third + fourth + fifth;
first = second;
second = third;
third = fourth;
fourth = fifth;
fifth = curr;
}
document.write(curr + "</br>" );
}
}
let n = 10;
printpenta(n);
</script>
|
Last Updated :
27 Feb, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...