A heptagonal number is a number which can be represented as a heptagon. A heptagonal number can be represented as a combination of successive layers of heptagon( 7-sided polygon). be better explained with the below figures.
The first heptagon number is 1. Therefore, it can be represented by a small dot.
The second heptagonal number is 7, which can be represented by a heptagon.
The third heptagon number is 18, which can be represented by a heptagon and combined with a continuous heptagon layer.
The fourth heptagonal number is 34. It can be represented in the way shown above as a heptagon plus two consecutive layers of heptagons, giving 34.
Similar concepts will be used for further heptagonal numbers. Following the same logic, the first few heptagonal numbers are 1, 7, 18, 34, 55, 81, 112, 148, 189, 235, 286, 342, 403……
In this problem, our task is to give any positive number N as input and print the Nth heptagon number as output.
For example,
INPUT : N=6
Output : 81
INPUT : N=9
Output: 189
Now let's look at the algorithm we will use to solve this problem.
To solve this problem, we need to see the pattern followed by calculating the nth heptagonal number. The nth heptagonal number can be expressed as −
$$Heptagonal_{n}\:=\:\frac{n}{2}(5n\:-\:3)$$
If we look carefully at this expression, every heptagonal number has the following form
$\frac{n}{2}(5n\:-\:3)$, where n represents the number of heptagonal numbers.
Let us understand it better with examples.
For n=1, $\frac{1}{2}(5\:\times\:1\:-\:3)$= 1, which is the first heptagonal number.
For n=2, $\frac{2}{2}(5\:\times\:2\:-\:3)$= 7, which is the second heptagonal number.
When n=3, $\frac{3}{2}(5\:\times\:3\:-\:3)$= 18, which is the third heptagonal number.
Now, let us check the case of n=8. The result obtained by $\frac{8}{2}(5\:\times\:8\:-\:3)$ is 148, which is actually the eighth heptagonal number in the sequence of heptagonal numbers.
Since we can get any nth heptagonal number using the above expression, so in our method we will use this expression to calculate the nth heptagonal number, where n can be any positive number .
We will explain it in the following steps:
Take any positive number N as input and calculate the corresponding heptagon value N.
Initialize a function to calculate the Nth heptagon number.
Use the expression mentioned in the algorithm section, i.e. $\frac{N}{2}(5N\:-\:3)$, calculate the Nth heptagon number and store it in any in variables.
Returns the variable we stored, which will be the value of the Nth heptagonal number corresponding to any positive value N.
Note − We will use floating point data type instead of integer data type to avoid any errors due to decimal values when calculating the Nth heptagonal number using the above formula.
The Chinese translation ofImplement this method in C −
#include <bits/stdc++.h> #include <iostream> using namespace std; //function to calculate nth heptagonal number using formula n/2(5n-3) float heptagonal(float N){ float ans= (N/2)*((5*N) - 3); //to store nth heptagonal number return ans; } int main(){ float N=5; //input float a=heptagonal(N); //store the answer in a variable N=13; float b=heptagonal(N); cout<<a<<endl<<b<<endl; //print the answer return 0; }
55 403
Time complexity: O(1), Because it only takes constant time.
Space complexity: O(1), because no additional space is used.
We tried to learn the concept of heptagonal numbers and the formula for calculating the nth heptagonal number we used in the method.
I hope you found this article helpful in learning the concept of printing the nth heptagonal number entered by any user.
The above is the detailed content of heptagon number. For more information, please follow other related articles on the PHP Chinese website!