Posted on by Kalkicode
Code Number

Centered pentagonal number

The centered pentagonal number is a fascinating sequence in mathematics that combines both pentagonal numbers and triangular numbers. A pentagonal number is a figurate number representing the number of dots in a pentagonal pattern, while a centered pentagonal number is formed by placing a central dot surrounded by successive layers of dots arranged in a pentagonal pattern. In this article, we will explore the concept of centered pentagonal numbers, understand the algorithm to generate them, and analyze the time complexity of the provided C program.

Understanding the Problem

Before diving into the algorithm and code, let's first understand the problem by considering an example. Consider the following centered pentagonal numbers: 1, 6, 16, 31, 51, 76, 106, 141, 181, 226, ...

Each number in this sequence is formed by a specific formula based on the index 'n' of the centered pentagonal number. For instance, the formula to find the nth centered pentagonal number is:

Centered_pentagonal(n) = (5 * n^2 - 5 * n + 2) / 2

Pseudocode

Now, let's develop the pseudocode for generating the first 'k' centered pentagonal numbers.

function centeredPentagonalNumbers(k):
    for n from 1 to k do:
        result = (5 * n^2 - 5 * n + 2) / 2
        print result

Algorithm Explanation

  1. Start the centeredPentagonalNumbers function that takes an input 'k', representing the number of centered pentagonal numbers to generate.
  2. Loop from 'n' = 1 to 'k'.
  3. For each 'n', calculate the corresponding centered pentagonal number using the formula.
  4. Print the result.

Code Solution

Here given code implementation process.

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

void centeredPentagonalNo(int k)
{
	// Print all initial k centered pentagonal number
	for (int n = 1; n <= k; ++n)
	{
		//   Formula
		//   (5n² - 5n + 2) 
		//   ———————————————
		//        5
      
		// Calculate nth pentagonal number
		int result = (5 *(n *n) - 5 *n + 2) / 2;
      
		// Display Calculated value
		printf("  %d", result);
	}
}
int main()
{
	// Centered pentagonal numbers are
	// —————————————————————————————————————————————
	// 1, 6, 16, 31, 51, 76, 106, 141, 181, 226, 276, 
	// 331, 391, 456, 526, 601, 681, 766, 856, 951, 
	// 1051, 1156, 1266, 1381, 1501, 1626 ... etc
  
	//  Test  k = 10
	centeredPentagonalNo(10);
	return 0;
}

Output

  1  6  16  31  51  76  106  141  181  226
// Java program for
// Centered pentagonal number
public class CenteredPentagonal
{
	public void centeredPentagonalNo(int k)
	{
		// Print all initial k centered pentagonal number
		for (int n = 1; n <= k; ++n)
		{
			//   Formula
			//   (5n² - 5n + 2) 
			//   ———————————————
			//        5
          
			// Calculate nth pentagonal number
			int result = (5 * (n * n) - 5 * n + 2) / 2;
          
			// Display Calculated value
			System.out.print(" " + result);
		}
	}
	public static void main(String[] args)
	{
		CenteredPentagonal task = new CenteredPentagonal();
		// Centered pentagonal numbers are
		// —————————————————————————————————————————————
		// 1, 6, 16, 31, 51, 76, 106, 141, 181, 226, 276, 
		// 331, 391, 456, 526, 601, 681, 766, 856, 951, 
		// 1051, 1156, 1266, 1381, 1501, 1626 ... etc
      
		//  Test  k = 10
		task.centeredPentagonalNo(10);
	}
}

Output

 1 6 16 31 51 76 106 141 181 226
// Include header file
#include <iostream>
using namespace std;
// C++ program for
// Centered pentagonal number

class CenteredPentagonal
{
	public: void centeredPentagonalNo(int k)
	{
		// Print all initial k centered pentagonal number
		for (int n = 1; n <= k; ++n)
		{
			//   Formula
			//   (5n² - 5n + 2) 
			//   ———————————————
			//        5
          
			// Calculate nth pentagonal number
			int result = (5 *(n *n) - 5 *n + 2) / 2;
          
			// Display Calculated value
			cout << " " << result;
		}
	}
};
int main()
{
	CenteredPentagonal *task = new CenteredPentagonal();
	// Centered pentagonal numbers are
	// —————————————————————————————————————————————
	// 1, 6, 16, 31, 51, 76, 106, 141, 181, 226, 276, 
	// 331, 391, 456, 526, 601, 681, 766, 856, 951, 
	// 1051, 1156, 1266, 1381, 1501, 1626 ... etc
	//  Test  k = 10
	task->centeredPentagonalNo(10);
	return 0;
}

Output

 1 6 16 31 51 76 106 141 181 226
// Include namespace system
using System;
// Csharp program for
// Centered pentagonal number
public class CenteredPentagonal
{
	public void centeredPentagonalNo(int k)
	{
		// Print all initial k centered pentagonal number
		for (int n = 1; n <= k; ++n)
		{
			//   Formula
			//   (5n² - 5n + 2) 
			//   ———————————————
			//        5
          
			// Calculate nth pentagonal number
			int result = (5 * (n * n) - 5 * n + 2) / 2;
          
			// Display Calculated value
			Console.Write(" " + result);
		}
	}
	public static void Main(String[] args)
	{
		CenteredPentagonal task = new CenteredPentagonal();
		// Centered pentagonal numbers are
		// —————————————————————————————————————————————
		// 1, 6, 16, 31, 51, 76, 106, 141, 181, 226, 276, 
		// 331, 391, 456, 526, 601, 681, 766, 856, 951, 
		// 1051, 1156, 1266, 1381, 1501, 1626 ... etc
      
		//  Test  k = 10
		task.centeredPentagonalNo(10);
	}
}

Output

 1 6 16 31 51 76 106 141 181 226
package main
import "fmt"
// Go program for
// Centered pentagonal number

func centeredPentagonalNo(k int) {
	// Print all initial k centered pentagonal number
	for n := 1 ; n <= k ; n++ {
		//   Formula
		//   (5n² - 5n + 2) 
		//   ———————————————
		//        5

		// Calculate nth pentagonal number
		var result int = (5 * (n * n) - 5 * n + 2) / 2

		// Display Calculated value
		fmt.Print(" ", result)
	}
}
func main() {
	
	// Centered pentagonal numbers are
	// —————————————————————————————————————————————
	// 1, 6, 16, 31, 51, 76, 106, 141, 181, 226, 276, 
	// 331, 391, 456, 526, 601, 681, 766, 856, 951, 
	// 1051, 1156, 1266, 1381, 1501, 1626 ... etc
	
	//  Test  k = 10
	centeredPentagonalNo(10)
}

Output

 1 6 16 31 51 76 106 141 181 226
<?php
// Php program for
// Centered pentagonal number
class CenteredPentagonal
{
	public	function centeredPentagonalNo($k)
	{
		// Print all initial k centered pentagonal number
		for ($n = 1; $n <= $k; ++$n)
		{
			//   Formula
			//   (5n² - 5n + 2) 
			//   ———————————————
			//        5
          
			// Calculate nth pentagonal number
			$result = (int)((5 * ($n * $n) - 5 * $n + 2) / 2);
          
			// Display Calculated value
			echo(" ".$result);
		}
	}
}

function main()
{
	$task = new CenteredPentagonal();
	// Centered pentagonal numbers are
	// —————————————————————————————————————————————
	// 1, 6, 16, 31, 51, 76, 106, 141, 181, 226, 276, 
	// 331, 391, 456, 526, 601, 681, 766, 856, 951, 
	// 1051, 1156, 1266, 1381, 1501, 1626 ... etc
  
	//  Test  k = 10
	$task->centeredPentagonalNo(10);
}
main();

Output

 1 6 16 31 51 76 106 141 181 226
// Node JS program for
// Centered pentagonal number
class CenteredPentagonal
{
	centeredPentagonalNo(k)
	{
		// Print all initial k centered pentagonal number
		for (var n = 1; n <= k; ++n)
		{
			//   Formula
			//   (5n² - 5n + 2) 
			//   ———————————————
			//        5
          
			// Calculate nth pentagonal number
			var result = parseInt((5 * (n * n) - 5 * n + 2) / 2);
          
			// Display Calculated value
			process.stdout.write(" " + result);
		}
	}
}

function main()
{
	var task = new CenteredPentagonal();
	// Centered pentagonal numbers are
	// —————————————————————————————————————————————
	// 1, 6, 16, 31, 51, 76, 106, 141, 181, 226, 276, 
	// 331, 391, 456, 526, 601, 681, 766, 856, 951, 
	// 1051, 1156, 1266, 1381, 1501, 1626 ... etc
  
	//  Test  k = 10
	task.centeredPentagonalNo(10);
}
main();

Output

 1 6 16 31 51 76 106 141 181 226
#  Python 3 program for
#  Centered pentagonal number
class CenteredPentagonal :
	def centeredPentagonalNo(self, k) :
		n = 1
		#  Print all initial k centered pentagonal number
		while (n <= k) :
			#    Formula
			#    (5n² - 5n + 2) 
			#    ———————————————
			#         5
            
			#  Calculate nth pentagonal number
			result = int((5 * (n * n) - 5 * n + 2) / 2)

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

def main() :
	task = CenteredPentagonal()
	#  Centered pentagonal numbers are
	#  —————————————————————————————————————————————
	#  1, 6, 16, 31, 51, 76, 106, 141, 181, 226, 276, 
	#  331, 391, 456, 526, 601, 681, 766, 856, 951, 
	#  1051, 1156, 1266, 1381, 1501, 1626 ... etc
    
	#   Test  k = 10
	task.centeredPentagonalNo(10)

if __name__ == "__main__": main()

Output

  1  6  16  31  51  76  106  141  181  226
#  Ruby program for
#  Centered pentagonal number
class CenteredPentagonal 
	def centeredPentagonalNo(k) 
		n = 1
		#  Print all initial k centered pentagonal number
		while (n <= k) 
			#    Formula
			#    (5n² - 5n + 2) 
			#    ———————————————
			#         5
            
			#  Calculate nth pentagonal number
			result = (5 * (n * n) - 5 * n + 2) / 2

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

	end

end

def main() 
	task = CenteredPentagonal.new()
	#  Centered pentagonal numbers are
	#  —————————————————————————————————————————————
	#  1, 6, 16, 31, 51, 76, 106, 141, 181, 226, 276, 
	#  331, 391, 456, 526, 601, 681, 766, 856, 951, 
	#  1051, 1156, 1266, 1381, 1501, 1626 ... etc
    
	#   Test  k = 10
	task.centeredPentagonalNo(10)
end

main()

Output

 1 6 16 31 51 76 106 141 181 226
// Scala program for
// Centered pentagonal number
class CenteredPentagonal()
{
	def centeredPentagonalNo(k: Int): Unit = {
		var n: Int = 1;
		// Print all initial k centered pentagonal number
		while (n <= k)
		{
			//   Formula
			//   (5n² - 5n + 2) 
			//   ———————————————
			//        5
          
			// Calculate nth pentagonal number
			var result: Int = (5 * (n * n) - 5 * n + 2) / 2;
          
			// Display Calculated value
			print(" " + result);
			n += 1;
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: CenteredPentagonal = new CenteredPentagonal();
		// Centered pentagonal numbers are
		// —————————————————————————————————————————————
		// 1, 6, 16, 31, 51, 76, 106, 141, 181, 226, 276, 
		// 331, 391, 456, 526, 601, 681, 766, 856, 951, 
		// 1051, 1156, 1266, 1381, 1501, 1626 ... etc
  
		//  Test  k = 10
		task.centeredPentagonalNo(10);
	}
}

Output

 1 6 16 31 51 76 106 141 181 226
// Swift 4 program for
// Centered pentagonal number
class CenteredPentagonal
{
	func centeredPentagonalNo(_ k: Int)
	{
		var n: Int = 1;
		// Print all initial k centered pentagonal number
		while (n <= k)
		{
			//   Formula
			//   (5n² - 5n + 2) 
			//   ———————————————
			//        5
          
			// Calculate nth pentagonal number
			let result: Int = (5 * (n * n) - 5 * n + 2) / 2;
          
			// Display Calculated value
			print(" ", result, terminator: "");
			n += 1;
		}
	}
}
func main()
{
	let task: CenteredPentagonal = CenteredPentagonal();
	// Centered pentagonal numbers are
	// —————————————————————————————————————————————
	// 1, 6, 16, 31, 51, 76, 106, 141, 181, 226, 276, 
	// 331, 391, 456, 526, 601, 681, 766, 856, 951, 
	// 1051, 1156, 1266, 1381, 1501, 1626 ... etc
	//  Test  k = 10
	task.centeredPentagonalNo(10);
}
main();

Output

  1  6  16  31  51  76  106  141  181  226
// Kotlin program for
// Centered pentagonal number
class CenteredPentagonal
{
	fun centeredPentagonalNo(k: Int): Unit
	{
		var n: Int = 1;
		// Print all initial k centered pentagonal number
		while (n <= k)
		{
			//   Formula
			//   (5n² - 5n + 2) 
			//   ———————————————
			//        5
          
			// Calculate nth pentagonal number
			val result: Int = (5 * (n * n) - 5 * n + 2) / 2;
          
			// Display Calculated value
			print(" " + result);
			n += 1;
		}
	}
}
fun main(args: Array < String > ): Unit
{
	val task: CenteredPentagonal = CenteredPentagonal();
	// Centered pentagonal numbers are
	// —————————————————————————————————————————————
	// 1, 6, 16, 31, 51, 76, 106, 141, 181, 226, 276, 
	// 331, 391, 456, 526, 601, 681, 766, 856, 951, 
	// 1051, 1156, 1266, 1381, 1501, 1626 ... etc
	//  Test  k = 10
	task.centeredPentagonalNo(10);
}

Output

 1 6 16 31 51 76 106 141 181 226

Time Complexity Analysis

The time complexity of the provided algorithm can be analyzed by considering the dominant operation within the loop, which is the calculation of the centered pentagonal number for each 'n'. The calculation involves basic arithmetic operations (multiplication, addition, and division), which are constant-time operations. As a result, the time complexity of the algorithm is O(k), where 'k' is the input parameter representing the number of centered pentagonal numbers to generate.

Result and Output Explanation: Using the provided C code, the centeredPentagonalNo function is called with 'k = 10', which generates the first 10 centered pentagonal numbers. The output displayed by the code is as follows:

Output: 1 6 16 31 51 76 106 141 181 226

Explanation of the output

The function calculated the first 10 centered pentagonal numbers using the provided formula and displayed them as a space-separated sequence. Each number in the output represents the number of dots in a centered pentagonal pattern for the corresponding 'n'.

Finally

Centered pentagonal numbers form a captivating sequence with interesting properties. We explored the concept of centered pentagonal numbers, derived the formula to calculate them, and analyzed the time complexity of the given C program. The pseudocode and algorithm explained the process of generating the centered pentagonal numbers efficiently. The output demonstrated the first 10 centered pentagonal numbers as a sequence. Overall, centered pentagonal numbers are an exciting area of study in mathematics, and further research can lead to various applications and insights.

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