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 <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> |
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
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> |
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.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!