Centered Heptagonal Number


What do you understand by the term centered hepatgonal number? Let’s decode in this article.

First of all, what is a heptagonal number?

A heptagonal number is a figurate number representing the number of dots that can be arranged to form a regular heptagon (a seven−sided polygon). The formula for the nth heptagonal number is:

n(5n−3)/2, where n must be a positive integer.

The first few heptagonal numbers, for example, are:

  • 1 is the first heptagonal number (corresponding to a heptagon with one dot).

  • 7 is the second heptagonal number (corresponding to a heptagon with 7 dots).

  • 18 is the third heptagonal number (corresponding to a heptagon with 18 dots).

  • 34 is the fourth heptagonal number (corresponding to a heptagon with 34 dots).

  • And so forth.

Now, what is a centered heptagonal number?

A centered heptagonal number is a figurate number that represents the number of dots that can be arranged in the shape of a heptagon with a dot in the center and additional layers of dots surrounding it.

First, few centered heptagonal numbers are 1,8,22,43,...

The formula for the nth−centered heptagonal number is:

(7n^2 − 7n + 2)/2, where n is a positive integer.

Approach

Now, we have discussed what is centered heptagonal number. Let’s look at the stepwise approach to convert the discussed logic into working code.

  • Specify the value of n, you can also take this value as user input.

  • Use the formula (7n^2 − 7n + 2)/2, to calculate nth centered heptagonal number.

  • Print your calculation to the console.

C++ Code Implementation

Too much theory? Let’s get back to code. Here is the c++ implementation to calculate nth centered heptagonal number.

Example

#include <iostream>
using namespace std;

int main() {
    int n=7;

    int centered_heptagonal = (7 * n * n - 7 * n + 2) / 2;
    cout << "The " << n << "th centered heptagonal number is: " << centered_heptagonal << endl;

    return 0;
}

Output

The 7th centered heptagonal number is: 148

Time Complexity: O(1)

Space Complexity: O(1)

Conclusion

In this article, we have covered what is heptagonal number, along with what is centered heptagonal number. In addition we also covered how to calculate nth centered heptagonal number and wrote c++ code for the same. Hope you found the article insightful and able to grasp the concept better.

Updated on: 23-Aug-2023

73 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements