Centered Square Number


What do you understand by Centered Square Number? Let’s decode in this article.

Firstly, what is square number?

A square number, also known as a perfect square, is a non−negative integer formed by multiplying an integer by itself. A square number, in other words, is the result of multiplying a number by itself.

For example, 1, 4, 9, 16, 25, 36, 49, 64, 81, and 100 are all square numbers.

The sequence of square numbers can be represented by the formula n^2, where n is a positive integer. For instance, the first five square numbers are 1^2 = 1, 2^2 = 4, 3^2 = 9, 4^2 = 16, and 5^2 = 25.

Now, what is centered square number?

A centered square number is a centered figurate number that represents the number of dots in a square that is centered within a larger square, with a constant number of dots separating the two squares along each side.

The first few centered square numbers are: 1,5,13,25,41,..

The formula to calculate the nth centered square number is:

n^2 + (n−1)^2

where n is a positive integer.

Approach

Now, let’s discuss the stepwise approach to convert the discussed logic into code.

  • Specify the value of n, you can also take this as user input.

  • Use the formula n * n + (n−1) * (n−1), to calculate the nth centered square number.

  • Print the calculation to the console.

C++ code Implementation

Example

#include <iostream>
using namespace std;

int main() {
    int n=7, centeredSquare;
    // Calculate the nth centered square number
    centeredSquare = n * n + (n-1) * (n-1);

    cout << "The " << n << "th centered square number is " << centeredSquare << endl;

    return 0;
}

Output

The 7th centered square number is 85

Time Complexity: O(1)

Space Complexity: O(1)

Conclusion

In this article, we have covered what is centered square number and how to calculate nth centered square number. Hope you were able to grasp the concept in a better way and found the article helpful.

Updated on: 23-Aug-2023

64 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements