Skip to main content

Heptagonal pyramidal number

In mathematics, a heptagonal pyramidal number is a special type of figurate number that represents the number of balls needed to construct a pyramid with a heptagonal base. The heptagonal pyramidal numbers are generated using a specific formula based on the value of 'n', where 'n' represents the nth term in the sequence of heptagonal pyramidal numbers. In this article, we will explore the concept of heptagonal pyramidal numbers, understand the formula to generate them, and implement a program to print the initial 'k' heptagonal pyramidal numbers.

Understanding Heptagonal Pyramidal Numbers

Heptagonal pyramidal numbers are a subset of figurate numbers. A figurate number is a polygonal number representing the number of dots or balls required to form a regular polygon. A heptagonal pyramidal number can be visualized as a pyramid built using heptagonal layers of balls.

Let's consider the first few heptagonal pyramidal numbers for a better understanding: 1st heptagonal pyramidal number = 1 (a single ball forms the base pyramid) 2nd heptagonal pyramidal number = 8 (a pyramid with 7 balls in the base layer and 1 ball on top) 3rd heptagonal pyramidal number = 26 (a pyramid with 7 balls in the base layer, 6 balls in the second layer, and 1 ball on top)

Formula for Heptagonal Pyramidal Numbers

The formula to calculate the nth heptagonal pyramidal number is as follows: Heptagonal_pyramidal(n) = (n * (n + 1) * ((5 * n) - 2)) / 6

Pseudocode for Printing Heptagonal Pyramidal Numbers: To print the first 'k' heptagonal pyramidal numbers, we need to iterate from n = 0 to n = k-1 and calculate the corresponding value using the formula mentioned above. Here's the pseudocode for the same:

function heptagonalPyramidalNumbers(k):
    for n from 0 to k-1:
        result = (n * (n + 1) * ((5 * n) - 2)) / 6
        print result

Algorithm Explanation

  1. Start the function heptagonalPyramidalNumbers(k).
  2. Loop through n from 0 to k-1 using a for loop.
  3. Calculate the nth heptagonal pyramidal number using the formula (n * (n + 1) * ((5 * n) - 2)) / 6 and store it in the variable 'result'.
  4. Print the value of 'result' on the screen.
  5. Repeat steps 3-4 for each value of n in the loop to get all the initial k heptagonal pyramidal numbers.
  6. End the function.

Code Solution

Here given code implementation process.

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

void heptagonalPyramidalNo(int k)
{
	// Print all initial k Heptagonal pyramidal number
	for (int n = 0; n < k; ++n)
	{
		// Formula
		//  n (n + 1) (5n - 2) 
		//  ———————————————————
		//         6
      
		// Calculate nth Heptagonal pyramidal number
		int result = (n * (n + 1) * ((5 * n) - 2)) / 6;
      
		// Display calculated result
		printf("  %d", result);
	}
}
int main()
{
	// Heptagonal pyramidal number are
	// —————————————————————————————————————————————
	//  0, 1, 8, 26, 60, 115, 196, 308, 456, 645, 880, 
	// 1166, 1508, 1911, 2380, 2920, 3536, 4233, 5016, 
	// 5890, 6860, 7931, 9108, 10396, 11800, 13325, 14976, 
	// 16758, 18676, 20735, 22940, 25296, 27808, 30481, 
	// 33320, 36330, 39516, 42883 ... etc
  
	// Test k = 10
	heptagonalPyramidalNo(10);
	return 0;
}

Output

  0  1  8  26  60  115  196  308  456  645
// Java program for
// Heptagonal pyramidal number
public class HeptagonalPyramidalNumber
{
	public void heptagonalPyramidalNo(int k)
	{
		// Print all initial k Heptagonal pyramidal number
		for (int n = 0; n < k; ++n)
		{
			// Formula
			//  n (n + 1) (5n - 2) 
			//  ———————————————————
			//         6
			// Calculate nth Heptagonal pyramidal number
			int result = (n * (n + 1) * ((5 * n) - 2)) / 6;
			// Display calculated result
			System.out.print(" " + result);
		}
	}
	public static void main(String[] args)
	{
		HeptagonalPyramidalNumber task = new HeptagonalPyramidalNumber();
		// Heptagonal pyramidal number are
		// —————————————————————————————————————————————
		//  0, 1, 8, 26, 60, 115, 196, 308, 456, 645, 880, 
		// 1166, 1508, 1911, 2380, 2920, 3536, 4233, 5016, 
		// 5890, 6860, 7931, 9108, 10396, 11800, 13325, 14976, 
		// 16758, 18676, 20735, 22940, 25296, 27808, 30481, 
		// 33320, 36330, 39516, 42883 ... etc
		// Test k = 10
		task.heptagonalPyramidalNo(10);
	}
}

Output

 0 1 8 26 60 115 196 308 456 645
// Include header file
#include <iostream>
using namespace std;

// C++ program for
// Heptagonal pyramidal number

class HeptagonalPyramidalNumber
{
	public: void heptagonalPyramidalNo(int k)
	{
		// Print all initial k Heptagonal pyramidal number
		for (int n = 0; n < k; ++n)
		{
			// Formula
			//  n (n + 1) (5n - 2) 
			//  ———————————————————
			//         6
          
			// Calculate nth Heptagonal pyramidal number
			int result = (n *(n + 1) *((5 *n) - 2)) / 6;
          
			// Display calculated result
			cout << " " << result;
		}
	}
};
int main()
{
	HeptagonalPyramidalNumber *task = new HeptagonalPyramidalNumber();
	// Heptagonal pyramidal number are
	// —————————————————————————————————————————————
	//  0, 1, 8, 26, 60, 115, 196, 308, 456, 645, 880, 
	// 1166, 1508, 1911, 2380, 2920, 3536, 4233, 5016, 
	// 5890, 6860, 7931, 9108, 10396, 11800, 13325, 14976, 
	// 16758, 18676, 20735, 22940, 25296, 27808, 30481, 
	// 33320, 36330, 39516, 42883 ... etc
	// Test k = 10
	task->heptagonalPyramidalNo(10);
	return 0;
}

Output

 0 1 8 26 60 115 196 308 456 645
package main
import "fmt"
// Go program for
// Heptagonal pyramidal number

func heptagonalPyramidalNo(k int) {
	// Print all initial k heptagonal pyramidal number
	for n := 0 ; n < k ; n++ {
		// Formula
		//  n (n + 1) (5n - 2) 
		//  ———————————————————
		//         6
      
		// Calculate nth heptagonal pyramidal number
		var result int = (n * (n + 1) * ((5 * n) - 2)) / 6
      
		// Display calculated result
		fmt.Print(" ", result)
	}
}
func main() {

	// Heptagonal pyramidal number are
	// —————————————————————————————————————————————
	//  0, 1, 8, 26, 60, 115, 196, 308, 456, 645, 880, 
	// 1166, 1508, 1911, 2380, 2920, 3536, 4233, 5016, 
	// 5890, 6860, 7931, 9108, 10396, 11800, 13325, 14976, 
	// 16758, 18676, 20735, 22940, 25296, 27808, 30481, 
	// 33320, 36330, 39516, 42883 ... etc
  
	// Test k = 10
	heptagonalPyramidalNo(10)
}

Output

 0 1 8 26 60 115 196 308 456 645
// Include namespace system
using System;
// Csharp program for
// Heptagonal pyramidal number
public class HeptagonalPyramidalNumber
{
	public void heptagonalPyramidalNo(int k)
	{
		// Print all initial k Heptagonal pyramidal number
		for (int n = 0; n < k; ++n)
		{
			// Formula
			//  n (n + 1) (5n - 2) 
			//  ———————————————————
			//         6
          
			// Calculate nth heptagonal pyramidal number
			int result = (n * (n + 1) * ((5 * n) - 2)) / 6;
          
			// Display calculated result
			Console.Write(" " + result);
		}
	}
	public static void Main(String[] args)
	{
		HeptagonalPyramidalNumber task = new HeptagonalPyramidalNumber();
		// Heptagonal pyramidal number are
		// —————————————————————————————————————————————
		//  0, 1, 8, 26, 60, 115, 196, 308, 456, 645, 880, 
		// 1166, 1508, 1911, 2380, 2920, 3536, 4233, 5016, 
		// 5890, 6860, 7931, 9108, 10396, 11800, 13325, 14976, 
		// 16758, 18676, 20735, 22940, 25296, 27808, 30481, 
		// 33320, 36330, 39516, 42883 ... etc
      
		// Test k = 10
		task.heptagonalPyramidalNo(10);
	}
}

Output

 0 1 8 26 60 115 196 308 456 645
<?php
// Php program for
// Heptagonal pyramidal number
class HeptagonalPyramidalNumber
{
	public	function heptagonalPyramidalNo($k)
	{
		// Print all initial k Heptagonal pyramidal number
		for ($n = 0; $n < $k; ++$n)
		{
			// Formula
			//  n (n + 1) (5n - 2) 
			//  ———————————————————
			//         6
          
			// Calculate nth Heptagonal pyramidal number
			$result = (int)(($n * ($n + 1) * ((5 * $n) - 2)) / 6);
          
			// Display calculated result
			echo(" ".$result);
		}
	}
}

function main()
{
	$task = new HeptagonalPyramidalNumber();
	// Heptagonal pyramidal number are
	// —————————————————————————————————————————————
	//  0, 1, 8, 26, 60, 115, 196, 308, 456, 645, 880, 
	// 1166, 1508, 1911, 2380, 2920, 3536, 4233, 5016, 
	// 5890, 6860, 7931, 9108, 10396, 11800, 13325, 14976, 
	// 16758, 18676, 20735, 22940, 25296, 27808, 30481, 
	// 33320, 36330, 39516, 42883 ... etc
  
	// Test k = 10
	$task->heptagonalPyramidalNo(10);
}
main();

Output

 0 1 8 26 60 115 196 308 456 645
// Node JS program for
// Heptagonal pyramidal number
class HeptagonalPyramidalNumber
{
	heptagonalPyramidalNo(k)
	{
		// Print all initial k heptagonal pyramidal number
		for (var n = 0; n < k; ++n)
		{
			// Formula
			//  n (n + 1) (5n - 2) 
			//  ———————————————————
			//         6
          
			// Calculate nth heptagonal pyramidal number
			var result = parseInt((n * (n + 1) * ((5 * n) - 2)) / 6);
          
			// Display calculated result
			process.stdout.write(" " + result);
		}
	}
}

function main()
{
	var task = new HeptagonalPyramidalNumber();
	// Heptagonal pyramidal number are
	// —————————————————————————————————————————————
	//  0, 1, 8, 26, 60, 115, 196, 308, 456, 645, 880, 
	// 1166, 1508, 1911, 2380, 2920, 3536, 4233, 5016, 
	// 5890, 6860, 7931, 9108, 10396, 11800, 13325, 14976, 
	// 16758, 18676, 20735, 22940, 25296, 27808, 30481, 
	// 33320, 36330, 39516, 42883 ... etc
  
	// Test k = 10
	task.heptagonalPyramidalNo(10);
}
main();

Output

 0 1 8 26 60 115 196 308 456 645
#  Python 3 program for
#  Heptagonal pyramidal number
class HeptagonalPyramidalNumber :
	def heptagonalPyramidalNo(self, k) :
		n = 0
		#  Print all initial k heptagonal pyramidal number
		while (n < k) :
			#  Formula
			#   n (n + 1) (5n - 2) 
			#   ———————————————————
			#          6
            
			#  Calculate nth heptagonal pyramidal number
			result = int((n * (n + 1) * ((5 * n) - 2)) / 6)

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

def main() :
	task = HeptagonalPyramidalNumber()
	#  Heptagonal pyramidal number are
	#  —————————————————————————————————————————————
	#   0, 1, 8, 26, 60, 115, 196, 308, 456, 645, 880, 
	#  1166, 1508, 1911, 2380, 2920, 3536, 4233, 5016, 
	#  5890, 6860, 7931, 9108, 10396, 11800, 13325, 14976, 
	#  16758, 18676, 20735, 22940, 25296, 27808, 30481, 
	#  33320, 36330, 39516, 42883 ... etc
    
	#  Test k = 10
	task.heptagonalPyramidalNo(10)

if __name__ == "__main__": main()

Output

  0  1  8  26  60  115  196  308  456  645
#  Ruby program for
#  Heptagonal pyramidal number
class HeptagonalPyramidalNumber 
	def heptagonalPyramidalNo(k) 
		n = 0
		#  Print all initial k heptagonal pyramidal number
		while (n < k) 
			#  Formula
			#   n (n + 1) (5n - 2) 
			#   ———————————————————
			#          6
            
			#  Calculate nth heptagonal pyramidal number
			result = (n * (n + 1) * ((5 * n) - 2)) / 6

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

	end

end

def main() 
	task = HeptagonalPyramidalNumber.new()
	#  Heptagonal pyramidal number are
	#  —————————————————————————————————————————————
	#   0, 1, 8, 26, 60, 115, 196, 308, 456, 645, 880, 
	#  1166, 1508, 1911, 2380, 2920, 3536, 4233, 5016, 
	#  5890, 6860, 7931, 9108, 10396, 11800, 13325, 14976, 
	#  16758, 18676, 20735, 22940, 25296, 27808, 30481, 
	#  33320, 36330, 39516, 42883 ... etc
    
	#  Test k = 10
	task.heptagonalPyramidalNo(10)
end

main()

Output

 0 1 8 26 60 115 196 308 456 645
// Scala program for
// Heptagonal pyramidal number
class HeptagonalPyramidalNumber()
{
	def heptagonalPyramidalNo(k: Int): Unit = {
		var n: Int = 0;
		// Print all initial k heptagonal pyramidal number
		while (n < k)
		{
			// Formula
			//  n (n + 1) (5n - 2) 
			//  ———————————————————
			//         6
          
			// Calculate nth heptagonal pyramidal number
			var result: Int = (n * (n + 1) * ((5 * n) - 2)) / 6;
          
			// Display calculated result
			print(" " + result);
			n += 1;
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: HeptagonalPyramidalNumber = new HeptagonalPyramidalNumber();
		// Heptagonal pyramidal number are
		// —————————————————————————————————————————————
		//  0, 1, 8, 26, 60, 115, 196, 308, 456, 645, 880, 
		// 1166, 1508, 1911, 2380, 2920, 3536, 4233, 5016, 
		// 5890, 6860, 7931, 9108, 10396, 11800, 13325, 14976, 
		// 16758, 18676, 20735, 22940, 25296, 27808, 30481, 
		// 33320, 36330, 39516, 42883 ... etc
  
		// Test k = 10
		task.heptagonalPyramidalNo(10);
	}
}

Output

 0 1 8 26 60 115 196 308 456 645
// Swift 4 program for
// Heptagonal pyramidal number
class HeptagonalPyramidalNumber
{
	func heptagonalPyramidalNo(_ k: Int)
	{
		var n: Int = 0;
		// Print all initial k heptagonal pyramidal number
		while (n < k)
		{
			// Formula
			//  n (n + 1) (5n - 2) 
			//  ———————————————————
			//         6
          
			// Calculate nth heptagonal pyramidal number
			let result: Int = (n * (n + 1) * ((5 * n) - 2)) / 6;
          
			// Display calculated result
			print(" ", result, terminator: "");
			n += 1;
		}
	}
}
func main()
{
	let task: HeptagonalPyramidalNumber = HeptagonalPyramidalNumber();
	// Heptagonal pyramidal number are
	// —————————————————————————————————————————————
	//  0, 1, 8, 26, 60, 115, 196, 308, 456, 645, 880, 
	// 1166, 1508, 1911, 2380, 2920, 3536, 4233, 5016, 
	// 5890, 6860, 7931, 9108, 10396, 11800, 13325, 14976, 
	// 16758, 18676, 20735, 22940, 25296, 27808, 30481, 
	// 33320, 36330, 39516, 42883 ... etc
  
	// Test k = 10
	task.heptagonalPyramidalNo(10);
}
main();

Output

  0  1  8  26  60  115  196  308  456  645
// Kotlin program for
// Heptagonal pyramidal number
class HeptagonalPyramidalNumber
{
	fun heptagonalPyramidalNo(k: Int): Unit
	{
		var n: Int = 0;
		// Print all initial k heptagonal pyramidal number
		while (n < k)
		{
			// Formula
			//  n (n + 1) (5n - 2) 
			//  ———————————————————
			//         6
          
			// Calculate nth heptagonal pyramidal number
			val result: Int = (n * (n + 1) * ((5 * n) - 2)) / 6;
          
			// Display calculated result
			print(" " + result);
			n += 1;
		}
	}
}
fun main(args: Array < String > ): Unit
{
	val task: HeptagonalPyramidalNumber = HeptagonalPyramidalNumber();
	// Heptagonal pyramidal number are
	// —————————————————————————————————————————————
	//  0, 1, 8, 26, 60, 115, 196, 308, 456, 645, 880, 
	// 1166, 1508, 1911, 2380, 2920, 3536, 4233, 5016, 
	// 5890, 6860, 7931, 9108, 10396, 11800, 13325, 14976, 
	// 16758, 18676, 20735, 22940, 25296, 27808, 30481, 
	// 33320, 36330, 39516, 42883 ... etc
  
	// Test k = 10
	task.heptagonalPyramidalNo(10);
}

Output

 0 1 8 26 60 115 196 308 456 645

Time Complexity

The time complexity of the given code is O(k) because we are calculating the first 'k' heptagonal pyramidal numbers using a loop that runs from 0 to k-1.

Result and Output Explanation: For the given C code, when the function heptagonalPyramidalNo(10) is called, it calculates and prints the first 10 heptagonal pyramidal numbers, which are: 0 1 8 26 60 115 196 308 456 645

Finally Heptagonal pyramidal numbers are an interesting concept in mathematics that represents the number of balls needed to construct pyramids with heptagonal bases. The formula provided in the article can be used to calculate these numbers efficiently. The C program demonstrated in the article successfully prints the initial 'k' heptagonal pyramidal numbers. Understanding figurate numbers like heptagonal pyramidal numbers not only enhances our mathematical knowledge but also helps us appreciate the beauty and patterns in number sequences.





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