给定数字N ,任务是检查N是否为非对角数。如果数字N是非对角线数字,则打印“是”,否则打印“否” 。
Nonagonal Number is a figurate number that extends the concept of triangular and square numbers to the Nonagon. Specifically, the nth Nonagonal Numbers count the number of dots in a pattern of n nested nonagons(9 sided polygon), all sharing a common corner, where the ith nonagon in the pattern has sides made of i dots spaced one unit apart from each other. The first few Nonagonal Numbers are 1, 9, 24, 46, 75, 111, 154, …
例子:
Input: N = 9
Output: Yes
Explanation:
Second Nonagonal Number is 9.
Input: N = 20
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 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)