Program to check if N is a Centered Decagonal Number
Last Updated :
19 Sep, 2022
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
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:
=>
=>
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++
#include <bits/stdc++.h>
using namespace std;
bool isCentereddecagonal( int N)
{
float n
= (5 + sqrt (20 * N + 5))
/ 10;
return (n - ( int )n) == 0;
}
int main()
{
int N = 11;
if (isCentereddecagonal(N)) {
cout << "Yes" ;
}
else {
cout << "No" ;
}
return 0;
}
|
Java
import java.lang.Math;
class GFG{
public static boolean isCentereddecagonal( int N)
{
double n = ( 5 + Math.sqrt( 20 * N + 5 )) / 10 ;
return (n - ( int )n) == 0 ;
}
public static void main(String[] args)
{
int n = 11 ;
if (isCentereddecagonal(n))
{
System.out.println( "Yes" );
}
else
{
System.out.println( "No" );
}
}
}
|
Python3
import numpy as np
def isCentereddecagonal(N):
n = ( 5 + np.sqrt( 20 * N + 5 )) / 10
return (n - int (n)) = = 0
N = 11
if (isCentereddecagonal(N)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG{
static bool isCentereddecagonal( int N)
{
double n = (5 + Math.Sqrt(20 * N + 5)) / 10;
return (n - ( int )n) == 0;
}
static public void Main ()
{
int n = 11;
if (isCentereddecagonal(n))
{
Console.Write( "Yes" );
}
else
{
Console.Write( "No" );
}
}
}
|
Javascript
<script>
function isCentereddecagonal(N)
{
let n
= (5 + Math.sqrt(20 * N + 5))
/ 10;
return (n - parseInt(n)) == 0;
}
let N = 11;
if (isCentereddecagonal(N)) {
document.write( "Yes" );
}
else {
document.write( "No" );
}
</script>
|
Time Complexity: O(logn)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...