📜  居中立方数

📅  最后修改于: 2021-10-23 09:00:07             🧑  作者: Mango

给定一个数 n,找出第n 个居中的立方数。
中心立方数计算由在 3D 中由同心立方层包围的点形成的点数,其中 i 2个点位于第 i 层的方形面上。来源[维基]。请查看此图像以获得更清晰的信息。
前几个中心立方数是:
1, 9, 35, 91, 189, 341, 559, 855, 1241, 172…………………………
例子 :

Input :  n = 1
Output : 9

Input  : n = 7
Output : 855

第 n 个中心立方数的数学公式由下式给出:

n-th Centered Cube Number = (2n + 1)(n2 + n + 1)

下面是上述公式的基本实现:

C++
// Program to find nth Centered cube
// number
#include 
using namespace std;
 
// Function to find
// Centered cube number
int centered_cube(int n)
{
    // Formula to calculate nth
    // Centered cube number &
    // return it into main function.
    return (2 * n + 1) * ( n * n + n + 1);
}
 
// Driver Code
int main()
{
    int n = 3;
    cout << n << "th Centered cube number: ";
    cout << centered_cube(n);
    cout << endl;
 
    n = 10;
    cout << n << "th Centered cube number: ";
    cout << centered_cube(n);
    return 0;
}


Java
// Java Program to find nth Centered
// cube number
import java.io.*;
 
class GFG {
     
    // Function to find
    // Centered cube number
    static int centered_cube(int n)
    {
        // Formula to calculate nth
        // Centered cube number &
        // return it into main function.
        return (2 * n + 1) * ( n * n + n + 1);
    }
     
    // Driver code
    public static void main (String[] args)
    {
        int n = 3;
        System.out.print (n + "th Centered"
                         + " cube number: ");
        System.out.println (centered_cube(n));
     
        n = 10;
        System.out.print ( n + "th Centered"
                         + " cube number: ");
        System.out.println (centered_cube(n));
    }
}
 
// This code is contributed by m_kit.


Python3
# Python 3 Program to find
# nth Centered cube number
 
# Centered cube
# number function
def centered_cube(n) :
     
    # Formula to calculate
    # nth Centered cube
    # number return it
    # into main function.
    return (2 * n + 1) * (
                n * n + n + 1)
 
# Driver Code
if __name__ == '__main__' :
         
    n = 3
    print(n,"th Centered cube " +
                    "number : " ,
                centered_cube(n))
 
    n = 10
    print(n,"th Centered cube " +
                    "number : " ,
                centered_cube(n))
 
# This code is contributed by ajit


C#
// C# Program to find nth
// Centered cube number
using System;
 
class GFG
{
     
    // Function to find
    // Centered cube number
    static int centered_cube(int n)
    {
        // Formula to calculate
        // nth Centered cube
        // number & return it
        // into main function.
        return (2 * n + 1) *
               (n * n + n + 1);
    }
     
    // Driver code
    static public void Main ()
    {
        int n = 3;
        Console.Write(n + "th Centered" +
                       " cube number: ");
    Console.WriteLine (centered_cube(n));
     
        n = 10;
        Console.Write( n + "th Centered" +
                        " cube number: ");
        Console.WriteLine(centered_cube(n));
    }
}
 
// This code is contributed by aj_36


PHP


Javascript


输出 :

3th Centered cube number: 91
 10th Centered cube number: 2331

时间复杂度: O(1)
辅助空间: O(1)