📜  中心平方数

📅  最后修改于: 2021-04-23 06:47:02             🧑  作者: Mango

给定数字n ,任务是找到n中心平方数。

中心正方形数是一个中心的图形数,它在一个正方形中给出一个点的数量,其中一个点位于中心,而围绕中心点的所有其他点则位于连续的正方形层中。可以使用公式n 2 +(n-1) 2来计算N中心平方数。

中心平方数

例子 :

Input : n = 2
Output : 5

Input : n = 9
Output : 145
  1. 查找第n个中心平方数
    如果仔细观察,我们会注意到第n个中心平方数可以看作是两个连续平方数的总和(1点,4点,9点,16点等)。

    我们可以使用以下公式找到第n个中心平方数。

    下面是实现:

    C++
    // C++ program to find nth
    // Centered square number.
    #include 
      
    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;
    }


    Java
    // 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.


    Python3
    # 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))


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


    PHP


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


    Java
    // Java Code implementation of the above approach
    class GFG {
          
        static int centeredSquare_number(int N) 
        {     
            float n = (9 + (float)Math.sqrt(36*N+45))/18;  
            if (n - (int) n == 0)
                return 1;
            else
                return 0;
        } 
          
        // Driver code
        public static void main (String[] args) 
        { 
            int i = 13;
            System.out.println(centeredSquare_number(i));
        } 
          
    }
      
    // This code is contributed by Yash_R


    Python3
    # Python3 implementation of the above approach
    from math import sqrt
      
    def centeredSquare_number(N) : 
      
        n = (9 + sqrt(36 * N + 45))/18; 
        if (n - int(n)) == 0 :
            return 1
        else :
            return 0
      
    # Driver Code
    if __name__ == "__main__" : 
      
        i = 13;
        print(centeredSquare_number(i));
      
    # This code is contributed by Yash_R


    C#
    // C# Code implementation of the above approach
    using System;
      
    class GFG {
          
        static int centeredSquare_number(int N) 
        {     
            float n = (9 + (float)Math.Sqrt(36 * N + 45))/18;  
            if (n - (int) n == 0)
                return 1;
            else
                return 0;
        } 
          
        // Driver code
        public static void Main (String[] args) 
        { 
            int i = 13;
            Console.WriteLine(centeredSquare_number(i));
        } 
          
    }
      
    // This code is contributed by Yash_R


    输出 :

    7th Centered square number: 85
    
  2. 检查N是否为中心平方数:
    • 前几个居中平方数字是:
    • 由于第n个中心平方数为
      H(n) = n * n + ((n - 1) * (n - 1))
    • 该公式表明,第n个中心平方数的数量平方依赖于n。因此,尝试找到N = H(n)方程的正整数根。
      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; 
      
    • 获得n后,检查它是否为整数。如果n – floor(n)为0,则n为整数。

    下面是上述方法的实现:

    CPP

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

    Java

    // Java Code implementation of the above approach
    class GFG {
          
        static int centeredSquare_number(int N) 
        {     
            float n = (9 + (float)Math.sqrt(36*N+45))/18;  
            if (n - (int) n == 0)
                return 1;
            else
                return 0;
        } 
          
        // Driver code
        public static void main (String[] args) 
        { 
            int i = 13;
            System.out.println(centeredSquare_number(i));
        } 
          
    }
      
    // This code is contributed by Yash_R
    

    Python3

    # Python3 implementation of the above approach
    from math import sqrt
      
    def centeredSquare_number(N) : 
      
        n = (9 + sqrt(36 * N + 45))/18; 
        if (n - int(n)) == 0 :
            return 1
        else :
            return 0
      
    # Driver Code
    if __name__ == "__main__" : 
      
        i = 13;
        print(centeredSquare_number(i));
      
    # This code is contributed by Yash_R
    

    C#

    // C# Code implementation of the above approach
    using System;
      
    class GFG {
          
        static int centeredSquare_number(int N) 
        {     
            float n = (9 + (float)Math.Sqrt(36 * N + 45))/18;  
            if (n - (int) n == 0)
                return 1;
            else
                return 0;
        } 
          
        // Driver code
        public static void Main (String[] args) 
        { 
            int i = 13;
            Console.WriteLine(centeredSquare_number(i));
        } 
          
    }
      
    // This code is contributed by Yash_R
    
    输出:
    0
    

参考: https //en.wikipedia.org/wiki/Centered_square_number