Centered Square Number

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 numberreturnn * 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 functionstaticintcentered_square_num(intn){// Formula to calculate nth// Centered square numberreturnn * n + ((n -1) * (n -1));}// Driver Codepublicstaticvoidmain (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 numberreturnn*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 functionstaticintcentered_square_num(intn){// Formula to calculate nth// Centered square numberreturnn * n + ((n - 1) * (n - 1));}// Driver CodestaticpublicvoidMain (){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 numberreturn$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;elsereturn0;}// Driver codepublicstaticvoidmain (String[] args){inti =13;System.out.println(centeredSquare_number(i));}}// This code is contributed by Yash_RPython3
# Python3 implementation of the above approachfrommathimportsqrtdefcenteredSquare_number(N) :n=(9+sqrt(36*N+45))/18;if(n-int(n))==0:return1else: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;elsereturn0;}// Driver codepublicstaticvoidMain (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
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!




