Program to check if N is a Centered Octadecagonal number

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: K^{th} Term = 9*N^{2} + - 9*N + 1

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:

=> N = 9*K^{2} - 9*K + 1

=> K = \frac{9 + \sqrt{36*N + 45}}{18}

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation to check that
// a number is a Centered
// Octadecagonal number or not
  
#include <bits/stdc++.h>
using namespace std;
  
// Function to check that the
// number is a Centered
// Octadecagonal number
bool isCenteredOctadecagonal(int N)
{
  
    // Implement the formula generated
    float n = (9 + sqrt(36 * N + 45)) / 18;
  
    // Condition to check if the
    // number is a Centered
    // Octadecagonal number
    return (n - (int)n) == 0;
}
  
// Driver Code
int main()
{
    int n = 19;
  
    // Function call
    if (isCenteredOctadecagonal(n)) {
        cout << "Yes";
    }
    else {
        cout << "No";
    }
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation to check that 
// a number is a centered 
// octadecagonal number or not 
import java.lang.Math;
  
class GFG{
      
// Function to check that the 
// number is a centered 
// octadecagonal number 
public static boolean isCenteredOctadecagonal(int N) 
      
    // Implement the formula generated 
    double n = (9 + Math.sqrt(36 * N + 45)) / 18
      
    // Condition to check if the 
    // number is a Centered 
    // Octadecagonal number 
    return(n - (int)n) == 0
  
// Driver Code    
public static void main(String[] args)
{
    int n = 19
  
    // Function call 
    if (isCenteredOctadecagonal(n))
    
        System.out.println("Yes");
    
    else
    
        System.out.println("No");
    
}
}
  
// This code is contributed by divyeshrabadiya07

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation to check that 
# a number is a centered octadecagonal
# number or not 
import math
  
# Function to check that the 
# number is a centered 
# octadecagonal number 
def isCenteredOctadecagonal(N):
      
    # Implement the formula generated
    n = (9 + math.sqrt(36 * N + 45)) / 18
      
    # Condition to check if the 
    # number is a centered 
    # octadecagonal number 
    return (n - int(n)) == 0
  
# Driver code 
if __name__=='__main__':
      
    n = 19
      
    # Function call
    if isCenteredOctadecagonal(n):
        print('Yes')
    else:
        print('No')
  
# This code is contributed by rutvik_56

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation to check that
// a number is a centered
// octadecagonal number or not
using System;
  
class GFG{
  
// Function to check that the
// number is a centered
// octadecagonal number
static bool isCenteredOctadecagonal(int N)
{
  
    // Implement the formula generated
    double n = (9 + Math.Sqrt(36 * N + 45)) / 18;
      
    // Condition to check if the
    // number is a Centered
    // octadecagonal number
    return (n - (int)n) == 0;
}
      
// Driver Code
static public void Main ()
{
    int n = 19;
      
    // Function call
    if (isCenteredOctadecagonal(n))
    {
        Console.Write("Yes");
    }
    else
    {
        Console.Write("No");
    }
}
}
  
//This code is contributed by ShubhamCoder

chevron_right


Output:

Yes

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.