Program to check if N is a Decagonal Number
Last Updated :
11 Mar, 2024
Given a number N, the task is to check if N is a Decagonal Number or not. If the number N is an Decagonal Number then print “Yes” else print “No”.
Decagonal Number is a figurate number that extends the concept of triangular and square numbers to the decagon (10-sided polygon). The nth decagonal numbers count the number of dots in a pattern of n nested decagons, all sharing a common corner, where the ith decagon in the pattern has sides made of i dots spaced one unit apart from each other. The first few decagonal numbers are 1, 10, 27, 52, 85, 126, 175, …
Examples:
Input: N = 10
Output: Yes
Explanation:
Second decagonal number is 10.
Input: N = 30
Output: No
Approach:
- The Kth term of the decagonal number is given as
[Tex]K^{th} Term = 4*K^{2} – 3*K [/Tex]
- As we have to check that the given number can be expressed as a Decagonal Number or not. This can be checked as:
=> [Tex]N = 4*K^{2} – 3*K [/Tex]
=> [Tex]K = \frac{3 + \sqrt{16*N + 9}}{8} [/Tex]
- If the value of K calculated using the above formula is an integer, then N is a Decagonal Number.
- Else N is not a Decagonal Number.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isdecagonal( int N)
{
float n
= (3 + sqrt (16 * N + 9))
/ 8;
return (n - ( int )n) == 0;
}
int main()
{
int N = 10;
if (isdecagonal(N)) {
cout << "Yes" ;
}
else {
cout << "No" ;
}
return 0;
}
|
Java
import java.lang.Math;
class GFG{
public static boolean isdecagonal( int N)
{
double n = ( 3 + Math.sqrt( 16 * N + 9 )) / 8 ;
return (n - ( int )n) == 0 ;
}
public static void main(String[] args)
{
int N = 10 ;
if (isdecagonal(N))
{
System.out.println( "Yes" );
}
else
{
System.out.println( "No" );
}
}
}
|
Python3
import math
def isdecagonal(N):
n = ( 3 + math.sqrt( 16 * N + 9 )) / 8
return (n - int (n)) = = 0
if __name__ = = '__main__' :
N = 10
if isdecagonal(N):
print ( 'Yes' )
else :
print ( 'No' )
|
C#
using System;
class GFG{
static bool isdecagonal( int N)
{
double n = (3 + Math.Sqrt(16 * N + 9)) / 8;
return (n - ( int )n) == 0;
}
static public void Main ()
{
int N = 10;
if (isdecagonal(N))
{
Console.Write( "Yes" );
}
else
{
Console.Write( "No" );
}
}
}
|
Javascript
<script>
function isdecagonal( N)
{
let n
= (3 + Math.sqrt(16 * N + 9))
/ 8;
return (n - parseInt(n)) == 0;
}
let N = 10;
if (isdecagonal(N)) {
document.write( "Yes" );
}
else {
document.write( "No" );
}
</script>
|
Time Complexity: O(logN) since sqrt function is being used
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...