Wednesday, November 20, 2024
Google search engine
HomeData Modelling & AIProgram to check if N is a Concentric Hexagonal Number

Program to check if N is a Concentric Hexagonal Number

Given an integer N, the task is to check if N is a Concentric Hexagonal Numbers or not. If the number N is a Concentric Hexagonal Number then print “Yes” else print “No”.

Concentric Hexagonal Numbers are the number sequence forms a pattern with concentric hexagons, and the numbers denote the number of points required after the N-th stage of the pattern. The first few concentric hexagonal numbers are 0, 1, 6, 13, 24, 37, 54, 73, 96, 121 …

Examples:

Input: N = 6 
Output: Yes 
Explanation: Third Concentrichexagonal number is 6. 

Input: N = 20 
Output: No

Approach:

  • The Kth term of the Concentric hexagonal number is given as:

Kth Term = (3 * K * K) / 2

  • As we have to check that the given number can be expressed as a Concentric hexagonal number or not. This can be checked as:

Here, Kth Term = N => (3 * K * K) / 2 = N => 3 * K * K – 2 * N = 0 The positive root of this equation is: K = sqrt((2 * N )/3)

  • If the value of K calculated using the above formula is an integer, then N is a Concentric Hexagonal Number.
  • Else the number N is not a ConcentricHexagonal Number.

C++




// C++ program to check if N is a
// Concentric Hexagonal Number
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to check if the
// number is a Concentric hexagonal number
bool isConcentrichexagonal(int N)
{
    float n = sqrt((2 * N) / 3);
 
    // Condition to check if the
    // number is a Concentric
    // hexagonal number
    return (n - (int)n) == 0;
}
 
// Driver Code
int main()
{
    int N = 6;
 
    // Function call
    if (isConcentrichexagonal(N)) {
        cout << "Yes";
    }
    else {
        cout << "No";
    }
    return 0;
}


Java




// Java program to check if N is a
// Concentric Hexagonal Number
class GFG{
 
// Function to check if the
// number is a Concentric hexagonal number
static boolean isConcentrichexagonal(int N)
{
    float n = (float) Math.sqrt((2 * N) / 3);
 
    // Condition to check if the
    // number is a Concentric
    // hexagonal number
    return (n - (int)n) == 0;
}
 
// Driver Code
public static void main(String[] args)
{
    int N = 6;
 
    // Function call
    if (isConcentrichexagonal(N))
    {
        System.out.print("Yes");
    }
    else
    {
        System.out.print("No");
    }
}
}
 
// This code is contributed by PrinciRaj1992


Python3




# Python3 program to check if N is a
# concentric hexagonal number
import math
 
# Function to check if the number
# is a concentric hexagonal number
def isConcentrichexagonal(N):
     
    n = math.sqrt((2 * N) / 3)
     
    # Condition to check if the
    # number is a concentric
    # hexagonal number
    return (n - int(n)) == 0
 
# Driver code
N = 6
 
if isConcentrichexagonal(N):
    print("Yes")
else:
    print("No")
 
# This code is contributed by divyeshrabadiya07


C#




// C# program to check if N is a
// concentric hexagonal number
using System;
 
class GFG{
 
// Function to check if the number
// is a concentric hexagonal number
static bool isConcentrichexagonal(int N)
{
    float n = (float) Math.Sqrt((2 * N) / 3);
 
    // Condition to check if the
    // number is a concentric
    // hexagonal number
    return (n - (int)n) == 0;
}
 
// Driver Code
public static void Main()
{
    int N = 6;
 
    // Function call
    if (isConcentrichexagonal(N))
    {
        Console.Write("Yes");
    }
    else
    {
        Console.Write("No");
    }
}
}
 
// This code is contributed by Code_Mech


Javascript




// JS program to check if N is a
// concentric hexagonal number
 
// Function to check if the number
// is a concentric hexagonal number
function isConcentrichexagonal(N)
{
    let n = Math.sqrt((2 * N) / 3)
       
    // Condition to check if the
    // number is a concentric
    // hexagonal number
    return (n - Math.floor(n)) == 0
}
   
   
// Driver code
let N = 6
   
if (isConcentrichexagonal(N))
    console.log("Yes")
else
    console.log("No")
   
// This code is contributed by phasing17


Output

Yes

Time complexity: O(logN) for given n, as it is using inbuilt sqrt function
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 :
20 Sep, 2022
Like Article
Save Article


Previous

<!–

8 Min Read | Java

–>


Next


<!–

8 Min Read | Java

–>

Share your thoughts in the comments

RELATED ARTICLES

Most Popular

Recent Comments