Posted on by Kalkicode
Code Number

Centered tetrahedral number

Centered tetrahedral numbers are a fascinating sequence of numbers that have numerous applications in mathematics and beyond. A centered tetrahedral number can be thought of as the number of balls needed to form a tetrahedral pyramid with a center ball and additional layers around it. These numbers have interesting properties and appear in various mathematical contexts, including combinatorics and number theory.

Problem Statement

The task is to write a program to calculate and display the first 'k' centered tetrahedral numbers using a given formula. The formula to find the 'n-th' centered tetrahedral number is (n * (n + 1) * (n + 2)) / 6. The program should output the first 'k' centered tetrahedral numbers, where 'k' is a positive integer provided as input.

Example

Let's take 'k' as 5 for illustration purposes. The first 5 centered tetrahedral numbers will be calculated as follows:

For n = 0: (0 * (0 + 1) * (0 + 2)) / 6 = 0 / 6 = 0

For n = 1: (1 * (1 + 1) * (1 + 2)) / 6 = 6 / 6 = 1

For n = 2: (2 * (2 + 1) * (2 + 2)) / 6 = 24 / 6 = 4

For n = 3: (3 * (3 + 1) * (3 + 2)) / 6 = 60 / 6 = 10

For n = 4: (4 * (4 + 1) * (4 + 2)) / 6 = 120 / 6 = 20

Thus, the first 5 centered tetrahedral numbers are 0, 1, 4, 10, and 20.

Pseudocode

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

Algorithm Explanation

1. Start the program execution from the 'main' function.
2. In the 'main' function, call the 'centeredTetrahedral' function with a parameter 'k' to calculate the first 'k' centered tetrahedral numbers.
3. Inside the 'centeredTetrahedral' function, use a loop to iterate over the values of 'n' from 0 to k-1.
4. For each 'n', use the given formula to calculate the centered tetrahedral number and store it in the 'result' variable.
5. Print the calculated 'result' for each value of 'n'.

Code Solution

Here given code implementation process.

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

void centeredTetrahedral(int k )
{
// Print all initial k centered tetrahedral number
for (int n = 0; n < k; ++n)
{
// Formula
//   n*(n+1)*(n+2)
// —————————————
//       6

// Calculate nth tetrahedral number
int result  =  (n*(n+1)*(n+2)) / 6 ;

// Display Calculated value
printf("  %d",result);
}
}

int main() {

// Centered tetrahedral number are
// —————————————————————————————————————————————
//  0, 1, 4, 10, 20, 35, 56, 84, 120, 165, 220, 286,
//  364, 455, 560, 680, 816, 969, 1140, 1330,
//  1540, 1771, 2024, 2300, 2600 ... etc

// Test k = 10
centeredTetrahedral(10);
return 0;
}``````

Output

``  0  1  4  10  20  35  56  84  120  165``
``````// Java program for
// Centered tetrahedral number
public class CenteredTetrahedral
{
public void centeredTetrahedralNo(int k)
{
// Print all initial k centered tetrahedral number
for (int n = 0; n < k; ++n)
{
// Formula
//   n*(n+1)*(n+2)
// —————————————
//       6
// Calculate nth tetrahedral number
int result = (n * (n + 1) * (n + 2)) / 6;
// Display Calculated value
System.out.print(" " + result);
}
}
public static void main(String[] args)
{
// Centered tetrahedral number are
// —————————————————————————————————————————————
//  0, 1, 4, 10, 20, 35, 56, 84, 120, 165, 220, 286,
//  364, 455, 560, 680, 816, 969, 1140, 1330,
//  1540, 1771, 2024, 2300, 2600 ... etc
// Test k = 10
}
}``````

Output

`` 0 1 4 10 20 35 56 84 120 165``
``````// Include header file
#include <iostream>
using namespace std;
// C++ program for
// Centered tetrahedral number
class CenteredTetrahedral
{
public: void centeredTetrahedralNo(int k)
{
// Print all initial k centered tetrahedral number
for (int n = 0; n < k; ++n)
{
// Formula
//   n*(n+1)*(n+2)
// —————————————
//       6
// Calculate nth tetrahedral number
int result = (n *(n + 1) *(n + 2)) / 6;
// Display Calculated value
cout << " " << result;
}
}
};
int main()
{
// Centered tetrahedral number are
// —————————————————————————————————————————————
//  0, 1, 4, 10, 20, 35, 56, 84, 120, 165, 220, 286,
//  364, 455, 560, 680, 816, 969, 1140, 1330,
//  1540, 1771, 2024, 2300, 2600 ... etc
// Test k = 10
return 0;
}``````

Output

`` 0 1 4 10 20 35 56 84 120 165``
``````// Include namespace system
using System;
// Csharp program for
// Centered tetrahedral number
public class CenteredTetrahedral
{
public void centeredTetrahedralNo(int k)
{
// Print all initial k centered tetrahedral number
for (int n = 0; n < k; ++n)
{
// Formula
//   n*(n+1)*(n+2)
// —————————————
//       6
// Calculate nth tetrahedral number
int result = (n * (n + 1) * (n + 2)) / 6;
// Display Calculated value
Console.Write(" " + result);
}
}
public static void Main(String[] args)
{
// Centered tetrahedral number are
// —————————————————————————————————————————————
//  0, 1, 4, 10, 20, 35, 56, 84, 120, 165, 220, 286,
//  364, 455, 560, 680, 816, 969, 1140, 1330,
//  1540, 1771, 2024, 2300, 2600 ... etc
// Test k = 10
}
}``````

Output

`` 0 1 4 10 20 35 56 84 120 165``
``````package main
import "fmt"
// Go program for
// Centered tetrahedral number

func centeredTetrahedralNo(k int) {
// Print all initial k centered tetrahedral number
for n := 0 ; n < k ; n++ {
// Formula
//   n*(n+1)*(n+2)
// —————————————
//       6
// Calculate nth tetrahedral number
var result int = (n * (n + 1) * (n + 2)) / 6
// Display Calculated value
fmt.Print(" ", result)
}
}
func main() {
// Centered tetrahedral number are
// —————————————————————————————————————————————
//  0, 1, 4, 10, 20, 35, 56, 84, 120, 165, 220, 286,
//  364, 455, 560, 680, 816, 969, 1140, 1330,
//  1540, 1771, 2024, 2300, 2600 ... etc
// Test k = 10
centeredTetrahedralNo(10)
}``````

Output

`` 0 1 4 10 20 35 56 84 120 165``
``````<?php
// Php program for
// Centered tetrahedral number
class CenteredTetrahedral
{
public	function centeredTetrahedralNo(\$k)
{
// Print all initial k centered tetrahedral number
for (\$n = 0; \$n < \$k; ++\$n)
{
// Formula
//   n*(n+1)*(n+2)
// —————————————
//       6
// Calculate nth tetrahedral number
\$result = (int)((\$n * (\$n + 1) * (\$n + 2)) / 6);
// Display Calculated value
echo(" ".\$result);
}
}
}

function main()
{
// Centered tetrahedral number are
// —————————————————————————————————————————————
//  0, 1, 4, 10, 20, 35, 56, 84, 120, 165, 220, 286,
//  364, 455, 560, 680, 816, 969, 1140, 1330,
//  1540, 1771, 2024, 2300, 2600 ... etc
// Test k = 10
}
main();``````

Output

`` 0 1 4 10 20 35 56 84 120 165``
``````// Node JS program for
// Centered tetrahedral number
class CenteredTetrahedral
{
centeredTetrahedralNo(k)
{
// Print all initial k centered tetrahedral number
for (var n = 0; n < k; ++n)
{
// Formula
//   n*(n+1)*(n+2)
// —————————————
//       6
// Calculate nth tetrahedral number
var result = parseInt((n * (n + 1) * (n + 2)) / 6);
// Display Calculated value
process.stdout.write(" " + result);
}
}
}

function main()
{
// Centered tetrahedral number are
// —————————————————————————————————————————————
//  0, 1, 4, 10, 20, 35, 56, 84, 120, 165, 220, 286,
//  364, 455, 560, 680, 816, 969, 1140, 1330,
//  1540, 1771, 2024, 2300, 2600 ... etc
// Test k = 10
}
main();``````

Output

`` 0 1 4 10 20 35 56 84 120 165``
``````#  Python 3 program for
#  Centered tetrahedral number
class CenteredTetrahedral :
def centeredTetrahedralNo(self, k) :
n = 0
#  Print all initial k centered tetrahedral number
while (n < k) :
#  Formula
#    n*(n+1)*(n+2)
#  —————————————
#        6
#  Calculate nth tetrahedral number
result = int((n * (n + 1) * (n + 2)) / 6)
#  Display Calculated value
print(" ", result, end = "")
n += 1

def main() :
#  Centered tetrahedral number are
#  —————————————————————————————————————————————
#   0, 1, 4, 10, 20, 35, 56, 84, 120, 165, 220, 286,
#   364, 455, 560, 680, 816, 969, 1140, 1330,
#   1540, 1771, 2024, 2300, 2600 ... etc
#  Test k = 10

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

Output

``  0  1  4  10  20  35  56  84  120  165``
``````#  Ruby program for
#  Centered tetrahedral number
class CenteredTetrahedral
def centeredTetrahedralNo(k)
n = 0
#  Print all initial k centered tetrahedral number
while (n < k)
#  Formula
#    n*(n+1)*(n+2)
#  —————————————
#        6
#  Calculate nth tetrahedral number
result = (n * (n + 1) * (n + 2)) / 6
#  Display Calculated value
print(" ", result)
n += 1
end
end
end

def main()
#  Centered tetrahedral number are
#  —————————————————————————————————————————————
#   0, 1, 4, 10, 20, 35, 56, 84, 120, 165, 220, 286,
#   364, 455, 560, 680, 816, 969, 1140, 1330,
#   1540, 1771, 2024, 2300, 2600 ... etc
#  Test k = 10
end

main()``````

Output

`` 0 1 4 10 20 35 56 84 120 165``
``````// Scala program for
// Centered tetrahedral number
class CenteredTetrahedral()
{
def centeredTetrahedralNo(k: Int): Unit = {
var n: Int = 0;
// Print all initial k centered tetrahedral number
while (n < k)
{
// Formula
//   n*(n+1)*(n+2)
// —————————————
//       6
// Calculate nth tetrahedral number
var result: Int = (n * (n + 1) * (n + 2)) / 6;
// Display Calculated value
print(" " + result);
n += 1;
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: CenteredTetrahedral = new CenteredTetrahedral();
// Centered tetrahedral number are
// —————————————————————————————————————————————
//  0, 1, 4, 10, 20, 35, 56, 84, 120, 165, 220, 286,
//  364, 455, 560, 680, 816, 969, 1140, 1330,
//  1540, 1771, 2024, 2300, 2600 ... etc

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

Output

`` 0 1 4 10 20 35 56 84 120 165``
``````// Swift 4 program for
// Centered tetrahedral number
class CenteredTetrahedral
{
func centeredTetrahedralNo(_ k: Int)
{
var n: Int = 0;
// Print all initial k centered tetrahedral number
while (n < k)
{
// Formula
//   n*(n+1)*(n+2)
// —————————————
//       6
// Calculate nth tetrahedral number
let result: Int = (n * (n + 1) * (n + 2)) / 6;
// Display Calculated value
print(" ", result, terminator: "");
n += 1;
}
}
}
func main()
{
// Centered tetrahedral number are
// —————————————————————————————————————————————
//  0, 1, 4, 10, 20, 35, 56, 84, 120, 165, 220, 286,
//  364, 455, 560, 680, 816, 969, 1140, 1330,
//  1540, 1771, 2024, 2300, 2600 ... etc

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

Output

``  0  1  4  10  20  35  56  84  120  165``
``````// Kotlin program for
// Centered tetrahedral number
class CenteredTetrahedral
{
fun centeredTetrahedralNo(k: Int): Unit
{
var n: Int = 0;
// Print all initial k centered tetrahedral number
while (n < k)
{
// Formula
//   n*(n+1)*(n+2)
// —————————————
//       6
// Calculate nth tetrahedral number
val result: Int = (n * (n + 1) * (n + 2)) / 6;
// Display Calculated value
print(" " + result);
n += 1;
}
}
}
fun main(args: Array < String > ): Unit
{
// Centered tetrahedral number are
// —————————————————————————————————————————————
//  0, 1, 4, 10, 20, 35, 56, 84, 120, 165, 220, 286,
//  364, 455, 560, 680, 816, 969, 1140, 1330,
//  1540, 1771, 2024, 2300, 2600 ... etc
// Test k = 10
}``````

Output

`` 0 1 4 10 20 35 56 84 120 165``

In the given C code, we have two functions: 'centeredTetrahedral' and 'main.'

1. The 'centeredTetrahedral' function takes an integer parameter 'k,' which represents the number of centered tetrahedral numbers to be calculated and displayed.

2. Inside the 'centeredTetrahedral' function, we use a for-loop to iterate over the values of 'n' from 0 to (k-1).

3. For each value of 'n,' we calculate the centered tetrahedral number using the formula (n * (n + 1) * (n + 2)) / 6 and store the result in the 'result' variable.

4. Finally, we print each calculated 'result' for the corresponding value of 'n' using the printf function.

Resultant Output Explanation:

After running the provided C code with k = 10, the output is: 0 1 4 10 20 35 56 84 120 165.

The output displays the first 10 centered tetrahedral numbers, as calculated by the 'centeredTetrahedral' function. Each number in the output sequence is the result of the formula applied to the corresponding value of 'n' (0 to 9).

Time Complexity:

The time complexity of the 'centeredTetrahedral' function is O(k), where 'k' is the input parameter representing the number of centered tetrahedral numbers to be calculated. The loop runs 'k' times, and for each iteration, a constant-time calculation is performed. Hence, the dominant factor in the time complexity is the loop's iteration count.

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