Program to print pentatope numbers upto Nth term

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: \frac{N\times (N+1)\times (N+2)}{6}

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to generate Pentatope
// Number series
#include <bits/stdc++.h>
using namespace std;
  
// Function to generate nth tetrahedral number
int 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 code
int main()
{
    int n = 10;
      
    // Function call to print pentatope
    // number series
    printSeries(n);
      
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to generate Pentatope
// Number series
import 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);
    }
}

chevron_right


python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to generate Pentatope
# Number series
  
# Function to generate nth tetrahedral number
def 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 code
n = 10
      
# Function call to print pentatope
# number series
printSeries(n)

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to generate Pentatope
// Number series
using 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);
    }
}

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to generate Pentatope
// Number series
  
// Function to generate nth tetrahedral number
function findTetrahedralNumber($n)
{
    return (($n * ($n + 1) * ($n + 2)) / 6);
}
  
// Function to print pentatope number 
// series upto nth term.
function printSeries($n)
{
    // Initialize prev as 0. It store the
    // sum of all previously generated 
    // pentatope numbers
    $prev = 0;
    $curr;
  
    // Loop to print pentatope series
    for ($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;
        echo($curr . " ");
  
        // Update sum of all previously 
        // generated tetrahedral number
        $prev = $curr;
    }
}
  
// Driver code
$n = 10;
      
// Function call to print pentatope
// number series
printSeries($n);
?>

chevron_right


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 \frac{N\times (N+1)\times (N+2)\times (N+3)}{24}

Below is the required implementation:

CPP

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to print Pentatope number series.
#include <bits/stdc++.h>
using namespace std;
  
// Function to print pentatope series up to nth term
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);
          
        cout << num << " ";
    }
}
  
// Driver code
int main()
{
    int n = 10;
      
    // Function call to print pentatope number series
    printSeries(n);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to print Pentatope number series.
  
# Function to print pentatope series up to nth term
def 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 code
n = 10
      
# Function call to print pentatope number series
printSeries(n)

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to print Pentatope number series.
  
// Function to print pentatope series up to nth term
function printSeries($n)
{
  
    // Loop to print pentatope number series
    for ($i = 1; $i <= $n; $i++)
    {
        // calculate and print ith pentatope number
        $num = ($i * ($i + 1) * ($i + 2) * ($i + 3) / 24);
          
        echo($num . " ");
    }
}
  
// Driver code
$n = 10;
      
// Function call to print pentatope number series
printSeries($n);
?>

chevron_right


Output:

1 5 15 35 70 126 210 330 495 715


My Personal Notes arrow_drop_up

self motivated and passionate programmer

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.