Open In App

Program to check if N is a Dodecagonal Number

Improve
Improve
Like Article
Like
Save
Share
Report

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
K^{th} Term = 5*K^{2} - 4*K

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:

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

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

// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to check if number N
// is a dodecagonal number or not
bool isdodecagonal(int N)
{
    float n
        = (4 + sqrt(20 * N + 16))
          / 10;
 
    // Condition to check if the
    // N is a dodecagonal number
    return (n - (int)n) == 0;
}
 
// Driver Code
int main()
{
    // Given Number
    int N = 12;
 
    // Function call
    if (isdodecagonal(N)) {
        cout << "Yes";
    }
    else {
        cout << "No";
    }
    return 0;
}

                    

Java

// Java program for the above approach
import java.util.*;
 
class GFG{
 
// Function to check if number N
// is a dodecagonal number or not
static boolean isdodecagonal(int N)
{
    float n = (float) ((4 + Math.sqrt(20 * N +
                                      16)) / 10);
 
    // Condition to check if the
    // N is a dodecagonal number
    return (n - (int)n) == 0;
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Given Number
    int N = 12;
 
    // Function call
    if (isdodecagonal(N))
    {
        System.out.print("Yes");
    }
    else
    {
        System.out.print("No");
    }
}
}
 
// This code is contributed by sapnasingh4991

                    

Python3

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

                    

C#

// C# program for the above approach
using System;
 
class GFG{
 
// Function to check if number N
// is a dodecagonal number or not
static bool isdodecagonal(int N)
{
     
    float n = (float) ((4 + Math.Sqrt(20 * N +
                                      16)) / 10);
 
    // Condition to check if the
    // N is a dodecagonal number
    return (n - (int)n) == 0;
}
 
// Driver Code
public static void Main(string[] args)
{
     
    // Given number
    int N = 12;
 
    // Function call
    if (isdodecagonal(N))
    {
        Console.Write("Yes");
    }
    else
    {
        Console.Write("No");
    }
}
}
 
// This code is contributed by rutvik_56

                    

Javascript

<script>
// Javascript program for the above approach
 
// Function to check if number N
// is a dodecagonal number or not
function isdodecagonal(N)
{
    let n
        = (4 + Math.sqrt(20 * N + 16))
          / 10;
 
    // Condition to check if the
    // N is a dodecagonal number
    return (n - parseInt(n)) == 0;
}
 
// Driver Code
// Given Number
let N = 12;
 
// Function call
if (isdodecagonal(N))
{
    document.write("Yes");
}
else
{
    document.write("No");
}
 
// This code is contributed by subhammahato348.
</script>

                    

Output
Yes

Time Complexity: O(log N), since sqrt() function has been used
Auxiliary Space: O(1)



Last Updated : 23 Nov, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads