给定一个整数N ,任务是检查它是否是一个十三边形数。
Tridecagon number is a thirteen-sided polygon. The first few Tridecagon numbers are 1, 13, 36, 70, 115, 171, …
例子:
Input: N = 13
Output: Yes
Explanation:
Second Tridecagon number is 13.
Input: N = 30
Output: No
方法:
- 第i个的十三边形数项K被给定为
- 因为我们必须检查给定的数字是否可以表示为十三边形数字。这可以检查如下 –
=>
=>
- 最后,检查使用这个公式计算的值是一个整数,这意味着 N 是一个十三边形数。
下面是上述方法的实现:
C++
// C++ implementation to check that
// a number is a Tridecagon number or not
#include
using namespace std;
// Function to check that the
// number is a Tridecagon number
bool isTridecagon(int N)
{
float n
= (9 + sqrt(88 * N + 81))
/ 22;
// Condition to check if the
// number is a Tridecagon number
return (n - (int)n) == 0;
}
// Driver Code
int main()
{
int i = 13;
// Function call
if (isTridecagon(i)) {
cout << "Yes";
}
else {
cout << "No";
}
return 0;
}
Java
// Java implementation to check that a
// number is a tridecagon number or not
class GFG{
// Function to check that the
// number is a tridecagon number
static boolean isTridecagon(int N)
{
float n = (float) ((9 + Math.sqrt(88 * N +
81)) / 22);
// Condition to check if the
// number is a tridecagon number
return (n - (int)n) == 0;
}
// Driver Code
public static void main(String[] args)
{
int i = 13;
// Function call
if (isTridecagon(i))
{
System.out.print("Yes");
}
else
{
System.out.print("No");
}
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 implementation to check that
# a number is a tridecagon number or not
import math
# Function to check that the
# number is a tridecagon number
def isTridecagon(N):
n = (9 + math.sqrt(88 * N + 81)) / 22
# Condition to check if the
# number is a tridecagon number
return (n - int(n)) == 0
# Driver Code
i = 13
# Function call
if (isTridecagon(i)):
print("Yes")
else:
print("No")
# This code is contributed by divyamohan123
C#
// C# implementation to check that a
// number is a tridecagon number or not
using System;
class GFG{
// Function to check that the
// number is a tridecagon number
static bool isTridecagon(int N)
{
float n = (float)((9 + Math.Sqrt(88 * N +
81)) / 22);
// Condition to check if the
// number is a tridecagon number
return (n - (int)n) == 0;
}
// Driver Code
public static void Main()
{
int i = 13;
// Function call
if (isTridecagon(i))
{
Console.Write("Yes");
}
else
{
Console.Write("No");
}
}
}
// This code is contributed by Code_Mech
Javascript
输出:
Yes
时间复杂度: O(1)
辅助空间: O(1)