给定数字N ,任务是检查N是否是五边形数字。如果数字N是五边形数字,则打印“是”,否则打印“否” 。
Pentadecagon Number is a 15-sided polygon..The first few Pentadecagon numbers are 1, 15, 42, 82, 135, 201, …
例子:
Input: N = 15
Output: Yes
Explanation:
Second Pentadecagon number is 15.
Input: N = 30
Output: No
方法:
- 五角大楼数的第K个项为
- 因为我们必须检查给定的数字是否可以表示为五边形数。可以检查为:
=>
=>
- 如果使用上述公式计算出的K的值为整数,则N为五边形数。
- 其他N不是五边形编号。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to check if N is a
// Pentadecagon number
bool isPentadecagon(int N)
{
float n
= (11 + sqrt(104 * N + 121))
/ 26;
// Condition to check if the
// number is a Pentadecagon number
return (n - (int)n) == 0;
}
// Driver Code
int main()
{
// Given Number
int N = 15;
// Function call
if (isPentadecagon(N)) {
cout << "Yes";
}
else {
cout << "No";
}
return 0;
}
Java
// Java program for the above approach
import java.io.*;
import java.util.*;
class GFG {
// Function to check if N is
// a pentadecagon number
public static boolean isPentadecagon(int N)
{
double n = (11 + Math.sqrt(104 * N +
121)) / 26;
// Condition to check if the number
// is a pentadecagon number
return (n - (int)n) == 0;
}
// Driver code
public static void main(String[] args)
{
// Given Number
int N = 15;
// Function call
if (isPentadecagon(N))
{
System.out.println("Yes");
}
else
{
System.out.println("No");
}
}
}
// This code is contributed by coder001
Python3
# Python3 program for the above approach
from math import sqrt
# Function to check if N is a
# pentadecagon number
def isPentadecagon(N):
n = (11 + sqrt(104 * N + 121)) / 26;
# Condition to check if the
# number is a pentadecagon number
return (n - int(n) == 0);
# Driver Code
if __name__ == "__main__":
# Given number
N = 15;
# Function call
if (isPentadecagon(N)):
print("Yes");
else :
print("No");
# This code is contributed by AnkitRai01
C#
// C# program for the above approach
using System;
class GFG {
// Function to check if N is
// a pentadecagon number
public static bool isPentadecagon(int N)
{
double n = (11 + Math.Sqrt(104 * N +
121)) / 26;
// Condition to check if the number
// is a pentadecagon number
return (n - (int)n) == 0;
}
// Driver code
public static void Main(String[] args)
{
// Given Number
int N = 15;
// Function call
if (isPentadecagon(N))
{
Console.WriteLine("Yes");
}
else
{
Console.WriteLine("No");
}
}
}
// This code is contributed by Amit Katiyar
Javascript
输出:
Yes