给定一个数字N ,任务是检查N是否是一个居中的三边形数字。如果数字N是居中的三边形数字,则打印“是”,否则打印“否” 。
Centered tridecagonal number represents a dot at the center and other dots surrounding the center dot in the successive tridecagonal(13 sided polygon) layer. The first few Centered tridecagonal numbers are 1, 14, 40, 79 …
例子:
Input: N = 14
Output: Yes
Explanation:
Second Centered tridecagonal number is 14.
Input: N = 30
Output: No
方法:
1.中心三边形数的第K个项为
2.由于我们必须检查给定的数字是否可以表示为居中的三边形数字。可以检查如下:
=>
=>
3.如果使用上述公式计算出的K的值为整数,则N为居中的三边形数。
4.否则,数字N不是居中的三边形数字。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to check if the number N
// is a Centered tridecagonal number
bool isCenteredtridecagonal(int N)
{
float n
= (13 + sqrt(104 * N + 65))
/ 26;
// Condition to check if the N
// is a Centered tridecagonal number
return (n - (int)n) == 0;
}
// Driver Code
int main()
{
// Given Number
int N = 14;
// Function call
if (isCenteredtridecagonal(N)) {
cout << "Yes";
}
else {
cout << "No";
}
return 0;
}
Java
// Java program for the above approach
class GFG{
// Function to check if the number N
// is a centered tridecagonal number
static boolean isCenteredtridecagonal(int N)
{
float n = (float) ((13 + Math.sqrt(104 * N +
65)) / 26);
// Condition to check if the N
// is a centered tridecagonal number
return (n - (int)n) == 0;
}
// Driver Code
public static void main(String[] args)
{
// Given Number
int N = 14;
// Function call
if (isCenteredtridecagonal(N))
{
System.out.print("Yes");
}
else
{
System.out.print("No");
}
}
}
// This code is contributed by sapnasingh4991
Python3
# Python3 program for the above approach
import numpy as np
# Function to check if the number N
# is a centered tridecagonal number
def isCenteredtridecagonal(N):
n = (13 + np.sqrt(104 * N + 65)) / 26
# Condition to check if N
# is centered tridecagonal number
return (n - int(n)) == 0
# Driver Code
N = 14
# Function call
if (isCenteredtridecagonal(N)):
print ("Yes")
else:
print ("No")
# This code is contributed by PratikBasu
C#
// C# program for the above approach
using System;
class GFG{
// Function to check if the number N
// is a centered tridecagonal number
static bool isCenteredtridecagonal(int N)
{
float n = (float) ((13 + Math.Sqrt(104 * N +
65)) / 26);
// Condition to check if the N
// is a centered tridecagonal number
return (n - (int)n) == 0;
}
// Driver Code
public static void Main(string[] args)
{
// Given Number
int N = 14;
// Function call
if (isCenteredtridecagonal(N))
{
Console.Write("Yes");
}
else
{
Console.Write("No");
}
}
}
// This code is contributed by rutvik_56
Javascript
输出:
Yes
时间复杂度: O(1)
辅助空间: O(1)