Given an integer N, the task is to check if it is a Centered Octadecagonal number or not. Print “yes” if it is otherwise output is “no”.
Centered Octadecagonal number represents a dot in the centre and others dot are arranged around it in successive layers of octadecagon(18 sided polygon). The first few Centered Octadecagonal numbers are 1, 19, 55, 109, 181, 271, 379, …
Examples:
Input: N = 19
Output: Yes
Explanation:
19 is the Second Centered Octadecagonal number is 19.
Input: 38
Output: No
Explanation:
38 is not a Centered Octadecagonal number.
Approach: To solve the problem mentioned above we know that the Kth term of the Centered Octadecagonal number is given as: 
As we have to check that the given number can be expressed as a Centered Octadecagonal number or not. This can be checked by generalizing the equation as:
=> 
=> 
Finally, check the value of computation using this formula if it is an integer, then it means that N is a Centered Octadecagonal number.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isCenteredOctadecagonal( int N)
{
float n = (9 + sqrt (36 * N + 45)) / 18;
return (n - ( int )n) == 0;
}
int main()
{
int n = 19;
if (isCenteredOctadecagonal(n)) {
cout << "Yes" ;
}
else {
cout << "No" ;
}
return 0;
}
|
Java
import java.lang.Math;
class GFG{
public static boolean isCenteredOctadecagonal( int N)
{
double n = ( 9 + Math.sqrt( 36 * N + 45 )) / 18 ;
return (n - ( int )n) == 0 ;
}
public static void main(String[] args)
{
int n = 19 ;
if (isCenteredOctadecagonal(n))
{
System.out.println( "Yes" );
}
else
{
System.out.println( "No" );
}
}
}
|
Python3
import math
def isCenteredOctadecagonal(N):
n = ( 9 + math.sqrt( 36 * N + 45 )) / 18 ;
return (n - int (n)) = = 0
if __name__ = = '__main__' :
n = 19
if isCenteredOctadecagonal(n):
print ( 'Yes' )
else :
print ( 'No' )
|
C#
using System;
class GFG{
static bool isCenteredOctadecagonal( int N)
{
double n = (9 + Math.Sqrt(36 * N + 45)) / 18;
return (n - ( int )n) == 0;
}
static public void Main ()
{
int n = 19;
if (isCenteredOctadecagonal(n))
{
Console.Write( "Yes" );
}
else
{
Console.Write( "No" );
}
}
}
|
Javascript
<script>
function isCenteredOctadecagonal(N)
{
let n = parseInt((9 + Math.sqrt(36 * N + 45)) / 18);
return (n - parseInt(n)) == 0;
}
let n = 19;
if (isCenteredOctadecagonal(n))
{
document.write( "Yes" );
}
else
{
document.write( "No" );
}
</script>
|
Time Complexity: O(logn) for given n, as it is using inbuilt sqrt function
Auxiliary Space: O(1)