Posted on by Kalkicode
Code Number

Centered hexagonal numbers

Centered hexagonal numbers are a sequence of numbers that form a hexagonal shape with a central dot. In this article, we will explore the concept of centered hexagonal numbers, provide a problem statement, explain the algorithm and pseudocode, and analyze the time complexity of the code.

Problem Statement

The problem is to generate the first k centered hexagonal numbers, where k is a positive integer. A centered hexagonal number is calculated using the formula:

3n(n - 1) + 1

where n represents the position of the number in the sequence.

Example

Let's consider an example where k = 10. We need to generate the first 10 centered hexagonal numbers. Using the formula, we can calculate the values:

1, 7, 19, 37, 61, 91, 127, 169, 217, 271

Algorithm and Pseudocode

1. Start the centeredHexagonalNo function with an input parameter k.

2. Initialize a variable named result to 0.

3. Use a for loop to iterate from n = 1 to n = k.

4. Within the loop, calculate the centered hexagonal number using the formula: result = 3n(n - 1) + 1.

5. Display the calculated result.

6. End the function.

7. In the main function:

    a. Call the centeredHexagonalNo function with the desired value of k.

    b. Return 0 to indicate successful program execution.

Pseudocode:

function centeredHexagonalNo(k)
    result = 0
    for n = 1 to k
        result = 3 * n * (n - 1) + 1
        display result
main()
centeredHexagonalNo(10)
return 0

Code Solution

Here given code implementation process.

// C Program for
// Centered hexagonal numbers
#include <stdio.h>

void centeredHexagonalNo(int k)
{
	int result = 0;
  
	// Print all initial k centered hexagonal number
	for (int n = 1; n <= k; ++n)
	{
		// Centered hexagonal 
		// formula =  3n(n - 1) + 1
      
      	// Calculated centered hexagonal 
		result = ((3 *n) *(n - 1) + 1);
      
		// Display calculated result
		printf("  %d", result);
	}
}
int main()
{
	// Centered hexagonal numbers
	// --------------------------------------
	// 1, 7, 19, 37, 61, 91, 127, 169, 217, 271, 331, 
	// 397, 469, 547, 631, 721, 817, 919 .. etc
  
	// Test 
	// k  = 10
	centeredHexagonalNo(10);
	return 0;
}

Output

  1  7  19  37  61  91  127  169  217  271
// Java program for
// Centered hexagonal numbers
public class Hexagonal
{
    public void centeredHexagonalNo(int k)
    {
        int result = 0;
        // Print all initial k centered hexagonal number
        for (int n = 1; n <= k; ++n)
        {
            // Centered hexagonal 
            // formula =  3n(n - 1) + 1

            // Calculated centered hexagonal 
            result = ((3 * n) * (n - 1) + 1);
            
            // Display calculated result
            System.out.print(" " + result );
        }
    }
    public static void main(String[] args)
    {
        Hexagonal task = new Hexagonal();
        // Centered hexagonal numbers
        // --------------------------------------
        // 1, 7, 19, 37, 61, 91, 127, 169, 217, 271, 331, 
        // 397, 469, 547, 631, 721, 817, 919 .. etc
        // Test 
        // k  = 10
        task.centeredHexagonalNo(10);
    }
}

Output

 1 7 19 37 61 91 127 169 217 271
// Include header file
#include <iostream>
using namespace std;
// C++ program for
// Centered hexagonal numbers

class Hexagonal
{
	public: void centeredHexagonalNo(int k)
	{
		int result = 0;
		// Print all initial k centered hexagonal number
		for (int n = 1; n <= k; ++n)
		{
			// Centered hexagonal 
			// formula =  3n(n - 1) + 1
          
			// Calculated centered hexagonal 
			result = ((3 *n) *(n - 1) + 1);
          
			// Display calculated result
			cout << " " << result;
		}
	}
};
int main()
{
	Hexagonal *task = new Hexagonal();
	// Centered hexagonal numbers
	// --------------------------------------
	// 1, 7, 19, 37, 61, 91, 127, 169, 217, 271, 331, 
	// 397, 469, 547, 631, 721, 817, 919 .. etc
	// Test 
	// k  = 10
	task->centeredHexagonalNo(10);
	return 0;
}

Output

 1 7 19 37 61 91 127 169 217 271
// Include namespace system
using System;
// Csharp program for
// Centered hexagonal numbers
public class Hexagonal
{
	public void centeredHexagonalNo(int k)
	{
		int result = 0;
		// Print all initial k centered hexagonal number
		for (int n = 1; n <= k; ++n)
		{
			// Centered hexagonal 
			// formula =  3n(n - 1) + 1
          
			// Calculated centered hexagonal 
			result = ((3 * n) * (n - 1) + 1);
          
			// Display calculated result
			Console.Write(" " + result);
		}
	}
	public static void Main(String[] args)
	{
		Hexagonal task = new Hexagonal();
		// Centered hexagonal numbers
		// --------------------------------------
		// 1, 7, 19, 37, 61, 91, 127, 169, 217, 271, 331, 
		// 397, 469, 547, 631, 721, 817, 919 .. etc
		// Test 
		// k  = 10
		task.centeredHexagonalNo(10);
	}
}

Output

 1 7 19 37 61 91 127 169 217 271
package main
import "fmt"
// Go program for
// Centered hexagonal numbers

func centeredHexagonalNo(k int) {
	var result int = 0
	// Print all initial k centered hexagonal number
	for n := 1 ; n <= k ; n++ {
		// Centered hexagonal 
		// formula =  3n(n - 1) + 1

		// Calculated centered hexagonal 
		result = ((3 * n) * (n - 1) + 1)
		
		// Display calculated result
		fmt.Print(" ", result)
	}
}
func main() {
	// Centered hexagonal numbers
	// --------------------------------------
	// 1, 7, 19, 37, 61, 91, 127, 169, 217, 271, 331, 
	// 397, 469, 547, 631, 721, 817, 919 .. etc

	// Test 
	// k  = 10
	centeredHexagonalNo(10)
}

Output

 1 7 19 37 61 91 127 169 217 271
<?php
// Php program for
// Centered hexagonal numbers
class Hexagonal
{
	public	function centeredHexagonalNo($k)
	{
		$result = 0;
		// Print all initial k centered hexagonal number
		for ($n = 1; $n <= $k; ++$n)
		{
			// Centered hexagonal 
			// formula =  3n(n - 1) + 1
          
			// Calculated centered hexagonal 
			$result = ((3 * $n) * ($n - 1) + 1);
          
			// Display calculated result
			echo(" ".$result);
		}
	}
}

function main()
{
	$task = new Hexagonal();
	// Centered hexagonal numbers
	// --------------------------------------
	// 1, 7, 19, 37, 61, 91, 127, 169, 217, 271, 331, 
	// 397, 469, 547, 631, 721, 817, 919 .. etc
	// Test 
	// k  = 10
	$task->centeredHexagonalNo(10);
}
main();

Output

 1 7 19 37 61 91 127 169 217 271
// Node JS program for
// Centered hexagonal numbers
class Hexagonal
{
	centeredHexagonalNo(k)
	{
		var result = 0;
		// Print all initial k centered hexagonal number
		for (var n = 1; n <= k; ++n)
		{
			// Centered hexagonal 
			// formula =  3n(n - 1) + 1
          
			// Calculated centered hexagonal 
			result = ((3 * n) * (n - 1) + 1);
          
			// Display calculated result
			process.stdout.write(" " + result);
		}
	}
}

function main()
{
	var task = new Hexagonal();
	// Centered hexagonal numbers
	// --------------------------------------
	// 1, 7, 19, 37, 61, 91, 127, 169, 217, 271, 331, 
	// 397, 469, 547, 631, 721, 817, 919 .. etc
	// Test 
	// k  = 10
	task.centeredHexagonalNo(10);
}
main();

Output

 1 7 19 37 61 91 127 169 217 271
#  Python 3 program for
#  Centered hexagonal numbers
class Hexagonal :
	def centeredHexagonalNo(self, k) :
		result = 0
		n = 1
		#  Print all initial k centered hexagonal number
		while (n <= k) :
			#  Centered hexagonal 
			#  formula =  3n(n - 1) + 1
            
			#  Calculated centered hexagonal 
			result = ((3 * n) * (n - 1) + 1)

			#  Display calculated result
			print(" ", result, end = "")
			n += 1
		
	

def main() :
	task = Hexagonal()
	#  Centered hexagonal numbers
	#  --------------------------------------
	#  1, 7, 19, 37, 61, 91, 127, 169, 217, 271, 331, 
	#  397, 469, 547, 631, 721, 817, 919 .. etc
    
	#  Test 
	#  k  = 10
	task.centeredHexagonalNo(10)

if __name__ == "__main__": main()

Output

  1  7  19  37  61  91  127  169  217  271
#  Ruby program for
#  Centered hexagonal numbers
class Hexagonal 
	def centeredHexagonalNo(k) 
		result = 0
		n = 1
		#  Print all initial k centered hexagonal number
		while (n <= k) 
			#  Centered hexagonal 
			#  formula =  3n(n - 1) + 1
            
			#  Calculated centered hexagonal 
			result = ((3 * n) * (n - 1) + 1)

			#  Display calculated result
			print(" ", result)
			n += 1
		end

	end

end

def main() 
	task = Hexagonal.new()
	#  Centered hexagonal numbers
	#  --------------------------------------
	#  1, 7, 19, 37, 61, 91, 127, 169, 217, 271, 331, 
	#  397, 469, 547, 631, 721, 817, 919 .. etc
    
	#  Test 
	#  k  = 10
	task.centeredHexagonalNo(10)
end

main()

Output

 1 7 19 37 61 91 127 169 217 271
// Scala program for
// Centered hexagonal numbers
class Hexagonal()
{
	def centeredHexagonalNo(k: Int): Unit = {
		var result: Int = 0;
		var n: Int = 1;
		// Print all initial k centered hexagonal number
		while (n <= k)
		{
			// Centered hexagonal 
			// formula =  3n(n - 1) + 1
          
			// Calculated centered hexagonal 
			result = ((3 * n) * (n - 1) + 1);
          
			// Display calculated result
			print(" " + result);
			n += 1;
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Hexagonal = new Hexagonal();
		// Centered hexagonal numbers
		// --------------------------------------
		// 1, 7, 19, 37, 61, 91, 127, 169, 217, 271, 331, 
		// 397, 469, 547, 631, 721, 817, 919 .. etc
  
		// Test 
		// k  = 10
		task.centeredHexagonalNo(10);
	}
}

Output

 1 7 19 37 61 91 127 169 217 271
// Swift 4 program for
// Centered hexagonal numbers
class Hexagonal
{
	func centeredHexagonalNo(_ k: Int)
	{
		var result: Int = 0;
		var n: Int = 1;
		// Print all initial k centered hexagonal number
		while (n <= k)
		{
			// Centered hexagonal 
			// formula =  3n(n - 1) + 1
          
			// Calculated centered hexagonal 
			result = ((3 * n) * (n - 1) + 1);
          
			// Display calculated result
			print(" ", result, terminator: "");
			n += 1;
		}
	}
}
func main()
{
	let task: Hexagonal = Hexagonal();
	// Centered hexagonal numbers
	// --------------------------------------
	// 1, 7, 19, 37, 61, 91, 127, 169, 217, 271, 331, 
	// 397, 469, 547, 631, 721, 817, 919 .. etc
	// Test 
	// k  = 10
	task.centeredHexagonalNo(10);
}
main();

Output

  1  7  19  37  61  91  127  169  217  271
// Kotlin program for
// Centered hexagonal numbers
class Hexagonal
{
	fun centeredHexagonalNo(k: Int): Unit
	{
		var result: Int;
		var n: Int = 1;
		// Print all initial k centered hexagonal number
		while (n <= k)
		{
			// Centered hexagonal 
			// formula =  3n(n - 1) + 1
          
			// Calculated centered hexagonal 
			result = ((3 * n) * (n - 1) + 1);
          
			// Display calculated result
			print(" " + result);
			n += 1;
		}
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Hexagonal = Hexagonal();
	// Centered hexagonal numbers
	// --------------------------------------
	// 1, 7, 19, 37, 61, 91, 127, 169, 217, 271, 331, 
	// 397, 469, 547, 631, 721, 817, 919 .. etc
  
	// Test 
	// k  = 10
	task.centeredHexagonalNo(10);
}

Output

 1 7 19 37 61 91 127 169 217 271

Resultant Output Explanation

The code generates the first 10 centered hexagonal numbers. The output shows the sequence:

1, 7, 19, 37, 61, 91, 127, 169, 217, 271

Each number in the sequence is calculated using the centered hexagonal number formula. The code iterates through n = 1 to n = k and displays the calculated result for each n value.

Time Complexity Analysis

The time complexity of the given code is O(k) because it uses a single loop that iterates from n = 1 to n = k. In each iteration, the formula is evaluated once, which takes constant time.

As the value of k increases, the number of iterations increases linearly, resulting in a linear time complexity.

Conclusion

In this article, we explored the concept of centered hexagonal numbers. We discussed the problem statement, provided an example, explained the algorithm and pseudocode, and analy

Comment

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