📜  程序检查N是否是四角形数

📅  最后修改于: 2021-04-21 21:40:43             🧑  作者: Mango

给定整数N ,任务是检查N是否是四角形数。如果数字N是四边形数字,则打印“是”,否则打印“否”

例子:

方法:

  1. 十四角数的第K项为
    K^{th} Term = \frac{12*K^{2} - 10*K}{2}
  2. 因为我们必须检查给定的数字是否可以表示为四角形数。可以检查为:
  1. 如果使用上述公式计算出的K的值为整数,则N为四方对角数。
  2. 其他N不是四角形数。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to check if N is a
// Tetradecagonal Number
bool istetradecagonal(int N)
{
    float n
        = (10 + sqrt(96 * N + 100))
          / 24;
 
    // Condition to check if the
    // number is a tetradecagonal number
    return (n - (int)n) == 0;
}
 
// Driver Code
int main()
{
    // Given Number
    int N = 11;
 
    // Function call
    if (istetradecagonal(N)) {
        cout << "Yes";
    }
    else {
        cout << "No";
    }
    return 0;
}


Java
// Java program for the above approach
import java.lang.Math;
 
class GFG{
     
// Function to check if N is a
// tetradecagonal number
public static boolean istetradecagonal(int N)
{
    double n = (10 + Math.sqrt(96 * N +
                               100)) / 24;
     
    // Condition to check if the number
    // is a tetradecagonal number
    return (n - (int)n) == 0;
}
 
// Driver Code   
public static void main(String[] args)
{
         
    // Given number
    int N = 11;
     
    // Function call
    if (istetradecagonal(N))
    {
        System.out.println("Yes");
    }
    else
    {
        System.out.println("No");
    }
}
}
 
// This code is contributed by divyeshrabadiya07


Python3
# Python3 program for the above approach
import math
 
# Function to check if N is a
# Tetradecagonal Number
def istetradecagonal(N):
     
    n = (10 + math.sqrt(96 * N + 100)) / 24
     
    # Condition to check if the
    # number is a tetradecagonal number
    if (n - int(n)) == 0:
        return True
         
    return False
 
# Driver Code
 
# Given Number
N = 11
 
# Function call
if (istetradecagonal(N)):
    print("Yes")
else:
    print("No")
 
# This code is contributed by shubhamsingh10


C#
// C# program for the above approach
using System;
 
class GFG{
     
// Function to check if N is a
// tetradecagonal number
public static bool istetradecagonal(int N)
{
    double n = (10 + Math.Sqrt(96 * N +
                               100)) / 24;
         
    // Condition to check if the number
    // is a tetradecagonal number
    return (n - (int)n) == 0;
}
     
// Driver Code
static public void Main ()
{
             
    // Given number
    int N = 11;
         
    // Function call
    if (istetradecagonal(N))
    {
        Console.Write("Yes");
    }
    else
    {
        Console.Write("No");
    }
}
}
 
// This code is contributed by shubhamsingh10


Javascript


输出:
No