Program to check if N is a Centered Pentadecagonal Number
Last Updated :
19 Sep, 2022
Given a number N, the task is to check whether N is a Centered pentadecagonal number or not. If the number N is a Centered Pentadecagonal Number then print “Yes” else print “No”.
Centered Pentadecagonal Number represents a dot in the centre and other dots surrounding it in successive Pentadecagonal(15-sided polygon) layers. The first few Centered pentadecagonal numbers are 1, 16, 46 …
Examples:
Input: N = 16
Output: Yes
Explanation:
Second Centered pentadecagonal number is 16.
Input: N = 20
Output: No
Approach:
1. The Kth term of the Centered pentadecagonal number is given as:
2. As we have to check that the given number can be expressed as a Centered Pentadecagonal Number or not. This can be checked as:
=>
=>
3. If the value of K calculated using the above formula is an integer, then N is a Centered Pentadecagonal Number.
4. Else the number N is not a Centered Pentadecagonal Number.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isCenteredpentadecagonal( int N)
{
float n
= (16 + sqrt (120 * N + 105))
/ 30;
return (n - ( int )n) == 0;
}
int main()
{
int N = 16;
if (isCenteredpentadecagonal(N)) {
cout << "Yes" ;
}
else {
cout << "No" ;
}
return 0;
}
|
Java
class GFG{
static boolean isCenteredpentadecagonal( int N)
{
float n = ( float )( 16 + Math.sqrt( 120 * N +
105 )) / 30 ;
return (n - ( int )n) == 0 ;
}
public static void main(String[] args)
{
int N = 16 ;
if (isCenteredpentadecagonal(N))
{
System.out.println( "Yes" );
}
else
{
System.out.println( "No" );
}
}
}
|
Python3
import math
def isCenteredpentadecagonal(N):
n = ( 16 + math.sqrt( 120 * N + 105 )) / 30
return (n - int (n)) = = 0
N = 16
if isCenteredpentadecagonal(N):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG{
public static bool isCenteredpentadecagonal( int N)
{
double n = (16 + Math.Sqrt(120 * N +
105)) / 30;
return (n - ( int )n) == 0;
}
public static void Main()
{
int N = 16;
if (isCenteredpentadecagonal(N))
{
Console.WriteLine( "Yes" );
}
else
{
Console.WriteLine( "No" );
}
}
}
|
Javascript
<script>
function isCenteredpentadecagonal(N)
{
var n = (16 + Math.sqrt(120 * N +
105)) / 30;
return (n - (parseInt(n))) == 0;
}
var N = 16;
if (isCenteredpentadecagonal(N))
{
document.write( "Yes" );
}
else
{
document.write( "No" );
}
</script>
|
Time Complexity: O(logN) since inbuilt sqrt function has been used
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...