Related Articles

# Program to print pentatope numbers upto Nth term

• Last Updated : 12 Apr, 2021

Prerequisites:

Given a value n, and the task is to print pentatope numbers series up to nth term.
Examples:

Input: 5
Output: 1 5 15 35 70

Input: 10
Output: 1 5 15 35 70 126 210 330 495 715 

Method 1: Using Tetrahedral Number Series:
This problem can be easily solved with the fact that Nth Pentatope Number is equal to the sum of first N Tetrahedral numbers.
Let’s have a look on Series of Pentatope and Tetrahedral Numbers.

For n = 5
Tetrahedral Numbers = 1, 4, 10, 20, 35
Prefix Sum of Tetrahedral numbers for each term: (1), (1 + 4), (1 + 4 + 10), (1 + 4 + 10 + 20), (1 + 4 + 10 + 20 + 35)
So, Pentatope numbers are 1, 5, 15, 35, 70

Calculate Nth Tetrahedral number using formula: So, print the Pentatope Numbers series by generating tetrahedral numbers and adding it with the sum of all previously generated Tetrahedral Numbers.
Below is the implementation of above approach:

## CPP

 // C++ program to generate Pentatope// Number series#include using namespace std; // Function to generate nth tetrahedral numberint findTetrahedralNumber(int n){    return ((n * (n + 1) * (n + 2)) / 6);} // Function to print pentatope number// series upto nth term.void printSeries(int n){    // Initialize prev as 0. It store the    // sum of all previously generated    // pentatope numbers    int prev = 0;    int curr;     // Loop to print pentatope series    for (int i = 1; i <= n; i++)    {        // Find ith tetrahedral number        curr = findTetrahedralNumber(i);         // Add ith tetrahedral number to        // sum of all previously generated        // tetrahedral number to get ith        // pentatope number        curr = curr + prev;        cout << curr << " ";         // Update sum of all previously        // generated tetrahedral number        prev = curr;    }} // Driver codeint main(){    int n = 10;         // Function call to print pentatope    // number series    printSeries(n);         return 0;}

## Java

 // Java program to generate Pentatope// Number seriesimport java.io.*; class GFG {         // Function to generate nth tetrahedral number    static int findTetrahedralNumber(int n)    {        return ((n * (n + 1) * (n + 2)) / 6);    }         // Function to print pentatope number    // series upto nth term.    static void printSeries(int n)    {        // Initialize prev as 0. It store the        // sum of all previously generated        // pentatope numbers        int prev = 0;        int curr;             // Loop to print pentatope series        for (int i = 1; i <= n; i++)        {            // Find ith tetrahedral number            curr = findTetrahedralNumber(i);                 // Add ith tetrahedral number to            // sum of all previously generated            // tetrahedral number to get ith            // pentatope number            curr = curr + prev;            System.out.print(curr + " ");                 // Update sum of all previously            // generated tetrahedral number            prev = curr;        }    }     // Driver code    public static void main (String[] args)    {        int n = 10;             // Function call to print pentatope        // number series        printSeries(n);    }}

## python3

 # Python program to generate Pentatope# Number series # Function to generate nth tetrahedral numberdef findTetrahedralNumber(n) :    return (int((n * (n + 1) * (n + 2)) / 6)) # Function to print pentatope number# series upto nth term.def printSeries(n) :         # Initialize prev as 0. It store the    # sum of all previously generated    # pentatope numbers    prev = 0     # Loop to print pentatope series    for i in range(1, n + 1) :             # Find ith tetrahedral number        curr = findTetrahedralNumber(i)         # Add ith tetrahedral number to        # sum of all previously generated        # tetrahedral number to get ith        # pentatope number        curr = curr + prev;        print(curr, end=' ')         # Update sum of all previously        # generated tetrahedral number        prev = curr # Driver coden = 10     # Function call to print pentatope# number seriesprintSeries(n)

## C#

 // C# program to generate Pentatope// Number seriesusing System; public class GFG {         // Function to generate nth tetrahedral number    static int findTetrahedralNumber(int n)    {        return ((n * (n + 1) * (n + 2)) / 6);    }         // Function to print pentatope number    // series upto nth term.    static void printSeries(int n)    {        // Initialize prev as 0. It store the        // sum of all previously generated        // pentatope numbers        int prev = 0;        int curr;             // Loop to print pentatope series        for (int i = 1; i <= n; i++)        {            // Find ith tetrahedral number            curr = findTetrahedralNumber(i);                 // Add ith tetrahedral number to            // sum of all previously generated            // tetrahedral number to get ith            // pentatope number            curr = curr + prev;            Console.Write(curr + " ");                 // Update sum of all previously            // generated tetrahedral number            prev = curr;        }    }         // Driver code    static public void Main ()    {        int n = 10;             // Function call to print pentatope        // number series        printSeries(n);    }}

## PHP

 

## Javascript

 
Output:
1 5 15 35 70 126 210 330 495 715

Time Complexity: O(n)
Method 2: Using Pentatope Number Formula:
The formula to find Nth Pentatope number Below is the required implementation:

## CPP

 // C++ program to print Pentatope number series.#include using namespace std; // Function to print pentatope series up to nth termvoid printSeries(int n){     // Loop to print pentatope number series    for (int i = 1; i <= n; i++)    {        // calculate and print ith pentatope number        int num = (i * (i + 1) * (i + 2) * (i + 3) / 24);                 cout << num << " ";    }} // Driver codeint main(){    int n = 10;         // Function call to print pentatope number series    printSeries(n);    return 0;}

## Java

 // Java program to print Pentatope number series.import java.io.*; class GFG {         // Function to print pentatope series up to nth term    static void printSeries(int n)    {             // Loop to print pentatope number series        for (int i = 1; i <= n; i++)        {            // calculate and print ith pentatope number            int num = (i * (i + 1) * (i + 2) * (i + 3) / 24);                         System.out.print(num + " ");        }    }         // Driver code    public static void main (String[] args)    {        int n = 10;             // Function call to print pentatope number series        printSeries(n);    }}

## python3

 # Python program to print Pentatope number series. # Function to print pentatope series up to nth termdef printSeries(n) :     # Loop to print pentatope number series    for i in range(1, n + 1) :         # calculate and print ith pentatope number        num = int(i * (i + 1) * (i + 2) * (i + 3) // 24)                 print(num, end=' '); # Driver coden = 10     # Function call to print pentatope number seriesprintSeries(n)

## C#

 // C# program to print Pentatope number series.using System; public class GFG {         // Function to print pentatope series up to nth term    static void printSeries(int n)    {             // Loop to print pentatope number series        for (int i = 1; i <= n; i++)        {            // calculate and print ith pentatope number            int num = (i * (i + 1) * (i + 2) * (i + 3) / 24);                         Console.Write(num + " ");        }    }         // Driver code    static public void Main ()    {        int n = 10;             // Function call to print pentatope number series        printSeries(n);    }}

## PHP

 

## Javascript

 
Output:
1 5 15 35 70 126 210 330 495 715

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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up