Posted on by Kalkicode
Code Number

# Centered Nonagonal Number

The centered nonagonal number is a sequence of numbers that can be represented by the formula: `((3 * n) - 2) * ((3 * n) - 1)) / 2`, where `n` is the position of the number in the sequence. In this article, we will explore the concept of centered nonagonal numbers, provide an explanation of the given code, and analyze its time complexity.

### Introduction

Centered nonagonal numbers are a subset of polygonal numbers, which are numbers that can be arranged in the shape of regular polygons. The centered nonagonal numbers, in particular, are formed by placing a dot at the center and surrounding it with concentric layers of nonagons (nine-sided polygons).

### Problem Statement

The goal of the given code is to calculate and print the first `k` centered nonagonal numbers. The code takes an integer `k` as input and uses a loop to iterate from 1 to `k`. In each iteration, the centered nonagonal formula is applied to calculate the corresponding centered nonagonal number, which is then displayed on the console.

For example, when the input `k` is 10, the code will calculate and print the first 10 centered nonagonal numbers: 1, 10, 28, 55, 91, 136, 190, 253, 325, and 406.

### Algorithm

The algorithm used in the given code is straightforward:

1. Take an integer `k` as input.
2. Iterate from `n = 1` to `n = k` using a loop.
3. Inside the loop, calculate the centered nonagonal number using the formula: `((3 * n) - 2) * ((3 * n) - 1)) / 2`.
4. Display the calculated centered nonagonal number.
5. Repeat steps 3-4 for all values of `n` from 1 to `k`.

The algorithm calculates the centered nonagonal number for each iteration of the loop, starting from `n = 1` and ending at `n = k`. The calculated centered nonagonal numbers are then printed on the console.

## Code Solution

Here given code implementation process.

``````// C Program for
// Centered Nonagonal Number
#include <stdio.h>

void centeredNonagonalNo(int k)
{
// Print all initial k centered nonagonal number
for (int n = 1; n <= k; ++n)
{
// Centered nonagonal formula
//  (3n-2) (3n-1)
//  —————————————
//       2
// Centered nonagonal number
int result = (((3 * n) - 2) * ((3 * n) - 1)) / 2;
// Display calculated result
printf("  %d", result);
}
}
int main()
{
// Centered Nonagonal numbers
// --------------------------------------
// 1, 10, 28, 55, 91, 136, 190, 253,
// 325, 406, 496, 595, 703, 820, 946 ... etc.
// Test
// k  = 10
centeredNonagonalNo(10);
return 0;
}``````

#### Output

``  1  10  28  55  91  136  190  253  325  406``
``````// Java program for
// Centered Nonagonal Number
public class NonagonalNumber
{
public void centeredNonagonalNo(int k)
{
// Print all initial k centered nonagonal number
for (int n = 1; n <= k; ++n)
{
// Centered nonagonal formula
//  (3n-2) (3n-1)
//  —————————————
//       2
// Centered nonagonal number
int result = (((3 * n) - 2) * ((3 * n) - 1)) / 2;
// Display calculated result
System.out.print(" " + result);
}
}
public static void main(String[] args)
{
// Centered Nonagonal numbers
// --------------------------------------
// 1, 10, 28, 55, 91, 136, 190, 253,
// 325, 406, 496, 595, 703, 820, 946 ... etc.
// Test
// k  = 10
}
}``````

#### Output

`` 1 10 28 55 91 136 190 253 325 406``
``````// Include header file
#include <iostream>
using namespace std;
// C++ program for
// Centered Nonagonal Number
class NonagonalNumber
{
public: void centeredNonagonalNo(int k)
{
// Print all initial k centered nonagonal number
for (int n = 1; n <= k; ++n)
{
// Centered nonagonal formula
//  (3n-2) (3n-1)
//  —————————————
//       2

// Centered nonagonal number
int result = (((3 *n) - 2) *((3 *n) - 1)) / 2;

// Display calculated result
cout << " " << result;
}
}
};
int main()
{
// Centered Nonagonal numbers
// --------------------------------------
// 1, 10, 28, 55, 91, 136, 190, 253,
// 325, 406, 496, 595, 703, 820, 946 ... etc.

// Test
// k  = 10
return 0;
}``````

#### Output

`` 1 10 28 55 91 136 190 253 325 406``
``````// Include namespace system
using System;
// Csharp program for
// Centered Nonagonal Number
public class NonagonalNumber
{
public void centeredNonagonalNo(int k)
{
// Print all initial k centered nonagonal number
for (int n = 1; n <= k; ++n)
{
// Centered nonagonal formula
//  (3n-2) (3n-1)
//  —————————————
//       2

// Centered nonagonal number
int result = (((3 * n) - 2) * ((3 * n) - 1)) / 2;

// Display calculated result
Console.Write(" " + result);
}
}
public static void Main(String[] args)
{
// Centered Nonagonal numbers
// --------------------------------------
// 1, 10, 28, 55, 91, 136, 190, 253,
// 325, 406, 496, 595, 703, 820, 946 ... etc.

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

#### Output

`` 1 10 28 55 91 136 190 253 325 406``
``````package main
import "fmt"
// Go program for
// Centered Nonagonal Number

func centeredNonagonalNo(k int) {
// Print all initial k centered nonagonal number
for n := 1 ; n <= k ; n++ {
// Centered nonagonal formula
//  (3n-2) (3n-1)
//  —————————————
//       2

// Centered nonagonal number
var result int = (((3 * n) - 2) * ((3 * n) - 1)) / 2

// Display calculated result
fmt.Print(" ", result)
}
}
func main() {

// Centered Nonagonal numbers
// --------------------------------------
// 1, 10, 28, 55, 91, 136, 190, 253,
// 325, 406, 496, 595, 703, 820, 946 ... etc.

// Test
// k  = 10
centeredNonagonalNo(10)
}``````

#### Output

`` 1 10 28 55 91 136 190 253 325 406``
``````<?php
// Php program for
// Centered Nonagonal Number
class NonagonalNumber
{
public	function centeredNonagonalNo(\$k)
{
// Print all initial k centered nonagonal number
for (\$n = 1; \$n <= \$k; ++\$n)
{
// Centered nonagonal formula
//  (3n-2) (3n-1)
//  —————————————
//       2

// Centered nonagonal number
\$result = (int)((((3 * \$n) - 2) * ((3 * \$n) - 1)) / 2);

// Display calculated result
echo(" ".\$result);
}
}
}

function main()
{
// Centered Nonagonal numbers
// --------------------------------------
// 1, 10, 28, 55, 91, 136, 190, 253,
// 325, 406, 496, 595, 703, 820, 946 ... etc.

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

#### Output

`` 1 10 28 55 91 136 190 253 325 406``
``````// Node JS program for
// Centered Nonagonal Number
class NonagonalNumber
{
centeredNonagonalNo(k)
{
// Print all initial k centered nonagonal number
for (var n = 1; n <= k; ++n)
{
// Centered nonagonal formula
//  (3n-2) (3n-1)
//  —————————————
//       2

// Centered nonagonal number
var result = parseInt((((3 * n) - 2) * ((3 * n) - 1)) / 2);

// Display calculated result
process.stdout.write(" " + result);
}
}
}

function main()
{
// Centered Nonagonal numbers
// --------------------------------------
// 1, 10, 28, 55, 91, 136, 190, 253,
// 325, 406, 496, 595, 703, 820, 946 ... etc.
// Test
// k  = 10
}
main();``````

#### Output

`` 1 10 28 55 91 136 190 253 325 406``
``````#  Python 3 program for
#  Centered Nonagonal Number
class NonagonalNumber :
def centeredNonagonalNo(self, k) :
n = 1
#  Print all initial k centered nonagonal number
while (n <= k) :
#  Centered nonagonal formula
#   (3n-2) (3n-1)
#   —————————————
#        2

#  Centered nonagonal number
result = int((((3 * n) - 2) * ((3 * n) - 1)) / 2)

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

def main() :
#  Centered Nonagonal numbers
#  --------------------------------------
#  1, 10, 28, 55, 91, 136, 190, 253,
#  325, 406, 496, 595, 703, 820, 946 ... etc.

#  Test
#  k  = 10

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

#### Output

``  1  10  28  55  91  136  190  253  325  406``
``````#  Ruby program for
#  Centered Nonagonal Number
class NonagonalNumber
def centeredNonagonalNo(k)
n = 1
#  Print all initial k centered nonagonal number
while (n <= k)
#  Centered nonagonal formula
#   (3n-2) (3n-1)
#   —————————————
#        2

#  Centered nonagonal number
result = (((3 * n) - 2) * ((3 * n) - 1)) / 2

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

end

end

def main()
#  Centered Nonagonal numbers
#  --------------------------------------
#  1, 10, 28, 55, 91, 136, 190, 253,
#  325, 406, 496, 595, 703, 820, 946 ... etc.

#  Test
#  k  = 10
end

main()``````

#### Output

`` 1 10 28 55 91 136 190 253 325 406``
``````// Scala program for
// Centered Nonagonal Number
class NonagonalNumber()
{
def centeredNonagonalNo(k: Int): Unit = {
var n: Int = 1;
// Print all initial k centered nonagonal number
while (n <= k)
{
// Centered nonagonal formula
//  (3n-2) (3n-1)
//  —————————————
//       2

// Centered nonagonal number
var result: Int = (((3 * n) - 2) * ((3 * n) - 1)) / 2;

// Display calculated result
print(" " + result);
n += 1;
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: NonagonalNumber = new NonagonalNumber();
// Centered Nonagonal numbers
// --------------------------------------
// 1, 10, 28, 55, 91, 136, 190, 253,
// 325, 406, 496, 595, 703, 820, 946 ... etc.
// Test
// k  = 10
}
}``````

#### Output

`` 1 10 28 55 91 136 190 253 325 406``
``````// Swift 4 program for
// Centered Nonagonal Number
class NonagonalNumber
{
func centeredNonagonalNo(_ k: Int)
{
var n: Int = 1;
// Print all initial k centered nonagonal number
while (n <= k)
{
// Centered nonagonal formula
//  (3n-2) (3n-1)
//  —————————————
//       2
// Centered nonagonal number
let result: Int = (((3 * n) - 2) * ((3 * n) - 1)) / 2;
// Display calculated result
print(" ", result, terminator: "");
n += 1;
}
}
}
func main()
{
// Centered Nonagonal numbers
// --------------------------------------
// 1, 10, 28, 55, 91, 136, 190, 253,
// 325, 406, 496, 595, 703, 820, 946 ... etc.
// Test
// k  = 10
}
main();``````

#### Output

``  1  10  28  55  91  136  190  253  325  406``
``````// Kotlin program for
// Centered Nonagonal Number
class NonagonalNumber
{
fun centeredNonagonalNo(k: Int): Unit
{
var n: Int = 1;
// Print all initial k centered nonagonal number
while (n <= k)
{
// Centered nonagonal formula
//  (3n-2) (3n-1)
//  —————————————
//       2

// Centered nonagonal number
val result: Int = (((3 * n) - 2) * ((3 * n) - 1)) / 2;

// Display calculated result
print(" " + result);
n += 1;
}
}
}
fun main(args: Array < String > ): Unit
{
// Centered Nonagonal numbers
// --------------------------------------
// 1, 10, 28, 55, 91, 136, 190, 253,
// 325, 406, 496, 595, 703, 820, 946 ... etc.

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

#### Output

`` 1 10 28 55 91 136 190 253 325 406``

### Time Complexity

The time complexity of the given code is `O(k)`, where `k` is the input value. This is because the code uses a loop that iterates `k` times to calculate and display the centered nonagonal numbers.

### Resultant Output

The code, when executed with an input value of `k = 10`, will generate the following output:

```  1  10  28  55  91  136  190  253  325  406
```

The output represents the first 10 centered nonagonal numbers, as calculated by the code.

### Finally

In this article, we explored the concept of centered nonagonal numbers and provided an explanation of the given code to calculate and print these numbers. We discussed the problem statement, algorithm, and time complexity of the code. Additionally, we presented the resultant output for a sample input value. The centered nonagonal numbers have various applications in mathematics and can be further studied to explore their properties and patterns.

## 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