Open In App

Program to print pentatope numbers upto Nth term

Last Updated : 25 May, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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), where n represents the given integer.
Auxiliary Space: O(1), no extra space is required, so it is a constant.


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

 

Time Complexity: O(n), where n represents the given integer.
Auxiliary Space: O(1), no extra space is required, so it is a constant.



Similar Reads

Nth term where K+1th term is product of Kth term with difference of max and min digit of Kth term
Given two integers N and D, the task is to find the value of F(N) where the value of F(1) is D, where F(K) is given as: [Tex]F(K+1) = F(K) * (Max_{Digit}(F(K)) - Min_{Digit}(F(K)))[/Tex] Examples: Input: N = 3, D = 487 Output: 15584 Explanation: As F(1) = 487, F(2) = 487 * (maxDigit(487) - minDigit(487)) = 487 * 4 = 1948 F(3) = 1948 * (maxDigit(194
6 min read
Program to print tetrahedral numbers upto Nth term
Prerequisites: Triangular numbersTetrahedral numbers Given a value n, and the task is to print tetrahedral number series up to nth term.Examples: Input: 5 Output: 1 4 10 20 35 Input: 10 Output: 1 4 10 20 35 56 84 120 165 220 Method 1: Using Triangular Number series: This problem can be easily solved with the fact that Nth Tetrahedral Number is equa
9 min read
Program to get the Sum of series: 1 - x^2/2! + x^4/4! -.... upto nth term
This is a mathematical series program where a user must enter the number of terms up to which the sum of the series is to be found. Following this, we also need the value of x, which forms the base of the series. Examples: Input : x = 9, n = 10 Output : -5.1463 Input : x = 5, n = 15 Output : 0.2837 Simple approach : We use two nested loops to compu
10 min read
Program for sum of cosh(x) series upto Nth term
Given two numbers x and N, the task is to find the value of cosh(x) from the series upto N terms.The expansion of cosh(x) is given below: cosh(x) = 1 + x2/2! + x4/4! + ............ Examples: Input: x = 1, N = 5 Output: 1.54308035714 Input: x = 1, N = 10 Output: 1.54308063497 Approach: The above series can be easily implemented using a factorial fun
4 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+2) (n+3) / 24 Starting Pentatope numbers are : 1,
4 min read
Pentatope number
Given a number n, the task is to write a program to find Nth pentatope number. A pentatope number is class of figurative number and can be represented as regular and discrete geometric patterns. In pascal triangle's the number in the fifth cell of any row starting with 5-th term row 1 4 6 4 1 from right to left or left to right is Pentatope number.
4 min read
Find Nth term of the series where each term differs by 6 and 2 alternately
Given a number N, the task is to find the Nth term of the series where each term differs by 6 and 2 alternately.Examples: Input: N = 6 Output: 24 Explanation: The Nth term is 0 + 6 + 2 + 6 + 2 + 6 + 2 = 24Input: N = 3 Output: 14 Explanation: The Nth term is 0 + 6 + 2 + 6 = 14 Naive Approach: The idea is to iterate from 1 with an increment of 6 and
7 min read
Nth term of a sequence formed by sum of current term with product of its largest and smallest digit
Given two numbers N and K, where K represents the starting term of the sequence. The task is to find the Nth term of a sequence formed by sum of current term with product of largest and smallest digit of current term, i.e., AN+1 = AN + max(digits of AN) * min(digits of AN) Examples: Input: K = 1, N = 5 Output: 50 Explanation: A1 = 1 A2 = A1 + minDi
6 min read
Nth term of given recurrence relation having each term equal to the product of previous K terms
Given two positive integers N and K and an array F[] consisting of K positive integers. The Nth term of the recurrence relation is given by: FN = FN - 1 * FN - 2 * FN - 3 *.......* FN - K The task is to find the Nth term of the given recurrence relation. As the Nth term can be very large, print the Nth term modulo 109 + 7. Examples: Input: N = 5, K
16 min read
First term from given Nth term of the equation F(N) = (2 * F(N - 1)) % 10^9 + 7
Given an integer N and an integer FN which denotes the Nth term of the linear equation F(N) = (2 * F(N - 1)) % M, where M is 109 + 7, the task is to find the value of F(1). Examples: Input : N = 2, FN = 6 Output:3 Explanation: If F(1) = 3, F(2) = (2 * F(1)) % M = (2 * 3) % M = 6. For F(1) = 3 the given linear equation satisfies the value of F(2). T
8 min read