Skip to content
Related Articles

Related Articles

Improve Article

Program to print pentatope numbers upto Nth term

  • Last Updated : 12 Apr, 2021
Geek Week

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




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

Java




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

python3




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

C#




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

PHP




<?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);
?>

Javascript




<script>
 
// JavaScript 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
    let prev = 0;
    let curr;
 
    // Loop to print pentatope series
    for (let 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;
        document.write(curr+" ");
 
        // Update sum of all previously
        // generated tetrahedral number
        prev = curr;
    }
}
 
// Driver code
let n = 10;
     
// Function call to print pentatope
// number series
printSeries(n);
 
// This code is contributed by sravan kumar
 
</script>
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




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

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

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




<?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);
?>

Javascript




<script>
 
// Javascript 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 (let i = 1; i <= n; i++)
    {
        // calculate and print ith pentatope number
        num = (i * (i + 1) * (i + 2) * (i + 3) / 24);
         
        document.write(num+" ");
    }
}
 
// Driver code
let n = 10;
     
// Function call to print pentatope number series
printSeries(n);
 
// This code is contributed by sravan kumar
 
</script>
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
Recommended Articles
Page :