Given a number n , the task is to find nth Centered Square Number.
Centered Square Number is a centered figurate number that gives the number of dots in a square with a dot in the center and all other dots surrounding the center dot in successive square layers. Nth Centered square number can be calculated by using formula n2 + (n-1)2.
Examples :
Input : n = 2 Output : 5 Input : n = 9 Output : 145
- Finding n-th Centered Square Number
If we take a closer look, we can notice that the n-th Centered Square Number can be seen as the sum of two consecutive square numbers (1 dot, 4 dots, 9 dots, 16 dots, etc).We can find n-th Centered Square Number using below formula.
n-th Centered Square Number = n2 + (n-1)2
Below is the implementation :
C++
// C++ program to find nth// Centered square number.#include <bits/stdc++.h>ÂÂusingnamespacestd;ÂÂ// Function to calculate Centered// square number functionintcentered_square_num(intn){   Â// Formula to calculate nth   Â// Centered square number   Âreturnn * n + ((n - 1) * (n - 1));}ÂÂ// Driver Codeintmain(){   Âintn = 7;   Âcout << n <<"th Centered square number: ";   Âcout << centered_square_num(n);   Âreturn0;}Java
// Java program to find nth Centered square// numberimportjava.io.*;ÂÂclassGFG {   Â// Function to calculate Centered   Â// square number function   Âstaticintcentered_square_num(intn)   Â{       Â// Formula to calculate nth       Â// Centered square number       Âreturnn * n + ((n -1) * (n -1));   Â}    Â   Â// Driver Code   Âpublicstaticvoidmain (String[] args)   Â{       Âintn =7;       ÂSystem.out.print( n +"th Centered"                      Â+" square number: "                Â+ centered_square_num(n));   Â}}ÂÂ// This code is contributed by anuj_67.Python3
# Python program to find nth# Centered square number.ÂÂÂÂ# Function to calculate Centered# square number functiondefcentered_square_num(n):   Â# Formula to calculate nth   Â# Centered square number   Âreturnn*n+((n-1)*(n-1))ÂÂÂÂ# Driver Coden=7print("%sth Centered square number: "%n,                 Âcentered_square_num(n))   ÂÂC#
// C# program to find nth// Centered square number.usingSystem;ÂÂpublicclassGFG {   Â// Function to calculate Centered   Â// square number function   Âstaticintcentered_square_num(intn)   Â{       Â// Formula to calculate nth       Â// Centered square number       Âreturnn * n + ((n - 1) * (n - 1));   Â}    Â   Â// Driver Code   ÂstaticpublicvoidMain (){   Âintn = 7;   ÂConsole.WriteLine( n +"th Centered"                   Â+" square number: "              Â+ centered_square_num(n));   Â}}ÂÂ// This code is contributed by anuj_67.PHP
<?php// PHP program to find nth// Centered square numberÂÂ// Function to calculate Centered// square number functionfunctioncentered_square_num($n){   Â// Formula to calculate nth   Â// Centered square number   Âreturn$n*$n+ (($n- 1) *                     Â($n- 1));}ÂÂ// Driver Code$n= 7;echo$n,"th Centered square number: ";echocentered_square_num($n);ÂÂ// This code is contributed by anuj_67.?>Output :
7th Centered square number: 85
- Check if N is centred-square-number or not:
- The first few centered-square-number numbers are:
1,5,13,25,41,61,85,113,145,181,…………
- Since the nth centered-square-number number is given by
H(n) = n * n + ((n - 1) * (n - 1))
- The formula indicates that the n-th centred-square-number number depends quadratically on n. Therefore, try to find the positive integral root of N = H(n) equation.
H(n) = nth centered-square-number number N = Given Number Solve for n: H(n) = N n * n + ((n - 1) * (n - 1)) = N Applying Shridharacharya Formula The positive root of equation (i) n = (9 + sqrt(36*N+45))/18;
- After obtaining n, check if it is an integer or not. n is an integer if n – floor(n) is 0.
Below is the implementation of the above approach:
CPP
#include <bits/stdc++.h>usingnamespacestd;ÂÂboolcenteredSquare_number(intN)Â{Â Â Â Â ÂÂ Â Â Âfloatn = (9 +sqrt(36*N+45))/18;Â ÂÂ Â Â Âreturn(n - (int) n) == 0;Â}ÂÂÂintmain()Â{ÂÂ Â Â Âinti = 13;Â Â Â Âcout<<centeredSquare_number(i);Â Â Â Âreturn0;Â}ÂJava
// Java Code implementation of the above approachclassGFG {    Â   ÂstaticintcenteredSquare_number(intN)   Â{    Â       Âfloatn = (9+ (float)Math.sqrt(36*N+45))/18; Â       Âif(n - (int) n ==0)           Âreturn1;       Âelse           Âreturn0;   Â}    Â   Â// Driver code   Âpublicstaticvoidmain (String[] args)   Â{       Âinti =13;       ÂSystem.out.println(centeredSquare_number(i));   Â}    ÂÂ}ÂÂ// This code is contributed by Yash_RPython3
# Python3 implementation of the above approachfrommathimportsqrtÂÂdefcenteredSquare_number(N) :ÂÂÂÂ Â Â Ân=(9+sqrt(36*N+45))/18;ÂÂ Â Â Âif(n-int(n))==0:Â Â Â Â Â Â Â Âreturn1Â Â Â Âelse:Â Â Â Â Â Â Â Âreturn0ÂÂ# Driver Codeif__name__=="__main__":ÂÂÂÂ Â Â Âi=13;Â Â Â Âprint(centeredSquare_number(i));ÂÂ# This code is contributed by Yash_RC#
// C# Code implementation of the above approachusingSystem;ÂÂclassGFG {    Â   ÂstaticintcenteredSquare_number(intN)   Â{    Â       Âfloatn = (9 + (float)Math.Sqrt(36 * N + 45))/18; Â       Âif(n - (int) n == 0)           Âreturn1;       Âelse           Âreturn0;   Â}    Â   Â// Driver code   ÂpublicstaticvoidMain (String[] args)   Â{       Âinti = 13;       ÂConsole.WriteLine(centeredSquare_number(i));   Â}    ÂÂ}ÂÂ// This code is contributed by Yash_ROutput:0
- The first few centered-square-number numbers are:
Reference: https://en.wikipedia.org/wiki/Centered_square_number
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

