Program to check if N is a Centered nonadecagonal number
Last Updated :
18 Sep, 2022
Given an integer N, the task is to check if it is a Centered nonadecagonal number or not.
Centered nonadecagonal number represents a dot in the centre and other dots surrounding it in successive nonadecagon(19 sided polygon) layers.The first few Centered nonadecagonal numbers are 1, 20, 58, 115, 191, 286
Examples:
Input: N = 20
Output: Yes
Explanation:
20 is the Second Centered nonadecagonal number is 20.
Input: 38
Output: No
Explanation:
38 is not a Centered nonadecagonal number.
Approach: To solve the problem mentioned above we have to know that the Kth term of the Centered nonadecagonal number is given as:
As we have to check that the given number can be expressed as a Centered nonadecagonal number or not. This can be checked by generalizing the formula to:
=>
=>
Finally, check the value of computation using this formula if it is an integer, if yes then it means that N is a Centered nonadecagonal number.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isCenterednonadecagonal( int N)
{
float n = (19
+ sqrt (152 * N + 209))
/ 38;
return (n - ( int )n) == 0;
}
int main()
{
int n = 20;
if (isCenterednonadecagonal(n)) {
cout << "Yes" ;
}
else {
cout << "No" ;
}
return 0;
}
|
Java
class GFG{
static boolean isCenterednonadecagonal( int N)
{
float n = ( float )(( 19 + Math.sqrt( 152 * N +
209 )) / 38 );
return (n - ( int )n) == 0 ;
}
public static void main(String[] args)
{
int n = 20 ;
if (isCenterednonadecagonal(n))
{
System.out.print( "Yes" );
}
else
{
System.out.print( "No" );
}
}
}
|
Python3
import math
def isCenterednonadecagonal(N):
n = ( 19 + math.sqrt( 152 * N +
209 )) / 38 ;
return (n - int (n)) = = 0 ;
n = 20 ;
if (isCenterednonadecagonal(n)):
print ( "Yes" );
else :
print ( "No" );
|
C#
using System;
class GFG{
static bool isCenterednonadecagonal( int N)
{
float n = ( float )((19 + Math.Sqrt(152 * N +
209)) / 38);
return (n - ( int )n) == 0;
}
public static void Main()
{
int n = 20;
if (isCenterednonadecagonal(n))
{
Console.Write( "Yes" );
}
else
{
Console.Write( "No" );
}
}
}
|
Javascript
<script>
function isCenterednonadecagonal(N)
{
var n = (19
+ Math.sqrt(152 * N + 209))
/ 38;
return (n - parseInt(n)) == 0;
}
var n = 20;
if (isCenterednonadecagonal(n)) {
document.write( "Yes" );
}
else {
document.write( "No" );
}
</script>
|
Time Complexity: O(logn) for given n, because it using inbuilt sqrt function
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...