📜  程序检查N是否为非对角数

📅  最后修改于: 2021-04-29 05:49:29             🧑  作者: Mango

给定数字N ,任务是检查N是否为非对角数。如果数字N是非对角线数字,则打印“是”,否则打印“否”

例子:

方法:

1.在K的九边形数的术语被给定为
K^{th} Term = \frac{7*K^{2} - 5*K}{2}

2.由于我们必须检查给定的数字是否可以表示为非对角线数字。可以检查为:

3.如果使用上述公式计算的K值为整数,则N为非对角数。

4.其他N不是非对角数。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to check if N is a
// is a Nonagonal Number
bool isnonagonal(int N)
{
    float n
        = (5 + sqrt(56 * N + 25))
          / 14;
 
    // Condition to check if the
    // number is a nonagonal number
    return (n - (int)n) == 0;
}
 
// Driver Code
int main()
{
    // Given Number
    int N = 9;
 
    // Function call
    if (isnonagonal(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
// nonagonal number
public static boolean isnonagonal(int N)
{
    double n = (5 + Math.sqrt(56 * N + 25)) / 14;
     
    // Condition to check if the
    // number is a nonagonal number
    return (n - (int)n) == 0;
}
 
// Driver code    
public static void main(String[] args)
{
         
    // Given number
    int N = 9;
     
    // Function call
    if (isnonagonal(N))
    {
        System.out.println("Yes");
    }
    else
    {
        System.out.println("No");
    }
}
}
 
// This code is contributed by divyeshrabadiya07


Python3
# Python3 program for the above approach
 
# Function to check if N is a
# nonagonal number
def isnonagonal(N):
    n = (5 + pow((56 * N + 25), 1 / 2)) / 14;
 
    # Condition to check if the
    # number is a nonagonal number
    return (n - int(n)) == 0;
 
# Driver code
if __name__ == '__main__':
 
    # Given number
    N = 9;
 
    # Function call
    if (isnonagonal(N)):
        print("Yes");
    else:
        print("No");
 
# This code is contributed by Rajput-Ji


C#
// C# program for the above approach
using System;
 
class GFG{
     
// Function to check if N is a
// nonagonal number
public static bool isnonagonal(int N)
{
    double n = (5 + Math.Sqrt(56 * N + 25)) / 14;
     
    // Condition to check if the
    // number is a nonagonal number
    return (n - (int)n) == 0;
}
 
// Driver code    
public static void Main(string[] args)
{
         
    // Given number
    int N = 9;
     
    // Function call
    if (isnonagonal(N))
    {
        Console.Write("Yes");
    }
    else
    {
        Console.Write("No");
    }
}
}
 
// This code is contributed by rutvik_56


Javascript


输出:
Yes

时间复杂度: O(1)

辅助空间: O(1)