Skip to content
Related Articles

Related Articles

Program to check if N is a Star Number
  • Last Updated : 05 Apr, 2021

Given an integer N, the task is to check if it is a star number or not.
 

Star number is a centered figurate number that represents a centered hexagram (six-pointed star) similar to Chinese checker game. The first few Star numbers are 1, 13, 37, 73 …

Examples: 
 

Input: N = 13 
Output: Yes 
Explanation: 
Second star number is 13.
Input: 14 
Output: No 
Explanation: 
Second star number is 13, where as 37 is third. 
Therefore, 14 is not a star number. 
 

 



Approach: 
 

  1. The Kth term of the star number is given as
    K^{th} Term = 6*K*(K-1) + 1
     
  2. As we have to check that the given number can be expressed as a star number or not. This can be checked as follows – 
     

=> N = 6*K*(K-1) + 1
=> N = 6*K^{2} - 6*K + 1
=> K = \frac{6 + \sqrt{24*N + 12}}{12}
 

  1.  
  2. Finally, check the value of computed using this formulae is an integer, which means that N is a star number.

Below is the implementation of the above approach:
 

C++




// C++ implementation to check that
// a number is a star number or not
 
#include <bits/stdc++.h>
 
using namespace std;
 
// Function to check that the
// number is a star number
bool isStar(int N)
{
    float n
        = (6 + sqrt(24 * N + 12))
          / 6;
 
    // Condition to check if the
    // number is a star number
    return (n - (int)n) == 0;
}
 
// Driver Code
int main()
{
    int i = 13;
 
    // Function call
    if (isStar(i)) {
        cout << "Yes";
    }
    else {
        cout << "No";
    }
    return 0;
}

Java




// Java implementation to check that
// a number is a star number or not
import java.io.*;
import java.util.*;
 
class GFG{
     
// Function to check that the
// number is a star number
static boolean isStar(int N)
{
    double n = (6 + Math.sqrt(24 * N + 12)) / 6;
 
    // Condition to check if the
    // number is a star number
    return (n - (int)n) == 0;
}
     
// Driver code
public static void main(String[] args)
{
    int i = 13;
 
    // Function call
    if (isStar(i))
    {
        System.out.println("Yes");
    }
    else
    {
        System.out.println("No");
    }
}
}
 
// This code is contributed by coder001

Python3




# Python3 implementation to check that
# a number is a star number or not
import math
 
# Function to check that the
# number is a star number
def isStar(N):
     
    n = (math.sqrt(24 * N + 12) + 6) / 6
     
    # Condition to check if the
    # number is a star number
    return (n - int(n)) == 0
     
# Driver Code
i = 13
 
# Function call
if isStar(i):
    print("Yes")
else:
    print("No")
 
# This code is contributed by ishayadav181

C#




// C# implementation to check that
// a number is a star number or not
using System;
 
class GFG{
     
// Function to check that the
// number is a star number
static bool isStar(int N)
{
    double n = (6 + Math.Sqrt(24 * N + 12)) / 6;
 
    // Condition to check if the
    // number is a star number
    return (n - (int)n) == 0;
}
     
// Driver code
public static void Main()
{
    int i = 13;
 
    // Function call
    if (isStar(i))
    {
        Console.WriteLine("Yes");
    }
    else
    {
        Console.WriteLine("No");
    }
}
}
 
// This code is contributed by Code_Mech

Javascript




<script>
// Javascript implementation to check that
// a number is a star number or not
 
// Function to check that the
// number is a star number
function isStar(N)
{
    let n
        = (6 + Math.sqrt(24 * N + 12))
          / 6;
 
    // Condition to check if the
    // number is a star number
    return (n - parseInt(n)) == 0;
}
 
// Driver Code
let i = 13;
 
// Function call
if (isStar(i)) {
    document.write("Yes");
}
else {
    document.write("No");
}
 
// This code is contributed by rishavmahato348.
</script>
Output: 
Yes

 

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :