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)

withT(0) = T(1) = T(2) = T(3) = 0, T(4) = 1

### Find the N^{th} 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:

## Python3

`# A simple recursive program to print ` `# Nth Pentanacci number ` ` ` `# Recursive program to find the Nth ` `# Pentanacci number ` `def` `printpentaRec(n) : ` ` ` `if` `(n ` `=` `=` `0` `or` `n ` `=` `=` `1` `or` `\ ` ` ` `n ` `=` `=` `2` `or` `n ` `=` `=` `3` `or` `n ` `=` `=` `4` `): ` ` ` `return` `0` ` ` `elif` `(n ` `=` `=` `5` `): ` ` ` `return` `1` ` ` `else` `: ` ` ` `return` `(printpentaRec(n ` `-` `1` `) ` `+` ` ` `printpentaRec(n ` `-` `2` `) ` `+` ` ` `printpentaRec(n ` `-` `3` `)` `+` ` ` `printpentaRec(n ` `-` `4` `)` `+` ` ` `printpentaRec(n ` `-` `5` `)) ` ` ` `# Function to print the Nth ` `# Pentanacci number ` `def` `printPenta(n) : ` ` ` `print` `(printpentaRec(n)) ` ` ` ` ` `# Driver code ` `n ` `=` `10` `printPenta(n) ` |

*chevron_right*

*filter_none*

**Output:**

16

**Efficient Approach:** The idea is to use **Dynamic Programming** to solve this problem. That is memoization of the solution in four variables for last four terms such that the same subproblem is not computed again and again.

Below is the implementation of the above approach:

## Python3

`# Python implementation to print ` `# Nth Pentanacci numbers. ` ` ` `# Function to print Nth ` `# Pentanacci number ` `def` `printpenta(n) : ` ` ` `if` `(n < ` `0` `): ` ` ` `return` ` ` ` ` `# Initialize first five ` ` ` `# numbers to base cases ` ` ` `first ` `=` `0` ` ` `second ` `=` `0` ` ` `third ` `=` `0` ` ` `fourth ` `=` `0` ` ` `fifth ` `=` `1` ` ` ` ` `# declare a current variable ` ` ` `curr ` `=` `0` ` ` ` ` `if` `(n ` `=` `=` `0` `or` `n ` `=` `=` `1` `or` `\ ` ` ` `n ` `=` `=` `2` `or` `n ` `=` `=` `3` `): ` ` ` `print` `(first) ` ` ` `elif` `(n ` `=` `=` `5` `): ` ` ` `print` `(fifth) ` ` ` ` ` `else` `: ` ` ` ` ` `# Loop to add previous five numbers ` ` ` `# for each number starting from 5 ` ` ` `# and then assign first, second, ` ` ` `# third, fourth to second, third, fourth ` ` ` `# and curr to fifth respectively ` ` ` `for` `i ` `in` `range` `(` `5` `, n): ` ` ` `curr ` `=` `first ` `+` `second ` `+` `\ ` ` ` `third ` `+` `fourth ` `+` `fifth ` ` ` `first ` `=` `second ` ` ` `second ` `=` `third ` ` ` `third ` `=` `fourth ` ` ` `fourth ` `=` `fifth ` ` ` `fifth ` `=` `curr ` ` ` ` ` `print` `(curr) ` ` ` `# Driver code ` `n ` `=` `10` `printpenta(n) ` |

*chevron_right*

*filter_none*

**Output:**

16

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Fill the missing numbers in the array of N natural numbers such that arr[i] not equal to i
- Print all distinct integers that can be formed by K numbers from a given array of N numbers
- Count of N-digit Numbers having Sum of even and odd positioned digits divisible by given numbers
- Count of numbers upto M divisible by given Prime Numbers
- Check if a given pair of Numbers are Betrothed numbers or not
- Numbers less than N which are product of exactly two distinct prime numbers
- Print N lines of 4 numbers such that every pair among 4 numbers has a GCD K
- Maximum sum of distinct numbers such that LCM of these numbers is N
- Count numbers which can be constructed using two numbers
- Count numbers which are divisible by all the numbers from 2 to 10
- Absolute difference between the Product of Non-Prime numbers and Prime numbers of an Array
- Number of ways to obtain each numbers in range [1, b+c] by adding any two numbers in range [a, b] and [b, c]
- Absolute Difference between the Sum of Non-Prime numbers and Prime numbers of an Array
- Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime
- D Numbers
- Self Numbers
- Add two numbers using ++ and/or --
- Sum of first n even numbers
- Sum of first N Star Numbers
- Sum of first n odd numbers in O(1) Complexity

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.