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:
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++
// 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; } |
Java
// 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 |
Python3
# 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 |
C#
// 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 |
Javascript
<script> // Javascript implementation to check that // a number is a Centered Octadecagonal // number or not // Function to check that the // number is a Centered // Octadecagonal number function isCenteredOctadecagonal(N) { // Implement the formula generated let n = parseInt((9 + Math.sqrt(36 * N + 45)) / 18); // Condition to check if the // number is a Centered // Octadecagonal number return (n - parseInt(n)) == 0; } // Driver Code let n = 19; // Function call if (isCenteredOctadecagonal(n)) { document.write( "Yes" ); } else { document.write( "No" ); } // This code is contributed by souravmahato348 </script> |
Output:
Yes
Time Complexity: O(logn) for given n, as it is using inbuilt sqrt function
Auxiliary Space: O(1)
Contact Us