Posted on by Kalkicode
Code Number

Centered triangular number

A centered triangular number is a figurate number that represents a triangle with a dot at the center and additional dots surrounding it in a triangular pattern. Each centered triangular number can be calculated using the formula (3n² + 3n + 1) / 2, where 'n' is the position of the number in the sequence. In this article, we will explore the concept of centered triangular numbers, provide a detailed explanation of the problem statement, present an algorithm and pseudocode for calculating centered triangular numbers, and explain the output of the provided code example.

Problem Statement

The problem is to calculate and display the first 'k' centered triangular numbers, where 'k' is a given positive integer. The centered triangular numbers are generated by applying the formula (3n² + 3n + 1) / 2 for values of 'n' ranging from 0 to 'k-1'. The goal is to obtain a sequence of centered triangular numbers that can be used for various mathematical and computational purposes.

For example, let's consider the case where 'k' is 10. The first 10 centered triangular numbers are: 1, 4, 10, 19, 31, 46, 64, 85, 109, and 136. These numbers represent the number of dots in the centered triangle at each position in the sequence.

Algorithm and Pseudocode

The algorithm for calculating 'k' centered triangular numbers can be summarized as follows:

  1. Define a function centeredTriangularNo(k) that takes an integer 'k' as input.
  2. Iterate 'n' from 0 to 'k-1' using a for loop.
  3. Inside the loop, calculate the 'n'-th centered triangular number using the formula ((3n² + 3n + 1) / 2) and store it in a variable called 'result'.
  4. Print the value of 'result'.
  5. End the function.
  6. In the main function, call the centeredTriangularNo(k) function with a desired value of 'k'.
  7. Return 0 to indicate successful program execution.

Below is the pseudocode representation of the algorithm:

function centeredTriangularNo(k):
    for n from 0 to k-1:
        result = (3n² + 3n + 1) / 2
        print result

main():
    centeredTriangularNo(k)
    return 0

Code Solution

Here given code implementation process.

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

void centeredTriangularNo(int k)
{
	// Print all initial k centered triangular number
	for (int n = 0; n < k; ++n)
	{
		// Formula
		//  (3n²) + 3n + 1
		// —————————————
		//       2
      
		// Calculate nth triangular number
		int result = ((3 *(n *n) + (3 *n) + 2) / 2);
      
		// Display calculated result
		printf("  %d", result);
	}
}
int main()
{
	// Centered triangular number are
	// —————————————————————————————————————————————
	// 1, 4, 10, 19, 31, 46, 64, 85, 109, 136, 166, 
	// 199, 235, 274, 316, 361, 409, 460, 514, 
	// 571, 631, 694, 760, 829 ... etc
  
	// k = 10
	centeredTriangularNo(10);
	return 0;
}

Output

  1  4  10  19  31  46  64  85  109  136
// Java program for
// Centered triangular number
public class TriangularNumber
{
	public void centeredTriangularNo(int k)
	{
		// Print all initial k centered triangular number
		for (int n = 0; n < k; ++n)
		{
			// Formula
			//  (3n²) + 3n + 1
			// —————————————
			//       2
          
			// Calculate nth triangular number
			int result = ((3 * (n * n) + (3 * n) + 2) / 2);
          
			// Display calculated result
			System.out.print(" " + result);
		}
	}
	public static void main(String[] args)
	{
		TriangularNumber task = new TriangularNumber();
		// Centered triangular number are
		// —————————————————————————————————————————————
		// 1, 4, 10, 19, 31, 46, 64, 85, 109, 136, 166, 
		// 199, 235, 274, 316, 361, 409, 460, 514, 
		// 571, 631, 694, 760, 829 ... etc
      
		// k = 10
		task.centeredTriangularNo(10);
	}
}

Output

 1 4 10 19 31 46 64 85 109 136
// Include header file
#include <iostream>
using namespace std;
// C++ program for
// Centered triangular number
class TriangularNumber
{
	public: void centeredTriangularNo(int k)
	{
		// Print all initial k centered triangular number
		for (int n = 0; n < k; ++n)
		{
			// Formula
			//  (3n²) + 3n + 1
			// —————————————
			//       2
          
			// Calculate nth triangular number
			int result = ((3 *(n *n) + (3 *n) + 2) / 2);
          
			// Display calculated result
			cout << " " << result;
		}
	}
};
int main()
{
	TriangularNumber *task = new TriangularNumber();
	// Centered triangular number are
	// —————————————————————————————————————————————
	// 1, 4, 10, 19, 31, 46, 64, 85, 109, 136, 166, 
	// 199, 235, 274, 316, 361, 409, 460, 514, 
	// 571, 631, 694, 760, 829 ... etc
  
	// k = 10
	task->centeredTriangularNo(10);
	return 0;
}

Output

 1 4 10 19 31 46 64 85 109 136
// Include namespace system
using System;
// Csharp program for
// Centered triangular number
public class TriangularNumber
{
	public void centeredTriangularNo(int k)
	{
		// Print all initial k centered triangular number
		for (int n = 0; n < k; ++n)
		{
			// Formula
			//  (3n²) + 3n + 1
			// —————————————
			//       2
          
			// Calculate nth triangular number
			int result = ((3 * (n * n) + (3 * n) + 2) / 2);
          
			// Display calculated result
			Console.Write(" " + result);
		}
	}
	public static void Main(String[] args)
	{
		TriangularNumber task = new TriangularNumber();
		// Centered triangular number are
		// —————————————————————————————————————————————
		// 1, 4, 10, 19, 31, 46, 64, 85, 109, 136, 166, 
		// 199, 235, 274, 316, 361, 409, 460, 514, 
		// 571, 631, 694, 760, 829 ... etc
      
		// k = 10
		task.centeredTriangularNo(10);
	}
}

Output

 1 4 10 19 31 46 64 85 109 136
package main
import "fmt"
// Go program for
// Centered triangular number

func centeredTriangularNo(k int) {
	// Print all initial k centered triangular number
	for n := 0 ; n < k ; n++ {
		// Formula
		//  (3n²) + 3n + 1
		// —————————————
		//       2
		// Calculate nth triangular number
		var result int = ((3 * (n * n) + (3 * n) + 2) / 2)
		// Display calculated result
		fmt.Print(" ", result)
	}
}
func main() {
	
	// Centered triangular number are
	// —————————————————————————————————————————————
	// 1, 4, 10, 19, 31, 46, 64, 85, 109, 136, 166, 
	// 199, 235, 274, 316, 361, 409, 460, 514, 
	// 571, 631, 694, 760, 829 ... etc
  
	// k = 10
	centeredTriangularNo(10)
}

Output

 1 4 10 19 31 46 64 85 109 136
<?php
// Php program for
// Centered triangular number
class TriangularNumber
{
	public	function centeredTriangularNo($k)
	{
		// Print all initial k centered triangular number
		for ($n = 0; $n < $k; ++$n)
		{
			// Formula
			//  (3n²) + 3n + 1
			// —————————————
			//       2
          
			// Calculate nth triangular number
			$result = ((int)((3 * ($n * $n) + (3 * $n) + 2) / 2));
          
			// Display calculated result
			echo(" ".$result);
		}
	}
}

function main()
{
	$task = new TriangularNumber();
	// Centered triangular number are
	// —————————————————————————————————————————————
	// 1, 4, 10, 19, 31, 46, 64, 85, 109, 136, 166, 
	// 199, 235, 274, 316, 361, 409, 460, 514, 
	// 571, 631, 694, 760, 829 ... etc
  
	// k = 10
	$task->centeredTriangularNo(10);
}
main();

Output

 1 4 10 19 31 46 64 85 109 136
// Node JS program for
// Centered triangular number
class TriangularNumber
{
	centeredTriangularNo(k)
	{
		// Print all initial k centered triangular number
		for (var n = 0; n < k; ++n)
		{
			// Formula
			//  (3n²) + 3n + 1
			// —————————————
			//       2
          
			// Calculate nth triangular number
			var result = (parseInt((3 * (n * n) + (3 * n) + 2) / 2));
          
			// Display calculated result
			process.stdout.write(" " + result);
		}
	}
}

function main()
{
	var task = new TriangularNumber();
	// Centered triangular number are
	// —————————————————————————————————————————————
	// 1, 4, 10, 19, 31, 46, 64, 85, 109, 136, 166, 
	// 199, 235, 274, 316, 361, 409, 460, 514, 
	// 571, 631, 694, 760, 829 ... etc
  
	// k = 10
	task.centeredTriangularNo(10);
}
main();

Output

 1 4 10 19 31 46 64 85 109 136
#  Python 3 program for
#  Centered triangular number
class TriangularNumber :
	def centeredTriangularNo(self, k) :
		n = 0
		#  Print all initial k centered triangular number
		while (n < k) :
			#  Formula
			#   (3n²) + 3n + 1
			#  —————————————
			#        2
            
			#  Calculate nth triangular number
			result = (int((3 * (n * n) + (3 * n) + 2) / 2))

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

def main() :
	task = TriangularNumber()
	#  Centered triangular number are
	#  —————————————————————————————————————————————
	#  1, 4, 10, 19, 31, 46, 64, 85, 109, 136, 166, 
	#  199, 235, 274, 316, 361, 409, 460, 514, 
	#  571, 631, 694, 760, 829 ... etc
    
	#  k = 10
	task.centeredTriangularNo(10)

if __name__ == "__main__": main()

Output

  1  4  10  19  31  46  64  85  109  136
#  Ruby program for
#  Centered triangular number
class TriangularNumber 
	def centeredTriangularNo(k) 
		n = 0
		#  Print all initial k centered triangular number
		while (n < k) 
			#  Formula
			#   (3n²) + 3n + 1
			#  —————————————
			#        2
            
			#  Calculate nth triangular number
			result = ((3 * (n * n) + (3 * n) + 2) / 2)

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

	end

end

def main() 
	task = TriangularNumber.new()
	#  Centered triangular number are
	#  —————————————————————————————————————————————
	#  1, 4, 10, 19, 31, 46, 64, 85, 109, 136, 166, 
	#  199, 235, 274, 316, 361, 409, 460, 514, 
	#  571, 631, 694, 760, 829 ... etc
    
	#  k = 10
	task.centeredTriangularNo(10)
end

main()

Output

 1 4 10 19 31 46 64 85 109 136
// Scala program for
// Centered triangular number
class TriangularNumber()
{
	def centeredTriangularNo(k: Int): Unit = {
		var n: Int = 0;
		// Print all initial k centered triangular number
		while (n < k)
		{
			// Formula
			//  (3n²) + 3n + 1
			// —————————————
			//       2
          
			// Calculate nth triangular number
			var result: Int = ((3 * (n * n) + (3 * n) + 2) / 2);
          
			// Display calculated result
			print(" " + result);
			n += 1;
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: TriangularNumber = new TriangularNumber();
		// Centered triangular number are
		// —————————————————————————————————————————————
		// 1, 4, 10, 19, 31, 46, 64, 85, 109, 136, 166, 
		// 199, 235, 274, 316, 361, 409, 460, 514, 
		// 571, 631, 694, 760, 829 ... etc
		// k = 10
		task.centeredTriangularNo(10);
	}
}

Output

 1 4 10 19 31 46 64 85 109 136
// Swift 4 program for
// Centered triangular number
class TriangularNumber
{
	func centeredTriangularNo(_ k: Int)
	{
		var n: Int = 0;
		// Print all initial k centered triangular number
		while (n < k)
		{
			// Formula
			//  (3n²) + 3n + 1
			// —————————————
			//       2
          
			// Calculate nth triangular number
			let result: Int = ((3 * (n * n) + (3 * n) + 2) / 2);
          
			// Display calculated result
			print(" ", result, terminator: "");
			n += 1;
		}
	}
}
func main()
{
	let task: TriangularNumber = TriangularNumber();
	// Centered triangular number are
	// —————————————————————————————————————————————
	// 1, 4, 10, 19, 31, 46, 64, 85, 109, 136, 166, 
	// 199, 235, 274, 316, 361, 409, 460, 514, 
	// 571, 631, 694, 760, 829 ... etc
  
	// k = 10
	task.centeredTriangularNo(10);
}
main();

Output

  1  4  10  19  31  46  64  85  109  136
// Kotlin program for
// Centered triangular number
class TriangularNumber
{
	fun centeredTriangularNo(k: Int): Unit
	{
		var n: Int = 0;
		// Print all initial k centered triangular number
		while (n < k)
		{
			// Formula
			//  (3n²) + 3n + 1
			// —————————————
			//       2
          
			// Calculate nth triangular number
			val result: Int = ((3 * (n * n) + (3 * n) + 2) / 2);
          
			// Display calculated result
			print(" " + result);
			n += 1;
		}
	}
}
fun main(args: Array < String > ): Unit
{
	val task: TriangularNumber = TriangularNumber();
	// Centered triangular number are
	// —————————————————————————————————————————————
	// 1, 4, 10, 19, 31, 46, 64, 85, 109, 136, 166, 
	// 199, 235, 274, 316, 361, 409, 460, 514, 
	// 571, 631, 694, 760, 829 ... etc
  
	// k = 10
	task.centeredTriangularNo(10);
}

Output

 1 4 10 19 31 46 64 85 109 136

Output Explanation

The provided code example calculates and displays the first 10 centered triangular numbers. The output of the code is: 1 4 10 19 31 46 64 85 109 136. These values represent the number of dots in the centered triangle at each position in the sequence.

Time Complexity

The time complexity of the algorithm is O(k), where 'k' is the given input. The algorithm iterates 'n' from 0 to 'k-1' and performs constant time operations to calculate and print each centered triangular number. Therefore, the time taken by the algorithm is directly proportional to the value of '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