C++ Pentatope Number


A pentatope number is described as the fifth number in the pascal’s triangle. Now, as you know, it’s the fifth number, so it means that we need to have at least five numbers in the pascal’s triangle, so this series’ first number starts from 1 4 6 4 1 the fourth row of pascal’s triangle. So, In this given tutorial, we are required to find the nth pentatope number, for example

Input : 1

Output : 1

Input : 4

Output : 35

You can check the output from the following diagram −

Now for this problem, as you can, this is a type of series, so we try to find out the pattern for this series in the solution.

Approach to Find the Solution

In this program, we will find a general formula for this series which every number follows. Then we need to put our values to the formula, and then we get the output.

Example

C++ Code for the Above Approach 

#include<bits/stdc++.h>
using namespace std;
int answer(int n){ // function to find the value of nth pentatope number
    return (n * (n+1) * (n+2) * (n+3))/ 24; // the formula that we derived
}
int main(){
    int n = 6; // the pentatope number that we need to find
    cout << answer(n) << "\n";
    n = 4;
    cout << answer(n) << "\n";
    return 0;
}

Output

126
35

The overall complexity of the above code is O(1) which means it works in constant complexity, and this is the best time complexity that we can achieve because our time doesn’t depend on the input size so that we can calculate answers at the same time for any input.

Understanding the Code

In the above approach, as you know, we were trying to find out the pattern of the series and trying to devise a general formula from that pattern. Now the formula that we came up with was (n * (n + 1) * (n + 2) * (n + 3)) / 24 where n is the term which we need to find.

Conclusion

In this tutorial, we solve a problem to find the Nth Pentatope number by devising a formula for it. We also learned the C++ program for this problem and the complete approach we solved. We can write the same program in other languages such as C, java, python, and other languages. We hope you find this tutorial helpful.

Updated on: 25-Nov-2021

98 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements