Posted on by Kalkicode
Code Number

Hexagonal number

Hexagonal numbers are a fascinating mathematical sequence that can be represented in the form of a hexagon. In this article, we will delve into the concept of hexagonal numbers, understand the underlying formula to generate them, and explore the first 'k' hexagonal numbers using a C program. Additionally, we will provide an explanation of the code and analyze its time complexity.

Understanding Hexagonal Numbers

Hexagonal numbers are a subset of triangular numbers and have a unique property of forming regular hexagons when arranged in a specific pattern. The formula to calculate the 'n-th' hexagonal number is given by (2 * n^2 - n). Let's understand this formula with an example:

Example: For n = 1, the formula yields: Hexagonal number = 2 * 1^2 - 1 Hexagonal number = 2 - 1 Hexagonal number = 1

For n = 2, the formula yields: Hexagonal number = 2 * 2^2 - 2 Hexagonal number = 8 - 2 Hexagonal number = 6

Similarly, for n = 3, 4, 5, and so on, the hexagonal numbers would be 15, 28, 45, and 66, respectively.

Pseudocode

To generate the first 'k' hexagonal numbers, we will use a simple loop that calculates the hexagonal number for each 'n' from 1 to 'k' using the formula mentioned above. Let's outline the pseudocode for the hexagonal number function:

function hexagonalNumbers(k)
    for n = 1 to k
        result = 2 * n^2 - n
        print result

Algorithm Explanation

  1. Start the function 'hexagonalNumbers' with a parameter 'k' representing the number of hexagonal numbers to generate.
  2. Initialize a loop from 'n = 1' to 'n = k'.
  3. For each value of 'n', calculate the corresponding hexagonal number using the formula '(2 * n^2 - n)' and store it in the variable 'result.'
  4. Print the value of 'result' to display the hexagonal number.
  5. Repeat steps 3-4 for all 'n' from 1 to 'k' to generate the 'k' hexagonal numbers.

Code Solution

Here given code implementation process.

// C Program for
// Hexagonal number
#include <stdio.h>

void hexagonalNo(int k)
{
	// Print all initial k Hexagonal number
	for (int n = 1; n <= k; ++n)
	{
		// Formula
		// (2n² -n)
      
		// Calculate nth hexagonal number
		int result = (2 *(n *n) - n);
      
		// Display calculated result
		printf("  %d", result);
	}
}
int main()
{
	// Hexagonal number are
	// —————————————————————————————————————————————
	//  1, 6, 15, 28, 45, 66, 91, 120, 153, 190, 231, 
	//  276, 325, 378, 435, 496, 561, 630, 703, 780, 861, 
	//  946, 1035, 1128, 1225, 1326, 1431, 1540, 1653, 1770, 
	//  1891, 2016, 2145, 2278, 2415, 2556, 2701, 2850, 
	//  3003, 3160, 3321, 3486, 3655, 3828 ... etc
  
	// k = 10
	hexagonalNo(10);
	return 0;
}

Output

  1  6  15  28  45  66  91  120  153  190
// Java program for
// Heptagonal pyramidal number
public class HexagonalNumber
{
	public void hexagonalNo(int k)
	{
		// Print all initial k Hexagonal number
		for (int n = 1; n <= k; ++n)
		{
			// Formula
			// (2n² -n)
          
			// Calculate nth hexagonal number
			int result = (2 * (n * n) - n);
          
			// Display calculated result
			System.out.print(" " + result);
		}
	}
	public static void main(String[] args)
	{
		HexagonalNumber task = new HexagonalNumber();
		// Hexagonal number are
		// —————————————————————————————————————————————
		//  1, 6, 15, 28, 45, 66, 91, 120, 153, 190, 231, 
		//  276, 325, 378, 435, 496, 561, 630, 703, 780, 861, 
		//  946, 1035, 1128, 1225, 1326, 1431, 1540, 1653, 1770, 
		//  1891, 2016, 2145, 2278, 2415, 2556, 2701, 2850, 
		//  3003, 3160, 3321, 3486, 3655, 3828 ... etc
      
		// k = 10
		task.hexagonalNo(10);
	}
}

Output

 1 6 15 28 45 66 91 120 153 190
// Include header file
#include <iostream>
using namespace std;
// C++ program for
// Heptagonal pyramidal number

class HexagonalNumber
{
	public: void hexagonalNo(int k)
	{
		// Print all initial k Hexagonal number
		for (int n = 1; n <= k; ++n)
		{
			// Formula
			// (2n² -n)
          
			// Calculate nth hexagonal number
			int result = (2 *(n *n) - n);
          
			// Display calculated result
			cout << " " << result;
		}
	}
};
int main()
{
	HexagonalNumber *task = new HexagonalNumber();
	// Hexagonal number are
	// —————————————————————————————————————————————
	//  1, 6, 15, 28, 45, 66, 91, 120, 153, 190, 231, 
	//  276, 325, 378, 435, 496, 561, 630, 703, 780, 861, 
	//  946, 1035, 1128, 1225, 1326, 1431, 1540, 1653, 1770, 
	//  1891, 2016, 2145, 2278, 2415, 2556, 2701, 2850, 
	//  3003, 3160, 3321, 3486, 3655, 3828 ... etc
  
	// k = 10
	task->hexagonalNo(10);
	return 0;
}

Output

 1 6 15 28 45 66 91 120 153 190
package main
import "fmt"
// Go program for
// Heptagonal pyramidal number

func hexagonalNo(k int) {
	// Print all initial k Hexagonal number
	for n := 1 ; n <= k ; n++ {
		// Formula
		// (2n² -n)

		// Calculate nth hexagonal number
		var result int = (2 * (n * n) - n)
		
		// Display calculated result
		fmt.Print(" ", result)
	}
}
func main() {

	// Hexagonal number are
	// —————————————————————————————————————————————
	//  1, 6, 15, 28, 45, 66, 91, 120, 153, 190, 231, 
	//  276, 325, 378, 435, 496, 561, 630, 703, 780, 861, 
	//  946, 1035, 1128, 1225, 1326, 1431, 1540, 1653, 1770, 
	//  1891, 2016, 2145, 2278, 2415, 2556, 2701, 2850, 
	//  3003, 3160, 3321, 3486, 3655, 3828 ... etc

	// k = 10
	hexagonalNo(10)
}

Output

 1 6 15 28 45 66 91 120 153 190
// Include namespace system
using System;
// Csharp program for
// Heptagonal pyramidal number
public class HexagonalNumber
{
	public void hexagonalNo(int k)
	{
		// Print all initial k Hexagonal number
		for (int n = 1; n <= k; ++n)
		{
			// Formula
			// (2n² -n)
          
			// Calculate nth hexagonal number
			int result = (2 * (n * n) - n);
          
			// Display calculated result
			Console.Write(" " + result);
		}
	}
	public static void Main(String[] args)
	{
		HexagonalNumber task = new HexagonalNumber();
		// Hexagonal number are
		// —————————————————————————————————————————————
		//  1, 6, 15, 28, 45, 66, 91, 120, 153, 190, 231, 
		//  276, 325, 378, 435, 496, 561, 630, 703, 780, 861, 
		//  946, 1035, 1128, 1225, 1326, 1431, 1540, 1653, 1770, 
		//  1891, 2016, 2145, 2278, 2415, 2556, 2701, 2850, 
		//  3003, 3160, 3321, 3486, 3655, 3828 ... etc
      
		// k = 10
		task.hexagonalNo(10);
	}
}

Output

 1 6 15 28 45 66 91 120 153 190
<?php
// Php program for
// Heptagonal pyramidal number
class HexagonalNumber
{
	public	function hexagonalNo($k)
	{
		// Print all initial k Hexagonal number
		for ($n = 1; $n <= $k; ++$n)
		{
			// Formula
			// (2n² -n)
          
			// Calculate nth hexagonal number
			$result = (2 * ($n * $n) - $n);
          
			// Display calculated result
			echo(" ".$result);
		}
	}
}

function main()
{
	$task = new HexagonalNumber();
	// Hexagonal number are
	// —————————————————————————————————————————————
	//  1, 6, 15, 28, 45, 66, 91, 120, 153, 190, 231, 
	//  276, 325, 378, 435, 496, 561, 630, 703, 780, 861, 
	//  946, 1035, 1128, 1225, 1326, 1431, 1540, 1653, 1770, 
	//  1891, 2016, 2145, 2278, 2415, 2556, 2701, 2850, 
	//  3003, 3160, 3321, 3486, 3655, 3828 ... etc
  
	// k = 10
	$task->hexagonalNo(10);
}
main();

Output

 1 6 15 28 45 66 91 120 153 190
// Node JS program for
// Heptagonal pyramidal number
class HexagonalNumber
{
	hexagonalNo(k)
	{
		// Print all initial k Hexagonal number
		for (var n = 1; n <= k; ++n)
		{
			// Formula
			// (2n² -n)
          
			// Calculate nth hexagonal number
			var result = (2 * (n * n) - n);
          
			// Display calculated result
			process.stdout.write(" " + result);
		}
	}
}

function main()
{
	var task = new HexagonalNumber();
	// Hexagonal number are
	// —————————————————————————————————————————————
	//  1, 6, 15, 28, 45, 66, 91, 120, 153, 190, 231, 
	//  276, 325, 378, 435, 496, 561, 630, 703, 780, 861, 
	//  946, 1035, 1128, 1225, 1326, 1431, 1540, 1653, 1770, 
	//  1891, 2016, 2145, 2278, 2415, 2556, 2701, 2850, 
	//  3003, 3160, 3321, 3486, 3655, 3828 ... etc
  
	// k = 10
	task.hexagonalNo(10);
}
main();

Output

 1 6 15 28 45 66 91 120 153 190
#  Python 3 program for
#  Heptagonal pyramidal number
class HexagonalNumber :
	def hexagonalNo(self, k) :
		n = 1
		#  Print all initial k Hexagonal number
		while (n <= k) :
			#  Formula
			#  (2n² -n)
            
			#  Calculate nth hexagonal number
			result = (2 * (n * n) - n)

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

def main() :
	task = HexagonalNumber()
	#  Hexagonal number are
	#  —————————————————————————————————————————————
	#   1, 6, 15, 28, 45, 66, 91, 120, 153, 190, 231, 
	#   276, 325, 378, 435, 496, 561, 630, 703, 780, 861, 
	#   946, 1035, 1128, 1225, 1326, 1431, 1540, 1653, 1770, 
	#   1891, 2016, 2145, 2278, 2415, 2556, 2701, 2850, 
	#   3003, 3160, 3321, 3486, 3655, 3828 ... etc
    
	#  k = 10
	task.hexagonalNo(10)

if __name__ == "__main__": main()

Output

  1  6  15  28  45  66  91  120  153  190
#  Ruby program for
#  Heptagonal pyramidal number
class HexagonalNumber 
	def hexagonalNo(k) 
		n = 1
		#  Print all initial k Hexagonal number
		while (n <= k) 
			#  Formula
			#  (2n² -n)
            
			#  Calculate nth hexagonal number
			result = (2 * (n * n) - n)

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

	end

end

def main() 
	task = HexagonalNumber.new()
	#  Hexagonal number are
	#  —————————————————————————————————————————————
	#   1, 6, 15, 28, 45, 66, 91, 120, 153, 190, 231, 
	#   276, 325, 378, 435, 496, 561, 630, 703, 780, 861, 
	#   946, 1035, 1128, 1225, 1326, 1431, 1540, 1653, 1770, 
	#   1891, 2016, 2145, 2278, 2415, 2556, 2701, 2850, 
	#   3003, 3160, 3321, 3486, 3655, 3828 ... etc
    
	#  k = 10
	task.hexagonalNo(10)
end

main()

Output

 1 6 15 28 45 66 91 120 153 190
// Scala program for
// Heptagonal pyramidal number
class HexagonalNumber()
{
	def hexagonalNo(k: Int): Unit = {
		var n: Int = 1;
		// Print all initial k Hexagonal number
		while (n <= k)
		{
			// Formula
			// (2n² -n)
          
			// Calculate nth hexagonal number
			var result: Int = (2 * (n * n) - n);
          
			// Display calculated result
			print(" " + result);
			n += 1;
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: HexagonalNumber = new HexagonalNumber();
		// Hexagonal number are
		// —————————————————————————————————————————————
		//  1, 6, 15, 28, 45, 66, 91, 120, 153, 190, 231, 
		//  276, 325, 378, 435, 496, 561, 630, 703, 780, 861, 
		//  946, 1035, 1128, 1225, 1326, 1431, 1540, 1653, 1770, 
		//  1891, 2016, 2145, 2278, 2415, 2556, 2701, 2850, 
		//  3003, 3160, 3321, 3486, 3655, 3828 ... etc
  
		// k = 10
		task.hexagonalNo(10);
	}
}

Output

 1 6 15 28 45 66 91 120 153 190
// Swift 4 program for
// Heptagonal pyramidal number
class HexagonalNumber
{
	func hexagonalNo(_ k: Int)
	{
		var n: Int = 1;
		// Print all initial k Hexagonal number
		while (n <= k)
		{
			// Formula
			// (2n² -n)
          
			// Calculate nth hexagonal number
			let result: Int = (2 * (n * n) - n);
          
			// Display calculated result
			print(" ", result, terminator: "");
			n += 1;
		}
	}
}
func main()
{
	let task: HexagonalNumber = HexagonalNumber();
	// Hexagonal number are
	// —————————————————————————————————————————————
	//  1, 6, 15, 28, 45, 66, 91, 120, 153, 190, 231, 
	//  276, 325, 378, 435, 496, 561, 630, 703, 780, 861, 
	//  946, 1035, 1128, 1225, 1326, 1431, 1540, 1653, 1770, 
	//  1891, 2016, 2145, 2278, 2415, 2556, 2701, 2850, 
	//  3003, 3160, 3321, 3486, 3655, 3828 ... etc
  
	// k = 10
	task.hexagonalNo(10);
}
main();

Output

  1  6  15  28  45  66  91  120  153  190
// Kotlin program for
// Heptagonal pyramidal number
class HexagonalNumber
{
	fun hexagonalNo(k: Int): Unit
	{
		var n: Int = 1;
		// Print all initial k Hexagonal number
		while (n <= k)
		{
			// Formula
			// (2n² -n)
          
			// Calculate nth hexagonal number
			val result: Int = (2 * (n * n) - n);
          
			// Display calculated result
			print(" " + result);
			n += 1;
		}
	}
}
fun main(args: Array < String > ): Unit
{
	val task: HexagonalNumber = HexagonalNumber();
	// Hexagonal number are
	// —————————————————————————————————————————————
	//  1, 6, 15, 28, 45, 66, 91, 120, 153, 190, 231, 
	//  276, 325, 378, 435, 496, 561, 630, 703, 780, 861, 
	//  946, 1035, 1128, 1225, 1326, 1431, 1540, 1653, 1770, 
	//  1891, 2016, 2145, 2278, 2415, 2556, 2701, 2850, 
	//  3003, 3160, 3321, 3486, 3655, 3828 ... etc
  
	// k = 10
	task.hexagonalNo(10);
}

Output

 1 6 15 28 45 66 91 120 153 190

C Code Explanation: The given C program follows the pseudocode to generate the first 'k' hexagonal numbers. The main function calls the 'hexagonalNo' function with 'k = 10', and the function, in turn, generates and prints the first 10 hexagonal numbers.

Output Explanation

Upon executing the C program with 'k = 10', the output displays the first 10 hexagonal numbers, which are 1, 6, 15, 28, 45, 66, 91, 120, 153, and 190, respectively.

Time Complexity Analysis

The time complexity of the given code is determined by the loop that iterates 'k' times. Therefore, the time complexity of the code is O(k), where 'k' represents the number of hexagonal numbers to be generated.

Finally, this article discussed the concept of hexagonal numbers, provided an explanation of the C program to generate the first 'k' hexagonal numbers, and analyzed the time complexity of the code. Hexagonal numbers are intriguing mathematical entities that have various applications in different fields, including geometry and number theory. The code presented in this article offers a simple way to calculate these numbers and explore their sequence.

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