In this guide, we will explore the process of calculating the Pentagonal Pyramidal Number using the C++ programming language.
What is the pentagonal pyramid?
A pentagonal pyramid is a specific type of pyramid featuring a pentagonal base, which consists of 5 sides akin to triangles, positioned on a surface. The pyramid's sides are also constructed in the form of 5 triangles. In geometry, a pyramid represents a 3D object with a base polygon, and its volume is determined by adding up the areas of the triangles located on each of the edges leading to a single apex. The pentagonal pyramid is a three-dimensional geometric figure comprising a pentagon as its base. Along the lateral sides, five triangles are configured, converging at the apex. Consequently, the vertex serves as the pinnacle of the pentagonal pyramid encompassing the three sides and the base. The base of a standard pentagonal pyramid is a regular pentagon, while each side's face takes the shape of an equilateral triangle.
A pentagonal pyramid is a special kind of pyramid. It has the same basic properties of pyramids as others and has special properties related to a pentagonal base. Here are the properties of a pentagonal pyramid are:
- The regular pentagonal pyramid increases in the front side to five equilateral triangles and the backside to a regular pentagon.
- There are 6 vertices in a pentagonal pyramid, and it has 10 edges.
- There are 6 faces in a pentagonal pyramid, and they are all the same.
- A pentagonal pyramid can also be built based on isosceles triangles as its lateral sides.
Method 1: Naivety Driven Approach
It's a straightforward method that entails iterating through the n values. Here is the code implementation of the described approach.
//Program to find the nth pentagonal pyramidal number
#include <bits/stdc++.h>
using namespace std;
//the function for getting the nth pentagonal number
int pentagon_pyramidalNumber(int num)
{
int total= 0;
// iterate a loop up to num
for (int i = 1; i <= num; i++)
{
// pentagonal number
int pnum = (3 * i * i - i) / 2;
// add to sum
total=total + pnum;
}
return total;
}
// Driver Program
int main()
{
int num = 6;
cout << pentagon_pyramidalNumber(num) << endl;
return 0;
}
Output:
Explanation:
The provided code determines the nth pentagonal pyramidal number. The pentagon_pyramidalNumber function takes an integer num as its argument and iterates from 1 to num. Within this loop, the base of the pyramid is formed in the shape of a pentagon using the expression num + 4i. During each iteration, the function computes the pentagonal number using the formula (3 i * i - i)/2 and accumulates the total sum. Upon completion of the loop, the function returns this total sum.
In the main function, the variable num is initialized to 6, and the result of calling the pentagon_pyramidalNumber function with num is displayed. The pentagonal pyramidal number sequence denotes the count of spheres required to construct a pyramid with a pentagonal base.
Method 2: Efficient approach
//Program to find the nth pentagonal pyramidal number
#include <bits/stdc++.h>
using namespace std;
// the function for finding the nth pyramidal number
int pentagon_pyramidalNumber(int number)
{
return number * number * (number + 1) / 2;
}
// Driver Program
int main()
{
int number= 2;
cout << pentagon_pyramidalNumber(number) << endl;
return 0;
}
Output:
Explanation:
This script calculates the nth pentagonal pyramidal value. The method pentagon_pyramidalNumber accepts an integer input and calculates the pentagonal pyramidal number by applying the formula number number (number + 1) / 2 . This mathematical expression represents the total sum of the initial number of triangular numbers.
Within the main function, a variable named number is initialized with a value of 2. Subsequently, the pentagon_pyramidalNumber function is invoked, and the output is displayed.
Conclusion:
In summary, the provided C++ code calculates the nth pentagonal pyramidal number using two distinct approaches.
The initial strategy is the straightforward one. It iterates through numbers from 1 to n, employing the equation (3 i i-i) / 2 for pentagonal number calculations. This approach involves summing these numbers together to derive a final result. For instance, this technique is illustrated by determining the 6th pentagonal pyramidal number, resulting in 126.
Alternatively, the second technique computes the pentagonal pyramidal number directly using the formula number number (number + 1) / 2. This method is akin to summing the first n triangular numbers arithmetically. To exemplify this method, the program showcases the generation of the second pyramidal number within the pentagonal series, which amounts to 6.
Both approaches accurately calculate the nth pentagonal pyramidal number, however, the latter method excels by eliminating the necessity for a repetitive loop.