Program to check if N is a Dodecagonal Number
Last Updated :
23 Nov, 2022
Given a number N, the task is to check if N is a Dodecagonal Number or not. If the number N is a Dodecagonal Number then print “Yes” else print “No”.
dodecagonal number represent Dodecagonal(12 sides polygon).The first few dodecagonal numbers are 1, 12, 33, 64, 105, 156, 217…
Examples:
Input: N = 12
Output: Yes
Explanation:
Second dodecagonal number is 12.
Input: N = 30
Output: No
Approach:
1. The Kth term of the Dodecagonal Number is given as
2. As we have to check whether the given number can be expressed as a Dodecagonal 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 Dodecagonal Number.
4. Else the number N is not a Dodecagonal Number.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isdodecagonal( int N)
{
float n
= (4 + sqrt (20 * N + 16))
/ 10;
return (n - ( int )n) == 0;
}
int main()
{
int N = 12;
if (isdodecagonal(N)) {
cout << "Yes" ;
}
else {
cout << "No" ;
}
return 0;
}
|
Java
import java.util.*;
class GFG{
static boolean isdodecagonal( int N)
{
float n = ( float ) (( 4 + Math.sqrt( 20 * N +
16 )) / 10 );
return (n - ( int )n) == 0 ;
}
public static void main(String[] args)
{
int N = 12 ;
if (isdodecagonal(N))
{
System.out.print( "Yes" );
}
else
{
System.out.print( "No" );
}
}
}
|
Python3
import numpy as np
def isdodecagonal(N):
n = ( 4 + np.sqrt( 20 * N + 16 )) / 10
return (n - int (n)) = = 0
N = 12
if (isdodecagonal(N)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG{
static bool isdodecagonal( int N)
{
float n = ( float ) ((4 + Math.Sqrt(20 * N +
16)) / 10);
return (n - ( int )n) == 0;
}
public static void Main( string [] args)
{
int N = 12;
if (isdodecagonal(N))
{
Console.Write( "Yes" );
}
else
{
Console.Write( "No" );
}
}
}
|
Javascript
<script>
function isdodecagonal(N)
{
let n
= (4 + Math.sqrt(20 * N + 16))
/ 10;
return (n - parseInt(n)) == 0;
}
let N = 12;
if (isdodecagonal(N))
{
document.write( "Yes" );
}
else
{
document.write( "No" );
}
</script>
|
Time Complexity: O(log N), since sqrt() function has been used
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...