给定数字N ,任务是检查N是否为中心立方数。
A centred cubic number counts the number of points which are formed by a point that is surrounded by concentric cubical layers in 3D with i2 points on the square faces of the i-th layer. The first few Centered cube numbers are 1, 9, 35, 91, 189, 341, 559, 855 …
例子:
Input: N = 9
Output: Yes
Explanation:
9 is the second Centered cube number
Input: N = 6
Output: No
方法:想法是从一个迭代开始,并检查第i个项是否等于N。
- 中心立方数的第N个项由下式给出 。
- 从1开始运行循环,以找到第i个居中的立方体编号。
- 检查第i个项是否等于N。如果相等,则返回true。
- 如果第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)。