Centered heptagonal number
In this article, we will explore the concept of centered heptagonal numbers and understand how to generate these numbers using a simple formula. Centered heptagonal numbers are a type of figurate numbers that can be visualized as dots arranged in the shape of a regular heptagon, with additional dots added to the center. These numbers have a fascinating pattern and can be found in various mathematical and geometric contexts.
Problem Statement: The problem is to generate the first 'k' centered heptagonal numbers, where 'k' is a positive integer provided by the user. For each value of 'n' from 1 to 'k', we need to calculate the corresponding centered heptagonal number using the formula:
Heptagonal(n) = (7 * n^2 - 7 * n + 2) / 2
Pseudocode:
centeredHeptagonalNo(k):
for n from 1 to k:
result = (7 * n^2 - 7 * n + 2) / 2
print result
Algorithm Explanation
-
Start the function
centeredHeptagonalNo
that takes 'k' as input, representing the number of centered heptagonal numbers to be generated. -
Initialize a loop from 'n' = 1 to 'k'.
-
Within the loop, apply the formula for centered heptagonal numbers, i.e.,
result = (7 * n^2 - 7 * n + 2) / 2
. -
Print the value of
result
for each 'n', which represents the centered heptagonal number for the given value of 'n'. -
End the loop.
-
End the function.
Example
Let's take 'k' = 5 as an example to demonstrate the algorithm:
-
Start with 'n' = 1:
result = (7 * 1^2 - 7 * 1 + 2) / 2 = (7 - 7 + 2) / 2 = 2 / 2 = 1
The first centered heptagonal number is 1. -
Increment 'n' to 2:
result = (7 * 2^2 - 7 * 2 + 2) / 2 = (28 - 14 + 2) / 2 = 16 / 2 = 8
The second centered heptagonal number is 8. -
Increment 'n' to 3:
result = (7 * 3^2 - 7 * 3 + 2) / 2 = (63 - 21 + 2) / 2 = 44 / 2 = 22
The third centered heptagonal number is 22. -
Increment 'n' to 4:
result = (7 * 4^2 - 7 * 4 + 2) / 2 = (112 - 28 + 2) / 2 = 86 / 2 = 43
The fourth centered heptagonal number is 43. -
Increment 'n' to 5:
result = (7 * 5^2 - 7 * 5 + 2) / 2 = (175 - 35 + 2) / 2 = 142 / 2 = 71
The fifth centered heptagonal number is 71.
Code Solution
Here given code implementation process.
// C Program for
// Centered heptagonal number
#include <stdio.h>
void centeredHeptagonalNo(int k)
{
// Print all initial k centered heptagonal number
for (int n = 1; n <= k; ++n)
{
// Formula
// (7n² - 7n + 2)
// ————————————————
// 2
// Calculate nth heptagonal number
int result = (7 *(n *n) - 7 *n + 2) / 2;
// Display Calculated value
printf(" %d", result);
}
}
int main()
{
// Centered heptagonal number are
// —————————————————————————————————————————————
// 1, 8, 22, 43, 71, 106, 148, 197, 253, 316,
// 386, 463, 547, 638, 736, 841, 953, 1072, 1198, 1331,
// 1471, 1618, 1772, 1933, 2101, 2276, 2458, 2647, 2843,
// 3046, 3256, 3473, 3697, 3928, 4166, 4411, 4663,
// 4922, 5188, 5461, 5741, 6028 ... etc
// n = 10
centeredHeptagonalNo(10);
return 0;
}
Output
1 8 22 43 71 106 148 197 253 316
// Java program for
// Centered heptagonal number
public class CenteredHeptagonal
{
public void centeredHeptagonalNo(int k)
{
// Print all initial k centered heptagonal number
for (int n = 1; n <= k; ++n)
{
// Formula
// (7n² - 7n + 2)
// ————————————————
// 2
// Calculate nth heptagonal number
int result = (7 * (n * n) - 7 * n + 2) / 2;
// Display Calculated value
System.out.print(" " + result);
}
}
public static void main(String[] args)
{
CenteredHeptagonal task = new CenteredHeptagonal();
// Centered heptagonal number are
// —————————————————————————————————————————————
// 1, 8, 22, 43, 71, 106, 148, 197, 253, 316,
// 386, 463, 547, 638, 736, 841, 953, 1072, 1198, 1331,
// 1471, 1618, 1772, 1933, 2101, 2276, 2458, 2647, 2843,
// 3046, 3256, 3473, 3697, 3928, 4166, 4411, 4663,
// 4922, 5188, 5461, 5741, 6028 ... etc
// n = 10
task.centeredHeptagonalNo(10);
}
}
Output
1 8 22 43 71 106 148 197 253 316
// Include header file
#include <iostream>
using namespace std;
// C++ program for
// Centered heptagonal number
class CenteredHeptagonal
{
public: void centeredHeptagonalNo(int k)
{
// Print all initial k centered heptagonal number
for (int n = 1; n <= k; ++n)
{
// Formula
// (7n² - 7n + 2)
// ————————————————
// 2
// Calculate nth heptagonal number
int result = (7 *(n *n) - 7 *n + 2) / 2;
// Display Calculated value
cout << " " << result;
}
}
};
int main()
{
CenteredHeptagonal *task = new CenteredHeptagonal();
// Centered heptagonal number are
// —————————————————————————————————————————————
// 1, 8, 22, 43, 71, 106, 148, 197, 253, 316,
// 386, 463, 547, 638, 736, 841, 953, 1072, 1198, 1331,
// 1471, 1618, 1772, 1933, 2101, 2276, 2458, 2647, 2843,
// 3046, 3256, 3473, 3697, 3928, 4166, 4411, 4663,
// 4922, 5188, 5461, 5741, 6028 ... etc
// n = 10
task->centeredHeptagonalNo(10);
return 0;
}
Output
1 8 22 43 71 106 148 197 253 316
// Include namespace system
using System;
// Csharp program for
// Centered heptagonal number
public class CenteredHeptagonal
{
public void centeredHeptagonalNo(int k)
{
// Print all initial k centered heptagonal number
for (int n = 1; n <= k; ++n)
{
// Formula
// (7n² - 7n + 2)
// ————————————————
// 2
// Calculate nth heptagonal number
int result = (7 * (n * n) - 7 * n + 2) / 2;
// Display Calculated value
Console.Write(" " + result);
}
}
public static void Main(String[] args)
{
CenteredHeptagonal task = new CenteredHeptagonal();
// Centered heptagonal number are
// —————————————————————————————————————————————
// 1, 8, 22, 43, 71, 106, 148, 197, 253, 316,
// 386, 463, 547, 638, 736, 841, 953, 1072, 1198, 1331,
// 1471, 1618, 1772, 1933, 2101, 2276, 2458, 2647, 2843,
// 3046, 3256, 3473, 3697, 3928, 4166, 4411, 4663,
// 4922, 5188, 5461, 5741, 6028 ... etc
// n = 10
task.centeredHeptagonalNo(10);
}
}
Output
1 8 22 43 71 106 148 197 253 316
package main
import "fmt"
// Go program for
// Centered heptagonal number
func centeredHeptagonalNo(k int) {
// Print all initial k centered heptagonal number
for n := 1 ; n <= k ; n++ {
// Formula
// (7n² - 7n + 2)
// ————————————————
// 2
// Calculate nth heptagonal number
var result int = (7 * (n * n) - 7 * n + 2) / 2
// Display Calculated value
fmt.Print(" ", result)
}
}
func main() {
// Centered heptagonal number are
// —————————————————————————————————————————————
// 1, 8, 22, 43, 71, 106, 148, 197, 253, 316,
// 386, 463, 547, 638, 736, 841, 953, 1072, 1198, 1331,
// 1471, 1618, 1772, 1933, 2101, 2276, 2458, 2647, 2843,
// 3046, 3256, 3473, 3697, 3928, 4166, 4411, 4663,
// 4922, 5188, 5461, 5741, 6028 ... etc
// test k = 10
centeredHeptagonalNo(10)
}
Output
1 8 22 43 71 106 148 197 253 316
<?php
// Php program for
// Centered heptagonal number
class CenteredHeptagonal
{
public function centeredHeptagonalNo($k)
{
// Print all initial k centered heptagonal number
for ($n = 1; $n <= $k; ++$n)
{
// Formula
// (7n² - 7n + 2)
// ————————————————
// 2
// Calculate nth heptagonal number
$result = (int)((7 * ($n * $n) - 7 * $n + 2) / 2);
// Display Calculated value
echo(" ".$result);
}
}
}
function main()
{
$task = new CenteredHeptagonal();
// Centered heptagonal number are
// —————————————————————————————————————————————
// 1, 8, 22, 43, 71, 106, 148, 197, 253, 316,
// 386, 463, 547, 638, 736, 841, 953, 1072, 1198, 1331,
// 1471, 1618, 1772, 1933, 2101, 2276, 2458, 2647, 2843,
// 3046, 3256, 3473, 3697, 3928, 4166, 4411, 4663,
// 4922, 5188, 5461, 5741, 6028 ... etc
// n = 10
$task->centeredHeptagonalNo(10);
}
main();
Output
1 8 22 43 71 106 148 197 253 316
// Node JS program for
// Centered heptagonal number
class CenteredHeptagonal
{
centeredHeptagonalNo(k)
{
// Print all initial k centered heptagonal number
for (var n = 1; n <= k; ++n)
{
// Formula
// (7n² - 7n + 2)
// ————————————————
// 2
// Calculate nth heptagonal number
var result = parseInt((7 * (n * n) - 7 * n + 2) / 2);
// Display Calculated value
process.stdout.write(" " + result);
}
}
}
function main()
{
var task = new CenteredHeptagonal();
// Centered heptagonal number are
// —————————————————————————————————————————————
// 1, 8, 22, 43, 71, 106, 148, 197, 253, 316,
// 386, 463, 547, 638, 736, 841, 953, 1072, 1198, 1331,
// 1471, 1618, 1772, 1933, 2101, 2276, 2458, 2647, 2843,
// 3046, 3256, 3473, 3697, 3928, 4166, 4411, 4663,
// 4922, 5188, 5461, 5741, 6028 ... etc
// n = 10
task.centeredHeptagonalNo(10);
}
main();
Output
1 8 22 43 71 106 148 197 253 316
# Python 3 program for
# Centered heptagonal number
class CenteredHeptagonal :
def centeredHeptagonalNo(self, k) :
n = 1
# Print all initial k centered heptagonal number
while (n <= k) :
# Formula
# (7n² - 7n + 2)
# ————————————————
# 2
# Calculate nth heptagonal number
result = int((7 * (n * n) - 7 * n + 2) / 2)
# Display Calculated value
print(" ", result, end = "")
n += 1
def main() :
task = CenteredHeptagonal()
# Centered heptagonal number are
# —————————————————————————————————————————————
# 1, 8, 22, 43, 71, 106, 148, 197, 253, 316,
# 386, 463, 547, 638, 736, 841, 953, 1072, 1198, 1331,
# 1471, 1618, 1772, 1933, 2101, 2276, 2458, 2647, 2843,
# 3046, 3256, 3473, 3697, 3928, 4166, 4411, 4663,
# 4922, 5188, 5461, 5741, 6028 ... etc
# Test k = 10
task.centeredHeptagonalNo(10)
if __name__ == "__main__": main()
Output
1 8 22 43 71 106 148 197 253 316
# Ruby program for
# Centered heptagonal number
class CenteredHeptagonal
def centeredHeptagonalNo(k)
n = 1
# Print all initial k centered heptagonal number
while (n <= k)
# Formula
# (7n² - 7n + 2)
# ————————————————
# 2
# Calculate nth heptagonal number
result = (7 * (n * n) - 7 * n + 2) / 2
# Display Calculated value
print(" ", result)
n += 1
end
end
end
def main()
task = CenteredHeptagonal.new()
# Centered heptagonal number are
# —————————————————————————————————————————————
# 1, 8, 22, 43, 71, 106, 148, 197, 253, 316,
# 386, 463, 547, 638, 736, 841, 953, 1072, 1198, 1331,
# 1471, 1618, 1772, 1933, 2101, 2276, 2458, 2647, 2843,
# 3046, 3256, 3473, 3697, 3928, 4166, 4411, 4663,
# 4922, 5188, 5461, 5741, 6028 ... etc
# Test n = 10
task.centeredHeptagonalNo(10)
end
main()
Output
1 8 22 43 71 106 148 197 253 316
// Scala program for
// Centered heptagonal number
class CenteredHeptagonal()
{
def centeredHeptagonalNo(k: Int): Unit = {
var n: Int = 1;
// Print all initial k centered heptagonal number
while (n <= k)
{
// Formula
// (7n² - 7n + 2)
// ————————————————
// 2
// Calculate nth heptagonal number
var result: Int = (7 * (n * n) - 7 * n + 2) / 2;
// Display Calculated value
print(" " + result);
n += 1;
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: CenteredHeptagonal = new CenteredHeptagonal();
// Centered heptagonal number are
// —————————————————————————————————————————————
// 1, 8, 22, 43, 71, 106, 148, 197, 253, 316,
// 386, 463, 547, 638, 736, 841, 953, 1072, 1198, 1331,
// 1471, 1618, 1772, 1933, 2101, 2276, 2458, 2647, 2843,
// 3046, 3256, 3473, 3697, 3928, 4166, 4411, 4663,
// 4922, 5188, 5461, 5741, 6028 ... etc
// test k = 10
task.centeredHeptagonalNo(10);
}
}
Output
1 8 22 43 71 106 148 197 253 316
// Swift 4 program for
// Centered heptagonal number
class CenteredHeptagonal
{
func centeredHeptagonalNo(_ k: Int)
{
var n: Int = 1;
// Print all initial k centered heptagonal number
while (n <= k)
{
// Formula
// (7n² - 7n + 2)
// ————————————————
// 2
// Calculate nth heptagonal number
let result: Int = (7 * (n * n) - 7 * n + 2) / 2;
// Display Calculated value
print(" ", result, terminator: "");
n += 1;
}
}
}
func main()
{
let task: CenteredHeptagonal = CenteredHeptagonal();
// Centered heptagonal number are
// —————————————————————————————————————————————
// 1, 8, 22, 43, 71, 106, 148, 197, 253, 316,
// 386, 463, 547, 638, 736, 841, 953, 1072, 1198, 1331,
// 1471, 1618, 1772, 1933, 2101, 2276, 2458, 2647, 2843,
// 3046, 3256, 3473, 3697, 3928, 4166, 4411, 4663,
// 4922, 5188, 5461, 5741, 6028 ... etc
// test k = 10
task.centeredHeptagonalNo(10);
}
main();
Output
1 8 22 43 71 106 148 197 253 316
// Kotlin program for
// Centered heptagonal number
class CenteredHeptagonal
{
fun centeredHeptagonalNo(k: Int): Unit
{
var n: Int = 1;
// Print all initial k centered heptagonal number
while (n <= k)
{
// Formula
// (7n² - 7n + 2)
// ————————————————
// 2
// Calculate nth heptagonal number
val result: Int = (7 * (n * n) - 7 * n + 2) / 2;
// Display Calculated value
print(" " + result);
n += 1;
}
}
}
fun main(args: Array < String > ): Unit
{
val task: CenteredHeptagonal = CenteredHeptagonal();
// Centered heptagonal number are
// —————————————————————————————————————————————
// 1, 8, 22, 43, 71, 106, 148, 197, 253, 316,
// 386, 463, 547, 638, 736, 841, 953, 1072, 1198, 1331,
// 1471, 1618, 1772, 1933, 2101, 2276, 2458, 2647, 2843,
// 3046, 3256, 3473, 3697, 3928, 4166, 4411, 4663,
// 4922, 5188, 5461, 5741, 6028 ... etc
// Test k = 10
task.centeredHeptagonalNo(10);
}
Output
1 8 22 43 71 106 148 197 253 316
Resultant Output Explanation
The function centeredHeptagonalNo(5)
will output the first five centered heptagonal numbers: 1, 8,
22, 43, and 71.
Time Complexity
The time complexity of the code is O(k) because the loop runs from 1 to 'k', and for each iteration, a constant amount of computation is performed. As 'k' increases, the time taken will be directly proportional to 'k'.
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment