Skip to main content

Pentagonal pyramidal number

The Pentagonal Pyramidal Number is a sequence of numbers that represents the number of dots required to form a pyramid with pentagonal layers. In this problem, we are given the task to generate the first 'k' pentagonal pyramidal numbers and display them. The formula to calculate the nth pentagonal pyramidal number is (n^2 * (n + 1)) / 2.

Problem Statement & Example

We need to write a program in C to generate the first 'k' pentagonal pyramidal numbers and display them. For example, if 'k' is 10, then the program should generate and display the first 10 pentagonal pyramidal numbers: 1, 6, 18, 40, 75, 126, 196, 288, 405, 550.

Pseudocode & Algorithm

The algorithm for generating 'k' pentagonal pyramidal numbers is quite straightforward. We need a loop that iterates from 1 to 'k', and for each value of 'n', we apply the formula to calculate the nth pentagonal pyramidal number. Let's represent the algorithm in pseudocode:

// Function to generate and display the first 'k' pentagonal pyramidal numbers
function pentagonalPyramidalNo(k):
    // Loop from n = 1 to n = k
    for n = 1 to k do:
        // Calculate the nth pentagonal pyramidal number using the formula
        result = (n^2 * (n + 1)) / 2

        // Display the calculated result
        print result

// Main function
function main():
    // Initialize 'k' as 10 (can be any desired value)
    k = 10
    
    // Call the function to generate and display the first 'k' pentagonal pyramidal numbers
    pentagonalPyramidalNo(k)

// Call the main function to start the program
main()

Program Solution

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

void pentagonalPyramidalNo(int k)
{
	// Print all initial k pentagonal pyramidal number
	for (int n = 1; n <= k; ++n)
	{
		// Formula
		//  n²(n+1)
		//  —————————
		//     2
      
		// Calculate nth pentagonal pyramidal number
		int result = ((n *n) *(n + 1)) / 2;
      
		// Display calculated result
		printf("  %d", result);
	}
}
int main()
{
	//  Pentagonal pyramidal number are
	// —————————————————————————————————————————————
	// 1, 6, 18, 40, 75, 126, 196, 288, 405, 550, 726, 
	// 936, 1183, 1470, 1800, 2176, 2601, 3078, 3610, 
	// 4200, 4851, 5566, 6348, 7200, 8125, 9126, 10206, 
	// 11368, 12615, 13950, 15376, 16896, 18513, 
	// 20230, 22050, 23976 etc.
  
	// k = 10
	pentagonalPyramidalNo(10);
	return 0;
}

Output

  1  6  18  40  75  126  196  288  405  550
// Java program for
// Pentagonal pyramidal number
public class PentagonalNumber
{
	public void pentagonalPyramidalNo(int k)
	{
		// Print all initial k pentagonal pyramidal number
		for (int n = 1; n <= k; ++n)
		{
			// Formula
			//  n²(n+1)
			//  —————————
			//     2
			// Calculate nth pentagonal pyramidal number
			int result = ((n * n) * (n + 1)) / 2;
			// Display calculated result
			System.out.print(" " + result);
		}
	}
	public static void main(String[] args)
	{
		PentagonalNumber task = new PentagonalNumber();
		//  Pentagonal pyramidal number are
		// —————————————————————————————————————————————
		// 1, 6, 18, 40, 75, 126, 196, 288, 405, 550, 726, 
		// 936, 1183, 1470, 1800, 2176, 2601, 3078, 3610, 
		// 4200, 4851, 5566, 6348, 7200, 8125, 9126, 10206, 
		// 11368, 12615, 13950, 15376, 16896, 18513, 
		// 20230, 22050, 23976 etc.
		// k = 10
		task.pentagonalPyramidalNo(10);
	}
}

Output

 1 6 18 40 75 126 196 288 405 550
// Include header file
#include <iostream>
using namespace std;
// C++ program for
// Pentagonal pyramidal number

class PentagonalNumber
{
	public: void pentagonalPyramidalNo(int k)
	{
		// Print all initial k pentagonal pyramidal number
		for (int n = 1; n <= k; ++n)
		{
			// Formula
			//  n²(n+1)
			//  —————————
			//     2
          
			// Calculate nth pentagonal pyramidal number
			int result = ((n *n) *(n + 1)) / 2;
          
			// Display calculated result
			cout << " " << result;
		}
	}
};
int main()
{
	PentagonalNumber *task = new PentagonalNumber();
	//  Pentagonal pyramidal number are
	// —————————————————————————————————————————————
	// 1, 6, 18, 40, 75, 126, 196, 288, 405, 550, 726, 
	// 936, 1183, 1470, 1800, 2176, 2601, 3078, 3610, 
	// 4200, 4851, 5566, 6348, 7200, 8125, 9126, 10206, 
	// 11368, 12615, 13950, 15376, 16896, 18513, 
	// 20230, 22050, 23976 etc.
  
	// k = 10
	task->pentagonalPyramidalNo(10);
	return 0;
}

Output

 1 6 18 40 75 126 196 288 405 550
// Include namespace system
using System;
// Csharp program for
// Pentagonal pyramidal number
public class PentagonalNumber
{
	public void pentagonalPyramidalNo(int k)
	{
		// Print all initial k pentagonal pyramidal number
		for (int n = 1; n <= k; ++n)
		{
			// Formula
			//  n²(n+1)
			//  —————————
			//     2
          
			// Calculate nth pentagonal pyramidal number
			int result = ((n * n) * (n + 1)) / 2;
          
			// Display calculated result
			Console.Write(" " + result);
		}
	}
	public static void Main(String[] args)
	{
		PentagonalNumber task = new PentagonalNumber();
		//  Pentagonal pyramidal number are
		// —————————————————————————————————————————————
		// 1, 6, 18, 40, 75, 126, 196, 288, 405, 550, 726, 
		// 936, 1183, 1470, 1800, 2176, 2601, 3078, 3610, 
		// 4200, 4851, 5566, 6348, 7200, 8125, 9126, 10206, 
		// 11368, 12615, 13950, 15376, 16896, 18513, 
		// 20230, 22050, 23976 etc.
      
		// k = 10
		task.pentagonalPyramidalNo(10);
	}
}

Output

 1 6 18 40 75 126 196 288 405 550
package main
import "fmt"
// Go program for
// Pentagonal pyramidal number

func pentagonalPyramidalNo(k int) {
	// Print all initial k pentagonal pyramidal number
	for n := 1 ; n <= k ; n++ {
		// Formula
		//  n²(n+1)
		//  —————————
		//     2
      
		// Calculate nth pentagonal pyramidal number
		var result int = ((n * n) * (n + 1)) / 2
      
		// Display calculated result
		fmt.Print(" ", result)
	}
}
func main() {
	
	//  Pentagonal pyramidal number are
	// —————————————————————————————————————————————
	// 1, 6, 18, 40, 75, 126, 196, 288, 405, 550, 726, 
	// 936, 1183, 1470, 1800, 2176, 2601, 3078, 3610, 
	// 4200, 4851, 5566, 6348, 7200, 8125, 9126, 10206, 
	// 11368, 12615, 13950, 15376, 16896, 18513, 
	// 20230, 22050, 23976 etc.
  
	// k = 10
	pentagonalPyramidalNo(10)
}

Output

 1 6 18 40 75 126 196 288 405 550
<?php
// Php program for
// Pentagonal pyramidal number
class PentagonalNumber
{
	public	function pentagonalPyramidalNo($k)
	{
		// Print all initial k pentagonal pyramidal number
		for ($n = 1; $n <= $k; ++$n)
		{
			// Formula
			//  n²(n+1)
			//  —————————
			//     2
          
			// Calculate nth pentagonal pyramidal number
			$result = (int)((($n * $n) * ($n + 1)) / 2);
          
			// Display calculated result
			echo(" ".$result);
		}
	}
}

function main()
{
	$task = new PentagonalNumber();
	//  Pentagonal pyramidal number are
	// —————————————————————————————————————————————
	// 1, 6, 18, 40, 75, 126, 196, 288, 405, 550, 726, 
	// 936, 1183, 1470, 1800, 2176, 2601, 3078, 3610, 
	// 4200, 4851, 5566, 6348, 7200, 8125, 9126, 10206, 
	// 11368, 12615, 13950, 15376, 16896, 18513, 
	// 20230, 22050, 23976 etc.
  
	// k = 10
	$task->pentagonalPyramidalNo(10);
}
main();

Output

 1 6 18 40 75 126 196 288 405 550
// Node JS program for
// Pentagonal pyramidal number
class PentagonalNumber
{
	pentagonalPyramidalNo(k)
	{
		// Print all initial k pentagonal pyramidal number
		for (var n = 1; n <= k; ++n)
		{
			// Formula
			//  n²(n+1)
			//  ———————
			//     2
          
			// Calculate nth pentagonal pyramidal number
			var result = parseInt(((n * n) * (n + 1)) / 2);
          
			// Display calculated result
			process.stdout.write(" " + result);
		}
	}
}

function main()
{
	var task = new PentagonalNumber();
	//  Pentagonal pyramidal number are
	// —————————————————————————————————————————————
	// 1, 6, 18, 40, 75, 126, 196, 288, 405, 550, 726, 
	// 936, 1183, 1470, 1800, 2176, 2601, 3078, 3610, 
	// 4200, 4851, 5566, 6348, 7200, 8125, 9126, 10206, 
	// 11368, 12615, 13950, 15376, 16896, 18513, 
	// 20230, 22050, 23976 etc.
  
	// k = 10
	task.pentagonalPyramidalNo(10);
}
main();

Output

 1 6 18 40 75 126 196 288 405 550
#  Python 3 program for
#  Pentagonal pyramidal number
class PentagonalNumber :
	def pentagonalPyramidalNo(self, k) :
		n = 1
		#  Print all initial k pentagonal pyramidal number
		while (n <= k) :
			#  Formula
			#   n²(n+1)
			#   —————————
			#      2
            
			#  Calculate nth pentagonal pyramidal number
			result = int(((n * n) * (n + 1)) / 2)

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

def main() :
	task = PentagonalNumber()
	#   Pentagonal pyramidal number are
	#  —————————————————————————————————————————————
	#  1, 6, 18, 40, 75, 126, 196, 288, 405, 550, 726, 
	#  936, 1183, 1470, 1800, 2176, 2601, 3078, 3610, 
	#  4200, 4851, 5566, 6348, 7200, 8125, 9126, 10206, 
	#  11368, 12615, 13950, 15376, 16896, 18513, 
	#  20230, 22050, 23976 etc.
    
	#  k = 10
	task.pentagonalPyramidalNo(10)

if __name__ == "__main__": main()

Output

  1  6  18  40  75  126  196  288  405  550
#  Ruby program for
#  Pentagonal pyramidal number
class PentagonalNumber 
	def pentagonalPyramidalNo(k) 
		n = 1
		#  Print all initial k pentagonal pyramidal number
		while (n <= k) 
			#  Formula
			#   n²(n+1)
			#   —————————
			#      2
            
			#  Calculate nth pentagonal pyramidal number
			result = ((n * n) * (n + 1)) / 2

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

	end

end

def main() 
	task = PentagonalNumber.new()
	#   Pentagonal pyramidal number are
	#  —————————————————————————————————————————————
	#  1, 6, 18, 40, 75, 126, 196, 288, 405, 550, 726, 
	#  936, 1183, 1470, 1800, 2176, 2601, 3078, 3610, 
	#  4200, 4851, 5566, 6348, 7200, 8125, 9126, 10206, 
	#  11368, 12615, 13950, 15376, 16896, 18513, 
	#  20230, 22050, 23976 etc.
    
	#  k = 10
	task.pentagonalPyramidalNo(10)
end

main()

Output

 1 6 18 40 75 126 196 288 405 550
// Scala program for
// Pentagonal pyramidal number
class PentagonalNumber()
{
	def pentagonalPyramidalNo(k: Int): Unit = {
		var n: Int = 1;
		// Print all initial k pentagonal pyramidal number
		while (n <= k)
		{
			// Formula
			//  n²(n+1)
			//  —————————
			//     2
          
			// Calculate nth pentagonal pyramidal number
			var result: Int = ((n * n) * (n + 1)) / 2;
          
			// Display calculated result
			print(" " + result);
			n += 1;
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: PentagonalNumber = new PentagonalNumber();
		//  Pentagonal pyramidal number are
		// —————————————————————————————————————————————
		// 1, 6, 18, 40, 75, 126, 196, 288, 405, 550, 726, 
		// 936, 1183, 1470, 1800, 2176, 2601, 3078, 3610, 
		// 4200, 4851, 5566, 6348, 7200, 8125, 9126, 10206, 
		// 11368, 12615, 13950, 15376, 16896, 18513, 
		// 20230, 22050, 23976 etc.
  
		// k = 10
		task.pentagonalPyramidalNo(10);
	}
}

Output

 1 6 18 40 75 126 196 288 405 550
// Swift 4 program for
// Pentagonal pyramidal number
class PentagonalNumber
{
	func pentagonalPyramidalNo(_ k: Int)
	{
		var n: Int = 1;
		// Print all initial k pentagonal pyramidal number
		while (n <= k)
		{
			// Formula
			//  n²(n+1)
			//  —————————
			//     2
          
			// Calculate nth pentagonal pyramidal number
			let result: Int = ((n * n) * (n + 1)) / 2;
          
			// Display calculated result
			print(" ", result, terminator: "");
			n += 1;
		}
	}
}
func main()
{
	let task: PentagonalNumber = PentagonalNumber();
	//  Pentagonal pyramidal number are
	// —————————————————————————————————————————————
	// 1, 6, 18, 40, 75, 126, 196, 288, 405, 550, 726, 
	// 936, 1183, 1470, 1800, 2176, 2601, 3078, 3610, 
	// 4200, 4851, 5566, 6348, 7200, 8125, 9126, 10206, 
	// 11368, 12615, 13950, 15376, 16896, 18513, 
	// 20230, 22050, 23976 etc.
  
	// k = 10
	task.pentagonalPyramidalNo(10);
}
main();

Output

  1  6  18  40  75  126  196  288  405  550
// Kotlin program for
// Pentagonal pyramidal number
class PentagonalNumber
{
	fun pentagonalPyramidalNo(k: Int): Unit
	{
		var n: Int = 1;
		// Print all initial k pentagonal pyramidal number
		while (n <= k)
		{
			// Formula
			//  n²(n+1)
			//  ———————
			//     2
          
			// Calculate nth pentagonal pyramidal number
			val result: Int = ((n * n) * (n + 1)) / 2;
          
			// Display calculated result
			print(" " + result);
			n += 1;
		}
	}
}
fun main(args: Array < String > ): Unit
{
	val task: PentagonalNumber = PentagonalNumber();
	//  Pentagonal pyramidal number are
	// —————————————————————————————————————————————
	// 1, 6, 18, 40, 75, 126, 196, 288, 405, 550, 726, 
	// 936, 1183, 1470, 1800, 2176, 2601, 3078, 3610, 
	// 4200, 4851, 5566, 6348, 7200, 8125, 9126, 10206, 
	// 11368, 12615, 13950, 15376, 16896, 18513, 
	// 20230, 22050, 23976 etc.
  
	// k = 10
	task.pentagonalPyramidalNo(10);
}

Output

 1 6 18 40 75 126 196 288 405 550

Output Explanation

The provided C code implements the above algorithm. The function pentagonalPyramidalNo(k) takes an integer 'k' as input, representing the number of pentagonal pyramidal numbers to generate. It then uses a loop from 1 to 'k' to calculate and display the first 'k' pentagonal pyramidal numbers.

The main function calls pentagonalPyramidalNo(10) to generate and display the first 10 pentagonal pyramidal numbers. The output of this code is:

1  6  18  40  75  126  196  288  405  550

Time Complexity

The time complexity of this code is O(k), where 'k' is the number of pentagonal pyramidal numbers to be generated. The reason is that the function pentagonalPyramidalNo(k) contains a loop that iterates 'k' times. For each iteration, the algorithm performs constant-time calculations, so the overall time complexity is linear in '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.

New Comment