📜  程序检查N是否为居中立方数

📅  最后修改于: 2021-05-07 00:58:21             🧑  作者: Mango

给定数字N ,任务是检查N是否为中心立方数。

例子:

方法:想法是从一个迭代开始,并检查第i项是否等于N。

  1. 中心立方数的第N项由下式给出(2 * N + 1) * ( N^2 + N + 1)
  2. 从1开始运行循环,以找到i居中的立方体编号。
  3. 检查第i个项是否等于N。如果相等,则返回true。
  4. 如果第i个项大于N,则返回false。

下面是上述方法的实现:

C++
// C++ program to check if N
// is a centered cubic number
#include 
using namespace std;
 
// Function to check if the number N
// is a centered cubic number
bool isCenteredcube(int N)
{
// Iterating from 1
    int i = 1;
 
// Infinite loop
    while (true) {
 
        // Finding ith_term
        int ith_term = (2 * i + 1)
* (i * i + i + 1);
 
        // Checking if the number N
        // is a Centered cube number
        if (ith_term == N) {
            return true;
        }
 
        // If ith_term > N then
        // N is not a Centered cube number
        if (ith_term > N) {
            return false;
        }
 
        // Incrementing i
        i++;
    }
}
 
// Driver code
int main()
{
    int N = 9;
 
    // Function call
    if (isCenteredcube(N)) {
        cout << "Yes";
    }
    else {
        cout << "No";
    }
    return 0;
}


Java
// Java program to check if N
// is a centered cubic number
class GFG{
 
// Function to check if N
// is a centered cubic number
static boolean isCenteredcube(int N)
{
     
    // Iterating from 1
    int i = 1;
     
    // Infinite loop
    while (true)
    {
         
        // Finding ith_term
        int ith_term = (2 * i + 1) *
                       (i * i + i + 1);
 
        // Checking if the number N
        // is a centered cube number
        if (ith_term == N)
        {
            return true;
        }
 
        // If ith_term > N then N is 
        // not a centered cube number
        if (ith_term > N)
        {
            return false;
        }
 
        // Incrementing i
        i++;
    }
}
 
// Driver code
public static void main(String[] args)
{
    int N = 9;
 
    // Function call
    if (isCenteredcube(N))
    {
        System.out.println("Yes");
    }
    else
    {
        System.out.println("No");
    }
}
}
 
// This code is contributed by shubham


Python3
# Python3 program to check if N
# is a centered cubic number
 
# Function to check if N
# is a centered cubic number
def isCenteredcube(N):
 
    # Iterating from 1
    i = 1;
     
    # Infinite loop
    while (True):
     
        # Finding ith_term
        ith_term = ((2 * i + 1) *
                    (i * i + i + 1));
 
        # Checking if the number N
        # is a centered cube number
        if (ith_term == N):
            return True;
         
        # If ith_term > N then N is
        # not a centered cube number
        if (ith_term > N):
            return False;
         
        # Incrementing i
        i += 1;
     
# Driver code
N = 9;
 
# Function call
if (isCenteredcube(N)):
    print("Yes");
else:
    print("No");
 
# This code is contributed by Code_Mech


C#
// C# program to check if N
// is a centered cubic number
using System;
class GFG{
 
// Function to check if N
// is a centered cubic number
static Boolean isCenteredcube(int N)
{
     
    // Iterating from 1
    int i = 1;
     
    // Infinite loop
    while (true)
    {
         
        // Finding ith_term
        int ith_term = (2 * i + 1) *
                       (i * i + i + 1);
 
        // Checking if the number N
        // is a centered cube number
        if (ith_term == N)
        {
            return true;
        }
 
        // If ith_term > N then N is
        // not a centered cube number
        if (ith_term > N)
        {
            return false;
        }
 
        // Incrementing i
        i++;
    }
}
 
// Driver code
public static void Main()
{
    int N = 9;
     
    // Function call
    if (isCenteredcube(N))
    {
        Console.WriteLine("Yes");
    }
    else
    {
        Console.WriteLine("No");
    }
}
}
 
// This code is contributed by shivanisinghss2110


Javascript


输出:
Yes

时间复杂度: O(N)。