# Centered square numbers

In mathematics, centered square numbers are a sequence of numbers obtained by squaring the positive integers and adding consecutive odd numbers to them. These numbers are called "centered" because they can be visually represented as a square with a central dot surrounded by layers of concentric squares. The formula to calculate centered square numbers is `(n^2) + ((n - 1)^2)`, where `n` represents the position in the sequence.

Let's take an example to understand this concept. Suppose we want to find the first 5 centered square numbers. We start with the number 1, which is obtained by squaring 1. The next number is calculated by squaring 2 (4) and adding the previous square, which is 0. So, the second centered square number is 4 + 0 = 4. Following the same pattern, we can calculate the third, fourth, and fifth centered square numbers as 9, 16, and 25, respectively.

Here's the algorithm to generate the first `k` centered square numbers:

``````centeredSquareNo(k):
for n = 1 to k:
result = (n^2) + ((n - 1)^2)
print result
``````

## Code Solution

Here given code implementation process.

``````// C Program for
// Centered square numbers
#include <stdio.h>

void centeredSquareNo(int k)
{
// Print all initial k centered square number
for (int n = 1; n <= k; ++n)
{
// Centered square
// formula = (n)² + (n-1)²

// CalculatedCentered square numbers
int result = ((n *n) + ((n - 1) *(n - 1)));

// Display calculated result
printf("  %d", result);
}
}
int main()
{
// Centered square numbers
// --------------------------------------
// 1, 5, 13, 25, 41, 61, 85, 113, 145, 181, 221,
// 265, 313, 365, 421, 481, 545, 613, 685, 761,
// 841, 925, 1013, 1105, 1201, 1301, 1405,
// 1513, 1625, 1741, 1861, 1985, 2113, 2245,
// 2381, 2521, 2665, 2813, 2965, 3121, 3281,
// 3445, 3613, 3785, 3961, 4141 .. etc

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

#### Output

``  1  5  13  25  41  61  85  113  145  181``
``````// Java program for
// Centered square numbers
public class SquareNumber
{
public void centeredSquareNo(int k)
{
// Print all initial k centered square number
for (int n = 1; n <= k; ++n)
{
// Centered square
// formula = (n)² + (n-1)²

// CalculatedCentered square numbers
int result = ((n * n) + ((n - 1) * (n - 1)));

// Display calculated result
System.out.print(" " + result);
}
}
public static void main(String[] args)
{
// Centered square numbers
// --------------------------------------
// 1, 5, 13, 25, 41, 61, 85, 113, 145, 181, 221,
// 265, 313, 365, 421, 481, 545, 613, 685, 761,
// 841, 925, 1013, 1105, 1201, 1301, 1405,
// 1513, 1625, 1741, 1861, 1985, 2113, 2245,
// 2381, 2521, 2665, 2813, 2965, 3121, 3281,
// 3445, 3613, 3785, 3961, 4141 .. etc

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

#### Output

`` 1 5 13 25 41 61 85 113 145 181``
``````// Include header file
#include <iostream>
using namespace std;
// C++ program for
// Centered square numbers

class SquareNumber
{
public: void centeredSquareNo(int k)
{
// Print all initial k centered square number
for (int n = 1; n <= k; ++n)
{
// Centered square
// formula = (n)² + (n-1)²

// CalculatedCentered square numbers
int result = ((n *n) + ((n - 1) *(n - 1)));

// Display calculated result
cout << " " << result;
}
}
};
int main()
{
// Centered square numbers
// --------------------------------------
// 1, 5, 13, 25, 41, 61, 85, 113, 145, 181, 221,
// 265, 313, 365, 421, 481, 545, 613, 685, 761,
// 841, 925, 1013, 1105, 1201, 1301, 1405,
// 1513, 1625, 1741, 1861, 1985, 2113, 2245,
// 2381, 2521, 2665, 2813, 2965, 3121, 3281,
// 3445, 3613, 3785, 3961, 4141 .. etc

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

#### Output

`` 1 5 13 25 41 61 85 113 145 181``
``````// Include namespace system
using System;
// Csharp program for
// Centered square numbers
public class SquareNumber
{
public void centeredSquareNo(int k)
{
// Print all initial k centered square number
for (int n = 1; n <= k; ++n)
{
// Centered square
// formula = (n)² + (n-1)²

// CalculatedCentered square numbers
int result = ((n * n) + ((n - 1) * (n - 1)));

// Display calculated result
Console.Write(" " + result);
}
}
public static void Main(String[] args)
{
// Centered square numbers
// --------------------------------------
// 1, 5, 13, 25, 41, 61, 85, 113, 145, 181, 221,
// 265, 313, 365, 421, 481, 545, 613, 685, 761,
// 841, 925, 1013, 1105, 1201, 1301, 1405,
// 1513, 1625, 1741, 1861, 1985, 2113, 2245,
// 2381, 2521, 2665, 2813, 2965, 3121, 3281,
// 3445, 3613, 3785, 3961, 4141 .. etc

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

#### Output

`` 1 5 13 25 41 61 85 113 145 181``
``````package main
import "fmt"
// Go program for
// Centered square numbers

func centeredSquareNo(k int) {
// Print all initial k centered square number
for n := 1 ; n <= k ; n++ {
// Centered square
// formula = (n)² + (n-1)²

// CalculatedCentered square numbers
var result int = ((n * n) + ((n - 1) * (n - 1)))

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

// Centered square numbers
// --------------------------------------
// 1, 5, 13, 25, 41, 61, 85, 113, 145, 181, 221,
// 265, 313, 365, 421, 481, 545, 613, 685, 761,
// 841, 925, 1013, 1105, 1201, 1301, 1405,
// 1513, 1625, 1741, 1861, 1985, 2113, 2245,
// 2381, 2521, 2665, 2813, 2965, 3121, 3281,
// 3445, 3613, 3785, 3961, 4141 .. etc

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

#### Output

`` 1 5 13 25 41 61 85 113 145 181``
``````<?php
// Php program for
// Centered square numbers
class SquareNumber
{
public	function centeredSquareNo(\$k)
{
// Print all initial k centered square number
for (\$n = 1; \$n <= \$k; ++\$n)
{
// Centered square
// formula = (n)² + (n-1)²

// CalculatedCentered square numbers
\$result = ((\$n * \$n) + ((\$n - 1) * (\$n - 1)));

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

function main()
{
// Centered square numbers
// --------------------------------------
// 1, 5, 13, 25, 41, 61, 85, 113, 145, 181, 221,
// 265, 313, 365, 421, 481, 545, 613, 685, 761,
// 841, 925, 1013, 1105, 1201, 1301, 1405,
// 1513, 1625, 1741, 1861, 1985, 2113, 2245,
// 2381, 2521, 2665, 2813, 2965, 3121, 3281,
// 3445, 3613, 3785, 3961, 4141 .. etc

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

#### Output

`` 1 5 13 25 41 61 85 113 145 181``
``````// Node JS program for
// Centered square numbers
class SquareNumber
{
centeredSquareNo(k)
{
// Print all initial k centered square number
for (var n = 1; n <= k; ++n)
{
// Centered square
// formula = (n)² + (n-1)²

// CalculatedCentered square numbers
var result = ((n * n) + ((n - 1) * (n - 1)));

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

function main()
{
// Centered square numbers
// --------------------------------------
// 1, 5, 13, 25, 41, 61, 85, 113, 145, 181, 221,
// 265, 313, 365, 421, 481, 545, 613, 685, 761,
// 841, 925, 1013, 1105, 1201, 1301, 1405,
// 1513, 1625, 1741, 1861, 1985, 2113, 2245,
// 2381, 2521, 2665, 2813, 2965, 3121, 3281,
// 3445, 3613, 3785, 3961, 4141 .. etc

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

#### Output

`` 1 5 13 25 41 61 85 113 145 181``
``````#  Python 3 program for
#  Centered square numbers
class SquareNumber :
def centeredSquareNo(self, k) :
n = 1
#  Print all initial k centered square number
while (n <= k) :
#  Centered square
#  formula = (n)² + (n-1)²

#  CalculatedCentered square numbers
result = ((n * n) + ((n - 1) * (n - 1)))

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

def main() :
#  Centered square numbers
#  --------------------------------------
#  1, 5, 13, 25, 41, 61, 85, 113, 145, 181, 221,
#  265, 313, 365, 421, 481, 545, 613, 685, 761,
#  841, 925, 1013, 1105, 1201, 1301, 1405,
#  1513, 1625, 1741, 1861, 1985, 2113, 2245,
#  2381, 2521, 2665, 2813, 2965, 3121, 3281,
#  3445, 3613, 3785, 3961, 4141 .. etc

#  Test
#  k = 10

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

#### Output

``  1  5  13  25  41  61  85  113  145  181``
``````#  Ruby program for
#  Centered square numbers
class SquareNumber
def centeredSquareNo(k)
n = 1
#  Print all initial k centered square number
while (n <= k)
#  Centered square
#  formula = (n)² + (n-1)²

#  CalculatedCentered square numbers
result = ((n * n) + ((n - 1) * (n - 1)))

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

end

end

def main()
#  Centered square numbers
#  --------------------------------------
#  1, 5, 13, 25, 41, 61, 85, 113, 145, 181, 221,
#  265, 313, 365, 421, 481, 545, 613, 685, 761,
#  841, 925, 1013, 1105, 1201, 1301, 1405,
#  1513, 1625, 1741, 1861, 1985, 2113, 2245,
#  2381, 2521, 2665, 2813, 2965, 3121, 3281,
#  3445, 3613, 3785, 3961, 4141 .. etc

#  Test
#  k = 10
end

main()``````

#### Output

`` 1 5 13 25 41 61 85 113 145 181``
``````// Scala program for
// Centered square numbers
class SquareNumber()
{
def centeredSquareNo(k: Int): Unit = {
var n: Int = 1;
// Print all initial k centered square number
while (n <= k)
{
// Centered square
// formula = (n)² + (n-1)²

// CalculatedCentered square numbers
var result: Int = ((n * n) + ((n - 1) * (n - 1)));

// Display calculated result
print(" " + result);
n += 1;
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: SquareNumber = new SquareNumber();
// Centered square numbers
// --------------------------------------
// 1, 5, 13, 25, 41, 61, 85, 113, 145, 181, 221,
// 265, 313, 365, 421, 481, 545, 613, 685, 761,
// 841, 925, 1013, 1105, 1201, 1301, 1405,
// 1513, 1625, 1741, 1861, 1985, 2113, 2245,
// 2381, 2521, 2665, 2813, 2965, 3121, 3281,
// 3445, 3613, 3785, 3961, 4141 .. etc
// Test
// k = 10
}
}``````

#### Output

`` 1 5 13 25 41 61 85 113 145 181``
``````// Swift 4 program for
// Centered square numbers
class SquareNumber
{
func centeredSquareNo(_ k: Int)
{
var n: Int = 1;
// Print all initial k centered square number
while (n <= k)
{
// Centered square
// formula = (n)² + (n-1)²

// CalculatedCentered square numbers
let result: Int = ((n * n) + ((n - 1) * (n - 1)));

// Display calculated result
print(" ", result, terminator: "");
n += 1;
}
}
}
func main()
{
// Centered square numbers
// --------------------------------------
// 1, 5, 13, 25, 41, 61, 85, 113, 145, 181, 221,
// 265, 313, 365, 421, 481, 545, 613, 685, 761,
// 841, 925, 1013, 1105, 1201, 1301, 1405,
// 1513, 1625, 1741, 1861, 1985, 2113, 2245,
// 2381, 2521, 2665, 2813, 2965, 3121, 3281,
// 3445, 3613, 3785, 3961, 4141 .. etc

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

#### Output

``  1  5  13  25  41  61  85  113  145  181``
``````// Kotlin program for
// Centered square numbers
class SquareNumber
{
fun centeredSquareNo(k: Int): Unit
{
var n: Int = 1;
// Print all initial k centered square number
while (n <= k)
{
// Centered square
// formula = (n)² + (n-1)²

// CalculatedCentered square numbers
val result: Int = ((n * n) + ((n - 1) * (n - 1)));

// Display calculated result
print(" " + result);
n += 1;
}
}
}
fun main(args: Array < String > ): Unit
{
// Centered square numbers
// --------------------------------------
// 1, 5, 13, 25, 41, 61, 85, 113, 145, 181, 221,
// 265, 313, 365, 421, 481, 545, 613, 685, 761,
// 841, 925, 1013, 1105, 1201, 1301, 1405,
// 1513, 1625, 1741, 1861, 1985, 2113, 2245,
// 2381, 2521, 2665, 2813, 2965, 3121, 3281,
// 3445, 3613, 3785, 3961, 4141 .. etc

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

#### Output

`` 1 5 13 25 41 61 85 113 145 181``

## Time complexity

The time complexity of the algorithm is `O(k)`, as it performs a constant amount of work for each value from 1 to `k`. The space complexity is constant as well, as it only requires a single variable to store the calculated result.

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