Posted on by Kalkicode
Code Number

# Centered octagonal number

In number theory, the centered octagonal number is a fascinating sequence of integers that forms a specific pattern. Centered octagonal numbers can be represented by the formula (4n² - 4n + 1), where 'n' is a positive integer. These numbers are called "centered" because they can be visualized as octagons with a dot at the center, and each successive octagon is formed by adding a ring of eight additional dots around the previous one.

## Problem Statement

The task is to write a program to generate the first 'k' centered octagonal numbers and display the results.

## Explanation with an Example

Let's take the value of 'k' as 10. We need to find the first 10 centered octagonal numbers using the given formula. The formula for the nth centered octagonal number is (4n² - 4n + 1).

1st centered octagonal number (n = 1): result = 4*(1^2) - 4*1 + 1 result = 4 - 4 + 1 result = 1

2nd centered octagonal number (n = 2): result = 4*(2^2) - 4*2 + 1 result = 16 - 8 + 1 result = 9

3rd centered octagonal number (n = 3): result = 4*(3^2) - 4*3 + 1 result = 36 - 12 + 1 result = 25

...and so on, until the 10th centered octagonal number.

## Standard Pseudocode

``````Function centeredOctagonalNo(k)
for n from 1 to k
result = 4 * (n * n) - 4 * n + 1
Print result
``````

## Algorithm Explanation

1. Start by defining a function centeredOctagonalNo that takes an integer parameter 'k' as input.
2. Inside the function, use a for loop to iterate over 'n' from 1 to 'k' (both inclusive).
3. For each 'n', calculate the corresponding centered octagonal number using the formula (4 * (n * n) - 4 * n + 1).
4. Print the calculated centered octagonal number.

## Code Solution

Here given code implementation process.

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

void centeredOctagonalNo(int k)
{
// Print all initial k centered octagonal number
for (int n = 1; n <= k; ++n)
{
//   Formula
//   (4n² - 4n + 1)

// Calculate nth octagonal number
int result = ((4 *(n *n) - 4 *n + 1));

// Display Calculated value
printf("  %d", result);
}
}
int main()
{
// Centered octagonal numbers are
// —————————————————————————————————————————————
//   1, 9, 25, 49, 81, 121, 169, 225, 289, 361
//   441, 529, 625, 729, 841, 961 ... etc
//  Test  k = 10
centeredOctagonalNo(10);
return 0;
}``````

#### Output

``  1  9  25  49  81  121  169  225  289  361``
``````// Java program for
// Centered octagonal number
public class CenteredOctagonal
{
public void centeredOctagonalNo(int k)
{
// Print all initial k centered octagonal number
for (int n = 1; n <= k; ++n)
{
//   Formula
//   (4n² - 4n + 1)

// Calculate nth octagonal number
int result = ((4 * (n * n) - 4 * n + 1));

// Display calculated result
System.out.print(" " + result);
}
}
public static void main(String[] args)
{
// Centered octagonal numbers are
// —————————————————————————————————————————————
//   1, 9, 25, 49, 81, 121, 169, 225, 289, 361
//   441, 529, 625, 729, 841, 961 ... etc
//  Test  k = 10
}
}``````

#### Output

`` 1 9 25 49 81 121 169 225 289 361``
``````// Include header file
#include <iostream>
using namespace std;
// C++ program for
// Centered octagonal number

class CenteredOctagonal
{
public: void centeredOctagonalNo(int k)
{
// Print all initial k centered octagonal number
for (int n = 1; n <= k; ++n)
{
//   Formula
//   (4n² - 4n + 1)

// Calculate nth octagonal number
int result = ((4 *(n *n) - 4 *n + 1));

// Display calculated result
cout << " " << result;
}
}
};
int main()
{
// Centered octagonal numbers are
// —————————————————————————————————————————————
//   1, 9, 25, 49, 81, 121, 169, 225, 289, 361
//   441, 529, 625, 729, 841, 961 ... etc
//  Test  k = 10
return 0;
}``````

#### Output

`` 1 9 25 49 81 121 169 225 289 361``
``````// Include namespace system
using System;
// Csharp program for
// Centered octagonal number
public class CenteredOctagonal
{
public void centeredOctagonalNo(int k)
{
// Print all initial k centered octagonal number
for (int n = 1; n <= k; ++n)
{
//   Formula
//   (4n² - 4n + 1)

// Calculate nth octagonal number
int result = ((4 * (n * n) - 4 * n + 1));

// Display calculated result
Console.Write(" " + result);
}
}
public static void Main(String[] args)
{
// Centered octagonal numbers are
// —————————————————————————————————————————————
//   1, 9, 25, 49, 81, 121, 169, 225, 289, 361
//   441, 529, 625, 729, 841, 961 ... etc
//  Test  k = 10
}
}``````

#### Output

`` 1 9 25 49 81 121 169 225 289 361``
``````package main
import "fmt"
// Go program for
// Centered octagonal number

func centeredOctagonalNo(k int) {
// Print all initial k centered octagonal number
for n := 1 ; n <= k ; n++ {
//   Formula
//   (4n² - 4n + 1)

// Calculate nth octagonal number
var result int = ((4 * (n * n) - 4 * n + 1))

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

// Centered octagonal numbers are
// —————————————————————————————————————————————
//   1, 9, 25, 49, 81, 121, 169, 225, 289, 361
//   441, 529, 625, 729, 841, 961 ... etc

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

#### Output

`` 1 9 25 49 81 121 169 225 289 361``
``````<?php
// Php program for
// Centered octagonal number
class CenteredOctagonal
{
public	function centeredOctagonalNo(\$k)
{
// Print all initial k centered octagonal number
for (\$n = 1; \$n <= \$k; ++\$n)
{
//   Formula
//   (4n² - 4n + 1)

// Calculate nth octagonal number
\$result = ((4 * (\$n * \$n) - 4 * \$n + 1));

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

function main()
{
// Centered octagonal numbers are
// —————————————————————————————————————————————
//   1, 9, 25, 49, 81, 121, 169, 225, 289, 361
//   441, 529, 625, 729, 841, 961 ... etc
//  Test  k = 10
}
main();``````

#### Output

`` 1 9 25 49 81 121 169 225 289 361``
``````// Node JS program for
// Centered octagonal number
class CenteredOctagonal
{
centeredOctagonalNo(k)
{
// Print all initial k centered octagonal number
for (var n = 1; n <= k; ++n)
{
//   Formula
//   (4n² - 4n + 1)

// Calculate nth octagonal number
var result = ((4 * (n * n) - 4 * n + 1));

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

function main()
{
// Centered octagonal numbers are
// —————————————————————————————————————————————
//   1, 9, 25, 49, 81, 121, 169, 225, 289, 361
//   441, 529, 625, 729, 841, 961 ... etc

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

#### Output

`` 1 9 25 49 81 121 169 225 289 361``
``````#  Python 3 program for
#  Centered octagonal number
class CenteredOctagonal :
def centeredOctagonalNo(self, k) :
n = 1
#  Print all initial k centered octagonal number
while (n <= k) :
#    Formula
#    (4n² - 4n + 1)

#  Calculate nth octagonal number
result = ((4 * (n * n) - 4 * n + 1))

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

def main() :
#  Centered octagonal numbers are
#  —————————————————————————————————————————————
#    1, 9, 25, 49, 81, 121, 169, 225, 289, 361
#    441, 529, 625, 729, 841, 961 ... etc

#   Test  k = 10

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

#### Output

``  1  9  25  49  81  121  169  225  289  361``
``````#  Ruby program for
#  Centered octagonal number
class CenteredOctagonal
def centeredOctagonalNo(k)
n = 1
#  Print all initial k centered octagonal number
while (n <= k)
#    Formula
#    (4n² - 4n + 1)

#  Calculate nth octagonal number
result = ((4 * (n * n) - 4 * n + 1))

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

end

end

def main()
#  Centered octagonal numbers are
#  —————————————————————————————————————————————
#    1, 9, 25, 49, 81, 121, 169, 225, 289, 361
#    441, 529, 625, 729, 841, 961 ... etc

#   Test  k = 10
end

main()``````

#### Output

`` 1 9 25 49 81 121 169 225 289 361``
``````// Scala program for
// Centered octagonal number
class CenteredOctagonal()
{
def centeredOctagonalNo(k: Int): Unit = {
var n: Int = 1;
// Print all initial k centered octagonal number
while (n <= k)
{
//   Formula
//   (4n² - 4n + 1)

// Calculate nth octagonal number
var result: Int = ((4 * (n * n) - 4 * n + 1));

// Display calculated result
print(" " + result);
n += 1;
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: CenteredOctagonal = new CenteredOctagonal();
// Centered octagonal numbers are
// —————————————————————————————————————————————
//   1, 9, 25, 49, 81, 121, 169, 225, 289, 361
//   441, 529, 625, 729, 841, 961 ... etc

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

#### Output

`` 1 9 25 49 81 121 169 225 289 361``
``````// Swift 4 program for
// Centered octagonal number
class CenteredOctagonal
{
func centeredOctagonalNo(_ k: Int)
{
var n: Int = 1;
// Print all initial k centered octagonal number
while (n <= k)
{
//   Formula
//   (4n² - 4n + 1)

// Calculate nth octagonal number
let result: Int = ((4 * (n * n) - 4 * n + 1));

// Display calculated result
print(" ", result, terminator: "");
n += 1;
}
}
}
func main()
{
// Centered octagonal numbers are
// —————————————————————————————————————————————
//   1, 9, 25, 49, 81, 121, 169, 225, 289, 361
//   441, 529, 625, 729, 841, 961 ... etc

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

#### Output

``  1  9  25  49  81  121  169  225  289  361``
``````// Kotlin program for
// Centered octagonal number
class CenteredOctagonal
{
fun centeredOctagonalNo(k: Int): Unit
{
var n: Int = 1;
// Print all initial k centered octagonal number
while (n <= k)
{
//   Formula
//   (4n² - 4n + 1)

// Calculate nth octagonal number
val result: Int = ((4 * (n * n) - 4 * n + 1));

// Display calculated result
print(" " + result);
n += 1;
}
}
}
fun main(args: Array < String > ): Unit
{
// Centered octagonal numbers are
// —————————————————————————————————————————————
//   1, 9, 25, 49, 81, 121, 169, 225, 289, 361
//   441, 529, 625, 729, 841, 961 ... etc

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

#### Output

`` 1 9 25 49 81 121 169 225 289 361``

## Resultant Output Explanation

The provided C program uses the centeredOctagonalNo function to find the first 10 centered octagonal numbers (k = 10). The output will display these numbers as follows:

Output: 1 9 25 49 81 121 169 225 289 361

## Time Complexity

The time complexity of the code is O(k) because the function centeredOctagonalNo iterates 'n' from 1 to 'k', and for each 'n', it performs constant time operations to calculate the centered octagonal number and print it. As the input 'k' grows, the number of iterations will increase linearly, leading to linear time complexity.

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