Open In App

Program to check if N is a Centered Decagonal Number

Last Updated : 19 Sep, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Given an integer N, the task is to check if N is a Centered Decagonal Number or not. If the number N is a Centered Decagonal Number then print “Yes” else print “No”.

Centered Decagonal Number is centered figurative number that represents a decagon with dot in center and all other dot surrounding it in successive Decagonal Number form. The first few Centered decagonal numbers are 1, 11, 31, 61, 101, 151 … 

Examples:  

Input: N = 11 
Output: Yes 
Explanation: 
Second Centered decagonal number is 11.


Input: N = 30 
Output: No 

Approach:  

1. The Kth term of the Centered Decagonal Number is given as
K^{th} Term = 4*K^{2} - 4*K + 1
 

2. As we have to check that the given number can be expressed as a Centered Decagonal Number or not. This can be checked as follows: 

=> N = {4*K^{2} - 4*K + 1}
=> K = \frac{5 + \sqrt{20*N + 5}}{10}

3. If the value of K calculated using the above formula is an integer, then N is a Centered Decagonal Number.

4. Else the number N is not a Centered Decagonal Number.

Below is the implementation of the above approach: 

C++

// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to check if number N
// is a Centered decagonal number
bool isCentereddecagonal(int N)
{
    float n
        = (5 + sqrt(20 * N + 5))
          / 10;
 
    // Condition to check if N
    // is Centered Decagonal Number
    return (n - (int)n) == 0;
}
 
// Driver Code
int main()
{
    int N = 11;
 
    // Function call
    if (isCentereddecagonal(N)) {
        cout << "Yes";
    }
    else {
        cout << "No";
    }
    return 0;
}

                    

Java

// Java implementation to check that a number
// is a centered decagonal number or not
import java.lang.Math;
 
class GFG{
     
// Function to check that the number
// is a centered decagonal number
public static boolean isCentereddecagonal(int N)
{
    double n = (5 + Math.sqrt(20 * N + 5)) / 10;
 
    // Condition to check if the number
    // is a centered decagonal number
    return (n - (int)n) == 0;
}
 
// Driver Code
public static void main(String[] args)
{
    int n = 11;
 
    // Function call
    if (isCentereddecagonal(n))
    {
        System.out.println("Yes");
    }
    else
    {
        System.out.println("No");
    }
}
}
 
// This code is contributed by ShubhamCoder

                    

Python3

# Python3 program for the above approach
import numpy as np
 
# Function to check if the number N
# is a centered decagonal number
def isCentereddecagonal(N):
 
    n = (5 + np.sqrt(20 * N + 5)) / 10
 
    # Condition to check if N
    # is centered decagonal number
    return (n - int(n)) == 0
 
# Driver Code
N = 11
 
# Function call
if (isCentereddecagonal(N)):
    print ("Yes")
else:
    print ("No")
 
# This code is contributed by PratikBasu

                    

C#

// C# implementation to check that a number
// is a centered decagonal number or not
using System;
 
class GFG{
     
// Function to check that the number
// is a centered decagonal number
static bool isCentereddecagonal(int N)
{
    double n = (5 + Math.Sqrt(20 * N + 5)) / 10;
     
    // Condition to check if the number
    // is a centered decagonal number
    return (n - (int)n) == 0;
}
     
// Driver Code
static public void Main ()
{
    int n = 11;
     
    // Function call
    if (isCentereddecagonal(n))
    {
        Console.Write("Yes");
    }
    else
    {
        Console.Write("No");
    }
}
}
 
// This code is contributed by ShubhamCoder

                    

Javascript

<script>
// Javascript program for the above approach
 
// Function to check if number N
// is a Centered decagonal number
function isCentereddecagonal(N)
{
    let n
        = (5 + Math.sqrt(20 * N + 5))
          / 10;
 
    // Condition to check if N
    // is Centered Decagonal Number
    return (n - parseInt(n)) == 0;
}
 
// Driver Code
let N = 11;
 
// Function call
if (isCentereddecagonal(N)) {
document.write("Yes");
}
else {
document.write("No");
}
 
</script>

                    

Output: 
Yes

 

Time Complexity: O(logn)

Auxiliary Space: O(1)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads