Wednesday, July 3, 2024
HomeData ModellingData Structure & AlgorithmProgram to check if N is a Decagonal Number

Program to check if N is a Decagonal Number

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: 
 

  1. The Kth term of the decagonal number is given as
    K^{th} Term = 4*K^{2} - 3*K
     
  2. As we have to check that the given number can be expressed as a Decagonal Number or not. This can be checked as: 
     

=> N = 4*K^{2} - 3*K
=> K = \frac{3 + \sqrt{16*N + 9}}{8}
 

  1.  
  2. If the value of K calculated using the above formula is an integer, then N is a Decagonal Number.
  3. Else N is not a 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 N is a
// Decagonal Number
bool isdecagonal(int N)
{
    float n
        = (3 + sqrt(16 * N + 9))
          / 8;
 
    // Condition to check if the
    // number is a decagonal number
    return (n - (int)n) == 0;
}
 
// Driver Code
int main()
{
    // Given Number
    int N = 10;
 
    // Function call
    if (isdecagonal(N)) {
        cout << "Yes";
    }
    else {
        cout << "No";
    }
    return 0;
}


Java




// Java program for the above approach
import java.lang.Math;
 
class GFG{
     
// Function to check if N is a
// decagonal number
public static boolean isdecagonal(int N)
{
    double n = (3 + Math.sqrt(16 * N + 9)) / 8;
     
    // Condition to check if the
    // number is a decagonal number
    return (n - (int)n) == 0;
}
 
// Driver code   
public static void main(String[] args)
{
         
    // Given number
    int N = 10;
     
    // Function call
    if (isdecagonal(N))
    {
        System.out.println("Yes");
    }
    else
    {
        System.out.println("No");
    }
}
}
 
// This code is contributed by divyeshrabadiya07   


Python3




# Python3 program for the above approach
import math
 
# Function to check if N is a
# decagonal number
def isdecagonal(N):
 
    n = (3 + math.sqrt(16 * N + 9)) / 8
     
    # Condition to check if the
    # number is a decagonal number
    return (n - int(n)) == 0
     
# Driver Code
if __name__=='__main__':
     
    # Given number
    N = 10
     
    # Function Call
    if isdecagonal(N):
        print('Yes')
    else:
        print('No')
 
# This code is contributed by rutvik_56


C#




// C# program for the above approach
using System;
 
class GFG{
     
// Function to check if N
// is a decagonal Number
static bool isdecagonal(int N)
{
    double n = (3 + Math.Sqrt(16 * N + 9)) / 8;
     
    // Condition to check if the
    // number is a decagonal number
    return (n - (int)n) == 0;
}
     
// Driver Code
static public void Main ()
{
     
    // Given Number
    int N = 10;
     
    // Function call
    if (isdecagonal(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 N is a
// Decagonal Number
function isdecagonal( N)
{
    let n
        = (3 + Math.sqrt(16 * N + 9))
          / 8;
 
    // Condition to check if the
    // number is a decagonal number
    return (n - parseInt(n)) == 0;
}
 
 
// Driver Code
 
    // Given Number
    let N = 10;
 
    // Function Call
    if (isdecagonal(N)) {
        document.write( "Yes");
    }
    else {
        document.write( "No");
    }
 
// This code contributed by gauravrajput1
 
</script>


Output: 

Yes

 

Time Complexity: O(logN) since sqrt function is being used

Auxiliary Space: O(1)

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Commit to GfG’s Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.

Last Updated :
19 Sep, 2022
Like Article
Save Article


Previous


Next


Share your thoughts in the comments

Nicole Veronica Rubhabha
Nicole Veronica Rubhabha
A highly competent and organized individual DotNet developer with a track record of architecting and developing web client-server applications. Recognized as a personable, dedicated performer who demonstrates innovation, communication, and teamwork to ensure quality and timely project completion. Expertise in C#, ASP.Net, MVC, LINQ, EF 6, Web Services, SQL Server, MySql, Web development,
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments