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.

Centered-Square-Number



Examples :

Input : n = 2
Output : 5

Input : n = 9
Output : 145
  1. 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++

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // C++ program to find nth
    // Centered square number.
    #include <bits/stdc++.h>
      
    using namespace std;
      
    // Function to calculate Centered
    // square number function
    int centered_square_num(int n)
    {
        // Formula to calculate nth
        // Centered square number
        return n * n + ((n - 1) * (n - 1));
    }
      
    // Driver Code
    int main()
    {
        int n = 7;
        cout << n << "th Centered square number: ";
        cout << centered_square_num(n);
        return 0;
    }

    chevron_right

    
    

    Java

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to find nth Centered square
    // number
    import java.io.*;
      
    class GFG {
      
        // Function to calculate Centered
        // square number function
        static int centered_square_num(int n)
        {
            // Formula to calculate nth
            // Centered square number
            return n * n + ((n - 1) * (n - 1));
        }
          
        // Driver Code
        public static void main (String[] args) 
        {
            int n = 7;
            System.out.print( n + "th Centered"
                           + " square number: "
                     + centered_square_num(n));
        }
    }
      
    // This code is contributed by anuj_67.

    chevron_right

    
    

    Python3

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    # Python program to find nth
    # Centered square number.
      
      
    # Function to calculate Centered
    # square number function
    def centered_square_num(n):
      
        # Formula to calculate nth
        # Centered square number
        return n * n + ((n - 1) * (n - 1))
      
      
    # Driver Code
    n = 7
    print("%sth Centered square number: " %n,
                      centered_square_num(n))
         

    chevron_right

    
    

    C#

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // C# program to find nth
    // Centered square number.
    using System;
      
    public class GFG {
      
        // Function to calculate Centered
        // square number function
        static int centered_square_num(int n)
        {
            // Formula to calculate nth
            // Centered square number
            return n * n + ((n - 1) * (n - 1));
        }
          
        // Driver Code
      
        static public void Main (){
        int n = 7;
        Console.WriteLine( n + "th Centered"
                        + " square number: "
                   + centered_square_num(n));
        }
    }
      
    // This code is contributed by anuj_67.

    chevron_right

    
    

    PHP

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <?php
    // PHP program to find nth
    // Centered square number
      
    // Function to calculate Centered
    // square number function
    function centered_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: ";
    echo centered_square_num($n);
      
    // This code is contributed by anuj_67.
    ?>

    chevron_right

    
    


    Output :

    7th Centered square number: 85
    
  2. 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

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    #include <bits/stdc++.h>
    using namespace std;
      
    bool centeredSquare_number(int N) 
    {     
        float n = (9 + sqrt(36*N+45))/18;  
        return (n - (int) n) == 0; 
      
    int main() 
        int = 13;
        cout<<centeredSquare_number(i);
        return 0; 

    chevron_right

    
    

    Output:

    1
    

Reference: https://en.wikipedia.org/wiki/Centered_square_number



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : vt_m, spp____