Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Natural Numbers
  • Whole Numbers
  • Real Numbers
  • Integers
  • Rational Numbers
  • Irrational Numbers
  • Complex Numbers
  • Prime Numbers
  • Odd Numbers
  • Even Numbers
  • Properties of Numbers
  • Number System
Open In App
Next Article:
Pentatope Numbers
Next article icon

Pentanacci Numbers

Last Updated : 27 Feb, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

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




// A simple recursive program to print
// Nth Pentanacci number
#include<bits/stdc++.h>
using namespace std;
     
// Recursive function to find the Nth
// Pentanacci number
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));
}
             
// Function to print the Nth
// Pentanacci number
void printPenta(int n)
{
    cout << printpentaRec(n) << "\n";
}
             
// Driver code
int main()
{
    int n = 10;
     
    printPenta(n);
     
    return 0;
}
 
// This code is contributed by yatinagg
 
 

Java




// A simple recursive program to print
// Nth Pentanacci number
import java.util.*;
class GFG{
     
// Recursive function to find the Nth
// Pentanacci number
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));
}
             
// Function to print the Nth
// Pentanacci number
static void printPenta(int n)
{
    System.out.print(printpentaRec(n) + "\n");
}
             
// Driver code
public static void main(String[] args)
{
    int n = 10;
     
    printPenta(n);
}
}
 
// This code is contributed by gauravrajput1
 
 

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):
        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))
           
# Function to print the Nth
# Pentanacci number
def printPenta(n) :
    print(printpentaRec(n))
           
   
# Driver code
n = 10
printPenta(n)
 
 

C#




// A simple recursive program to print
// Nth Pentanacci number
using System;
 
class GFG{
     
// Recursive function to find the Nth
// Pentanacci number
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));
}
              
// Function to print the Nth
// Pentanacci number
static void printPenta(int n)
{
    Console.WriteLine(printpentaRec(n));
}
 
// Driver code
static void Main()
{
    int n = 10;
      
    printPenta(n);
}
}
 
// This code is contributed divyeshrabadiya07
 
 

Javascript




<script>
    // A simple recursive program to print
    // Nth Pentanacci number
     
    // Recursive function to find the Nth
    // Pentanacci number
    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 to print the Nth
    // Pentanacci number
    function printPenta(n)
    {
        document.write(printpentaRec(n) + "</br>");
    }
 
    let n = 10;    
    printPenta(n);
 
// This code is contributed by divyesh072019.
</script>
 
 
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 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




// C++14 implementation to print
// Nth Pentanacci numbers.
#include<bits/stdc++.h>
using namespace std;
     
// Function to print Nth
// Pentanacci number
void printpenta(int n)
{
    if (n < 0)
        return;
     
    // Initialize first five
    // numbers to base cases
    int first = 0;
    int second = 0;
    int third = 0;
    int fourth = 0;
    int fifth = 1;
     
    // Declare a current variable
    int curr = 0;
     
    if (n == 0 || n == 1 ||
        n == 2 || n == 3)
        cout << first << "\n";
         
    else if (n == 5)
        cout << fifth << "\n";
     
    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(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";
    }
}
             
// Driver code
int main()
{
    int n = 10;
     
    printpenta(n);
     
    return 0;
}
 
// This code is contributed by yatinagg
 
 

Java




// Java implementation to print
// Nth Pentanacci numbers.
import java.util.*;
class GFG{
     
// Function to print Nth
// Pentanacci number
static void printpenta(int n)
{
  if (n < 0)
    return;
 
  // Initialize first five
  // numbers to base cases
  int first = 0;
  int second = 0;
  int third = 0;
  int fourth = 0;
  int fifth = 1;
 
  // Declare a current variable
  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
  {
    // 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(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");
  }
}
             
// Driver code
public static void main(String[] args)
{
  int n = 10;
  printpenta(n);   
}
}
 
// This code is contributed by Princi Singh
 
 

Python3




# Python3 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)
 
 

C#




// C# implementation to print
// Nth Pentanacci numbers.
using System;
class GFG{
     
// Function to print Nth
// Pentanacci number
static void printpenta(int n)
{
  if (n < 0)
    return;
 
  // Initialize first five
  // numbers to base cases
  int first = 0;
  int second = 0;
  int third = 0;
  int fourth = 0;
  int fifth = 1;
 
  // Declare a current variable
  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
  {
    // 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(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");
  }
}
 
// Driver code
public static void Main(String[] args)
{
  int n = 10;
  printpenta(n);   
}
}
 
// This code is contributed by shikhasingrajput
 
 

Javascript




<script>
 
    // Javascript implementation to print
    // Nth Pentanacci numbers.
     
    // Function to print Nth
    // Pentanacci number
    function printpenta(n)
    {
      if (n < 0)
        return;
 
      // Initialize first five
      // numbers to base cases
      let first = 0;
      let second = 0;
      let third = 0;
      let fourth = 0;
      let fifth = 1;
 
      // Declare a current variable
      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
      {
        // 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(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>
 
 
Output: 
16

 



Next Article
Pentatope Numbers

S

SHUBHAMSINGH10
Improve
Article Tags :
  • DSA
  • Dynamic Programming
  • Mathematical
  • Recursion
  • Numbers
Practice Tags :
  • Dynamic Programming
  • Mathematical
  • Numbers
  • Recursion

Similar Reads

  • Tetranacci Numbers
    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
    13 min read
  • Tribonacci Numbers
    The tribonacci series is a generalization of the Fibonacci sequence where each term is the sum of the three preceding terms. a(n) = a(n-1) + a(n-2) + a(n-3) with a(0) = a(1) = 0, a(2) = 1. First few numbers in the Tribonacci Sequence are 0, 0, 1, 1, 2, 4, 7, 13, 24, 44, 81, 149, 274, 504, .... Given
    15+ min read
  • Pentatope Numbers
    Pentatope numbers represent how many small spheres you can fit together in a four-dimensional shape known as a Pentatope (4-dimensional tetrahedron). A pentatope is a four-dimensional geometric shape, the simplest form of a four-dimensional simplex, consisting of 5 vertices, 10 edges, and 10 triangu
    4 min read
  • Pentacontagon number
    Given a number N, the task is to find Nth Pentacontagon number. A Pentacontagon number is class of figurate number. It has 50 - sided polygon called pentacontagon. The N-th pentacontagon number count’s the 50 number of dots and all others dots are surrounding with a common sharing corner and make a
    3 min read
  • Pentadecagonal Number
    Given a number N, the task is to find the Nth Pentadecagonal number. A Pentadecagonal number is a figurate number that extends the concept of triangular and square numbers to the pentadecagon(a 15-sided polygon). The Nth pentadecagonal number counts the number of dots in a pattern of N nested pentad
    3 min read
  • Pentatope number
    Given a number n, find the nth Pentatope number. A pentatope number is represented by the fifth number in any row of Pascal's Triangle. As it is fifth number, it should start from row having at least 5 numbers. So, it starts from row 1 4 6 4 1. The formula for the nth pentatope number is: n (n+1) (n
    4 min read
  • n'th Pentagonal Number
    Given an integer n, find the nth Pentagonal number. The first three pentagonal numbers are 1, 5, and 12 (Please see the below diagram). The n'th pentagonal number Pn is the number of distinct dots in a pattern of dots consisting of the outlines of regular pentagons with sides up to n dots when the p
    7 min read
  • Pentacontahenagon Number
    Pentacontahenagon Number is a class of figurate numbers. It has a 51 sided polygon called Pentacontahenagon. The N-th Pentacontahenagon number count’s the 51 number of dots and all other dots are surrounding with a common sharing corner and make a pattern.First few Pentacontahenagonol Numbers are: 1
    3 min read
  • Tetradic Number
    A Tetradic Number (sometimes called a four-way number) is a number that remains the same when flipped back-front, mirrored up-down, or flipped up-down. In other words, a Tetradic Number is a palindromic number containing only 0, 1 and 8 as digits in the number. The first few Tetradic Number are: 0,
    11 min read
  • Geek-onacci Number
    Given four integers A, B, C and N, where A, B, C represents the first three numbers of the geekonacci series, the task is to find the Nth term of the geekonacci series. The Nth term of the geekonacci series is the sum of its previous three terms in the series i.e., sum of (N - 1)th, (N - 2)th, and (
    13 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences