📜  程序检查N是否为三边形数字

📅  最后修改于: 2021-04-23 22:26:14             🧑  作者: Mango

给定整数N ,任务是检查它是否是三边形数字。

例子:

方法:

  1. 三角数的第K项为
    K^{th} Term = \frac{11*K^{2} - 9*K}{2}
  2. 因为我们必须检查给定的数字是否可以表示为Tridecagon数。可以检查如下–
  1. 最后,检查使用此公式计算的值是一个整数,这意味着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