Posted on by Kalkicode
Code Number

# Hexagonal pyramidal number

A hexagonal pyramidal number is a figurate number that represents the number of balls required to form a pyramid with a hexagonal base. Each layer of the pyramid contains hexagonal arrangements of balls, starting from one ball at the top and increasing by one on each subsequent layer. The formula to calculate the nth hexagonal pyramidal number is given as:

```n(n+1)(4n-1)
———————
6```

## Problem Statement

We want to write a C program to calculate and print the first k hexagonal pyramidal numbers.

## Explanation with an Example

Let's take k=5 as an example to understand how to calculate the first five hexagonal pyramidal numbers.

1. For n=1: Hexagonal pyramidal number = (1 * (1 + 1) * ((4 * 1) - 1)) / 6 = 1 * 2 * 3 / 6 = 1

2. For n=2: Hexagonal pyramidal number = (2 * (2 + 1) * ((4 * 2) - 1)) / 6 = 2 * 3 * 7 / 6 = 7

3. For n=3: Hexagonal pyramidal number = (3 * (3 + 1) * ((4 * 3) - 1)) / 6 = 3 * 4 * 11 / 6 = 22

4. For n=4: Hexagonal pyramidal number = (4 * (4 + 1) * ((4 * 4) - 1)) / 6 = 4 * 5 * 15 / 6 = 50

5. For n=5: Hexagonal pyramidal number = (5 * (5 + 1) * ((4 * 5) - 1)) / 6 = 5 * 6 * 19 / 6 = 95

## Pseudocode

``````function hexagonalPyramidalNo(k):
for n from 1 to k:
result = (n * (n + 1) * ((4 * n) - 1)) / 6
print result
``````

## Algorithm Explanation

1. Start the function `hexagonalPyramidalNo` which takes a single integer parameter `k`.
2. For each value of `n` from 1 to `k` (inclusive), do the following: a. Calculate the hexagonal pyramidal number using the formula (n * (n + 1) * ((4 * n) - 1)) / 6. b. Print the calculated result.
3. End the function.

## Code Solution

Here given code implementation process.

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

void hexagonalPyramidalNo(int k)
{
// Print all initial k hexagonal pyramidal number
for (int n = 1; n <= k; ++n)
{
// Formula
//  n(n+1)(4n-1)
// ——————————————
//       6

// Calculate nth hexagonal pyramidal number
int result = (n * (n + 1) * ((4 * n) - 1)) / 6;

// Display calculated result
printf("  %d", result);
}
}
int main()
{
//  Hexagonal pyramidal number are
// —————————————————————————————————————————————
//  1, 7, 22, 50, 95, 161, 252, 372, 525, 715,
//  946, 1222, 1547, 1925, 2360, 2856, 3417, 4047,
//  4750, 5530, 6391, 7337, 8372, 9500, 10725,
//  12051, 13482, 15022, 16675, 18445, 20336,
//  22352, 24497, 26775, 29190, 31746, 34447, 37297 ... etc

// k = 10
hexagonalPyramidalNo(10);
return 0;
}``````

#### Output

``  1  7  22  50  95  161  252  372  525  715``
``````// Java program for
// Heptagonal pyramidal number
public class HexagonalNumber
{
public void hexagonalPyramidalNo(int k)
{
// Print all initial k hexagonal pyramidal number
for (int n = 1; n <= k; ++n)
{
// Formula
//  n(n+1)(4n-1)
// ——————————————
//       6

// Calculate nth hexagonal pyramidal number
int result = (n * (n + 1) * ((4 * n) - 1)) / 6;

// Display calculated result
System.out.print(" " + result);
}
}
public static void main(String[] args)
{
//  Hexagonal pyramidal number are
// —————————————————————————————————————————————
//  1, 7, 22, 50, 95, 161, 252, 372, 525, 715,
//  946, 1222, 1547, 1925, 2360, 2856, 3417, 4047,
//  4750, 5530, 6391, 7337, 8372, 9500, 10725,
//  12051, 13482, 15022, 16675, 18445, 20336,
//  22352, 24497, 26775, 29190, 31746, 34447,
//  37297 ... etc

// k = 10
}
}``````

#### Output

`` 1 7 22 50 95 161 252 372 525 715``
``````// Include header file
#include <iostream>
using namespace std;
// C++ program for
// Heptagonal pyramidal number
class HexagonalNumber
{
public: void hexagonalPyramidalNo(int k)
{
// Print all initial k hexagonal pyramidal number
for (int n = 1; n <= k; ++n)
{
// Formula
//  n(n+1)(4n-1)
// ——————————————
//       6

// Calculate nth hexagonal pyramidal number
int result = (n * (n + 1) * ((4 * n) - 1)) / 6;

// Display calculated result
cout << " " << result;
}
}
};
int main()
{
//  Hexagonal pyramidal number are
// —————————————————————————————————————————————
//  1, 7, 22, 50, 95, 161, 252, 372, 525, 715,
//  946, 1222, 1547, 1925, 2360, 2856, 3417, 4047,
//  4750, 5530, 6391, 7337, 8372, 9500, 10725,
//  12051, 13482, 15022, 16675, 18445, 20336,
//  22352, 24497, 26775, 29190, 31746, 34447,
//  37297 ... etc

// k = 10
return 0;
}``````

#### Output

`` 1 7 22 50 95 161 252 372 525 715``
``````// Include namespace system
using System;
// Csharp program for
// Heptagonal pyramidal number
public class HexagonalNumber
{
public void hexagonalPyramidalNo(int k)
{
// Print all initial k hexagonal pyramidal number
for (int n = 1; n <= k; ++n)
{
// Formula
//  n(n+1)(4n-1)
// ——————————————
//       6

// Calculate nth hexagonal pyramidal number
int result = (n * (n + 1) * ((4 * n) - 1)) / 6;

// Display calculated result
Console.Write(" " + result);
}
}
public static void Main(String[] args)
{
//  Hexagonal pyramidal number are
// —————————————————————————————————————————————
//  1, 7, 22, 50, 95, 161, 252, 372, 525, 715,
//  946, 1222, 1547, 1925, 2360, 2856, 3417, 4047,
//  4750, 5530, 6391, 7337, 8372, 9500, 10725,
//  12051, 13482, 15022, 16675, 18445, 20336,
//  22352, 24497, 26775, 29190, 31746, 34447,
//  37297 ... etc

// k = 10
}
}``````

#### Output

`` 1 7 22 50 95 161 252 372 525 715``
``````package main
import "fmt"
// Go program for
// Heptagonal pyramidal number

func hexagonalPyramidalNo(k int) {
// Print all initial k hexagonal pyramidal number
for n := 1 ; n <= k ; n++ {
// Formula
//  n(n+1)(4n-1)
// ——————————————
//       6

// Calculate nth hexagonal pyramidal number
var result int = (n * (n + 1) * ((4 * n) - 1)) / 6

// Display calculated result
fmt.Print(" ", result)
}
}
func main() {

//  Hexagonal pyramidal number are
// —————————————————————————————————————————————
//  1, 7, 22, 50, 95, 161, 252, 372, 525, 715,
//  946, 1222, 1547, 1925, 2360, 2856, 3417, 4047,
//  4750, 5530, 6391, 7337, 8372, 9500, 10725,
//  12051, 13482, 15022, 16675, 18445, 20336,
//  22352, 24497, 26775, 29190, 31746, 34447,
//  37297 ... etc

// k = 10
hexagonalPyramidalNo(10)
}``````

#### Output

`` 1 7 22 50 95 161 252 372 525 715``
``````<?php
// Php program for
// Heptagonal pyramidal number
class HexagonalNumber
{
public	function hexagonalPyramidalNo(\$k)
{
// Print all initial k hexagonal pyramidal number
for (\$n = 1; \$n <= \$k; ++\$n)
{
// Formula
//  n(n+1)(4n-1)
// ——————————————
//       6

// Calculate nth hexagonal pyramidal number
\$result = (int)((\$n * (\$n + 1) * ((4 * \$n) - 1)) / 6);

// Display calculated result
echo(" ".\$result);
}
}
}

function main()
{
//  Hexagonal pyramidal number are
// —————————————————————————————————————————————
//  1, 7, 22, 50, 95, 161, 252, 372, 525, 715,
//  946, 1222, 1547, 1925, 2360, 2856, 3417, 4047,
//  4750, 5530, 6391, 7337, 8372, 9500, 10725,
//  12051, 13482, 15022, 16675, 18445, 20336,
//  22352, 24497, 26775, 29190, 31746, 34447,
//  37297 ... etc

// k = 10
}
main();``````

#### Output

`` 1 7 22 50 95 161 252 372 525 715``
``````// Node JS program for
// Heptagonal pyramidal number
class HexagonalNumber
{
hexagonalPyramidalNo(k)
{
// Print all initial k hexagonal pyramidal number
for (var n = 1; n <= k; ++n)
{
// Formula
//  n(n+1)(4n-1)
// ——————————————
//       6

// Calculate nth hexagonal pyramidal number
var result = parseInt((n * (n + 1) * ((4 * n) - 1)) / 6);

// Display calculated result
process.stdout.write(" " + result);
}
}
}

function main()
{
//  Hexagonal pyramidal number are
// —————————————————————————————————————————————
//  1, 7, 22, 50, 95, 161, 252, 372, 525, 715,
//  946, 1222, 1547, 1925, 2360, 2856, 3417, 4047,
//  4750, 5530, 6391, 7337, 8372, 9500, 10725,
//  12051, 13482, 15022, 16675, 18445, 20336,
//  22352, 24497, 26775, 29190, 31746, 34447,
//  37297 ... etc

// k = 10
}
main();``````

#### Output

`` 1 7 22 50 95 161 252 372 525 715``
``````#  Python 3 program for
#  Heptagonal pyramidal number
class HexagonalNumber :
def hexagonalPyramidalNo(self, k) :
n = 1
#  Print all initial k hexagonal pyramidal number
while (n <= k) :
#  Formula
#   n(n+1)(4n-1)
#  ——————————————
#        6

#  Calculate nth hexagonal pyramidal number
result = int((n * (n + 1) * ((4 * n) - 1)) / 6)

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

def main() :
#   Hexagonal pyramidal number are
#  —————————————————————————————————————————————
#   1, 7, 22, 50, 95, 161, 252, 372, 525, 715,
#   946, 1222, 1547, 1925, 2360, 2856, 3417, 4047,
#   4750, 5530, 6391, 7337, 8372, 9500, 10725,
#   12051, 13482, 15022, 16675, 18445, 20336,
#   22352, 24497, 26775, 29190, 31746, 34447,
#   37297 ... etc

#  k = 10

if __name__ == "__main__": main()``````

#### Output

``  1  7  22  50  95  161  252  372  525  715``
``````#  Ruby program for
#  Heptagonal pyramidal number
class HexagonalNumber
def hexagonalPyramidalNo(k)
n = 1
#  Print all initial k hexagonal pyramidal number
while (n <= k)
#  Formula
#   n(n+1)(4n-1)
#  ——————————————
#        6

#  Calculate nth hexagonal pyramidal number
result = (n * (n + 1) * ((4 * n) - 1)) / 6

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

end

end

def main()
#   Hexagonal pyramidal number are
#  —————————————————————————————————————————————
#   1, 7, 22, 50, 95, 161, 252, 372, 525, 715,
#   946, 1222, 1547, 1925, 2360, 2856, 3417, 4047,
#   4750, 5530, 6391, 7337, 8372, 9500, 10725,
#   12051, 13482, 15022, 16675, 18445, 20336,
#   22352, 24497, 26775, 29190, 31746, 34447,
#   37297 ... etc

#  k = 10
end

main()``````

#### Output

`` 1 7 22 50 95 161 252 372 525 715``
``````// Scala program for
// Heptagonal pyramidal number
class HexagonalNumber()
{
def hexagonalPyramidalNo(k: Int): Unit = {
var n: Int = 1;
// Print all initial k hexagonal pyramidal number
while (n <= k)
{
// Formula
//  n(n+1)(4n-1)
// ——————————————
//       6

// Calculate nth hexagonal pyramidal number
var result: Int = (n * (n + 1) * ((4 * n) - 1)) / 6;

// Display calculated result
print(" " + result);
n += 1;
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: HexagonalNumber = new HexagonalNumber();
//  Hexagonal pyramidal number are
// —————————————————————————————————————————————
//  1, 7, 22, 50, 95, 161, 252, 372, 525, 715,
//  946, 1222, 1547, 1925, 2360, 2856, 3417, 4047,
//  4750, 5530, 6391, 7337, 8372, 9500, 10725,
//  12051, 13482, 15022, 16675, 18445, 20336,
//  22352, 24497, 26775, 29190, 31746, 34447,
//  37297 ... etc

// k = 10
}
}``````

#### Output

`` 1 7 22 50 95 161 252 372 525 715``
``````// Swift 4 program for
// Heptagonal pyramidal number
class HexagonalNumber
{
func hexagonalPyramidalNo(_ k: Int)
{
var n: Int = 1;
// Print all initial k hexagonal pyramidal number
while (n <= k)
{
// Formula
//  n(n+1)(4n-1)
// ——————————————
//       6

// Calculate nth hexagonal pyramidal number
let result: Int = (n * (n + 1) * ((4 * n) - 1)) / 6;

// Display calculated result
print(" ", result, terminator: "");
n += 1;
}
}
}
func main()
{
//  Hexagonal pyramidal number are
// —————————————————————————————————————————————
//  1, 7, 22, 50, 95, 161, 252, 372, 525, 715,
//  946, 1222, 1547, 1925, 2360, 2856, 3417, 4047,
//  4750, 5530, 6391, 7337, 8372, 9500, 10725,
//  12051, 13482, 15022, 16675, 18445, 20336,
//  22352, 24497, 26775, 29190, 31746, 34447,
//  37297 ... etc

// k = 10
}
main();``````

#### Output

``  1  7  22  50  95  161  252  372  525  715``
``````// Kotlin program for
// Heptagonal pyramidal number
class HexagonalNumber
{
fun hexagonalPyramidalNo(k: Int): Unit
{
var n: Int = 1;
// Print all initial k hexagonal pyramidal number
while (n <= k)
{
// Formula
//  n(n+1)(4n-1)
// ——————————————
//       6

// Calculate nth hexagonal pyramidal number
val result: Int = (n * (n + 1) * ((4 * n) - 1)) / 6;

// Display calculated result
print(" " + result);
n += 1;
}
}
}
fun main(args: Array < String > ): Unit
{
//  Hexagonal pyramidal number are
// —————————————————————————————————————————————
//  1, 7, 22, 50, 95, 161, 252, 372, 525, 715,
//  946, 1222, 1547, 1925, 2360, 2856, 3417, 4047,
//  4750, 5530, 6391, 7337, 8372, 9500, 10725,
//  12051, 13482, 15022, 16675, 18445, 20336,
//  22352, 24497, 26775, 29190, 31746, 34447,
//  37297 ... etc
// k = 10
}``````

#### Output

`` 1 7 22 50 95 161 252 372 525 715``

## Resultant Output Explanation

For `k = 10`, the function `hexagonalPyramidalNo` will calculate and print the first 10 hexagonal pyramidal numbers:

1 7 22 50 95 161 252 372 525 715

## Time Complexity

The time complexity of the provided code is O(k) because the function `hexagonalPyramidalNo` iterates from `n=1` to `n=k` and performs constant-time calculations in each iteration. As the value of `k` increases, the number of iterations increases linearly. Hence, the time complexity is linear with respect to the input value `k`.

## 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.

Categories
Relative Post