Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

Program to check if N is a Centered nonadecagonal number

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

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

=> N = \frac {19 * k ^ {2} - 19 * k + 2} {2}
=> K = \frac{19 + \sqrt{152 * N + 209}} {38}
 

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




// C++ implementation to check that
// a number is a Centered
// nonadecagonal number or not
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to check that the
// number is a Centered
// nonadecagonal number
bool isCenterednonadecagonal(int N)
{
    float n = (19
               + sqrt(152 * N + 209))
              / 38;
 
    // Condition to check if the
    // number is a Centered
    // nonadecagonal number
    return (n - (int)n) == 0;
}
 
// Driver Code
int main()
{
    int n = 20;
 
    // Function call
    if (isCenterednonadecagonal(n)) {
        cout << "Yes";
    }
    else {
        cout << "No";
    }
    return 0;
}

Java




// Java implementation to check that
// a number is a Centered
// nonadecagonal number or not
class GFG{
 
// Function to check that the
// number is a Centered
// nonadecagonal number
static boolean isCenterednonadecagonal(int N)
{
    float n = (float)((19 + Math.sqrt(152 * N +
                                      209)) / 38);
 
    // Condition to check if the
    // number is a Centered
    // nonadecagonal number
    return (n - (int)n) == 0;
}
 
// Driver Code
public static void main(String[] args)
{
    int n = 20;
 
    // Function call
    if (isCenterednonadecagonal(n))
    {
        System.out.print("Yes");
    }
    else
    {
        System.out.print("No");
    }
}
}
 
// This code is contributed by sapnasingh4991

Python3




# Python3 implementation to check that
# a number is a Centered
# nonadecagonal number or not
import math
 
# Function to check that the
# number is a Centered
# nonadecagonal number
def isCenterednonadecagonal(N):
 
    n = (19 + math.sqrt(152 * N +
                        209)) / 38;
 
    # Condition to check if the
    # number is a Centered
    # nonadecagonal number
    return (n - int(n)) == 0;
 
# Driver Code
n = 20;
 
# Function call
if (isCenterednonadecagonal(n)):
    print("Yes");
 
else:
    print("No");
 
# This code is contributed by Code_Mech

C#




// C# implementation to check that
// a number is a Centered
// nonadecagonal number or not
using System;
class GFG{
 
// Function to check that the
// number is a Centered
// nonadecagonal number
static bool isCenterednonadecagonal(int N)
{
    float n = (float)((19 + Math.Sqrt(152 * N +
                                      209)) / 38);
 
    // Condition to check if the
    // number is a Centered
    // nonadecagonal number
    return (n - (int)n) == 0;
}
 
// Driver Code
public static void Main()
{
    int n = 20;
 
    // Function call
    if (isCenterednonadecagonal(n))
    {
        Console.Write("Yes");
    }
    else
    {
        Console.Write("No");
    }
}
}
 
// This code is contributed by Nidhi_biet

Javascript




<script>
 
// Javascript implementation to check that
// a number is a Centered
// nonadecagonal number or not
 
// Function to check that the
// number is a Centered
// nonadecagonal number
function isCenterednonadecagonal(N)
{
    var n = (19
               + Math.sqrt(152 * N + 209))
              / 38;
 
    // Condition to check if the
    // number is a Centered
    // nonadecagonal number
    return (n - parseInt(n)) == 0;
}
 
// Driver Code
var n = 20;
 
// Function call
if (isCenterednonadecagonal(n)) {
    document.write("Yes");
}
else {
    document.write("No");
}
 
 
</script>

Output: 

Yes

 

Time Complexity: O(logn) for given n, because it using inbuilt sqrt function
Auxiliary Space: O(1)


My Personal Notes arrow_drop_up
Last Updated : 18 Sep, 2022
Like Article
Save Article
Similar Reads
Related Tutorials