Program to check if N is a Centered Triangular Number Last Updated : 19 Sep, 2022 Comments Improve Suggest changes Like Article Like Report Given an integer N, the task is to check if it is a Centered triangular number or not. Centered triangular number is a centered polygonal number that represents a triangle with a dot in the centre and all other dots surrounding the centre in successive triangular layers . The first few Centered triangular numbers are 1, 4, 10, 19, 31, 46, 64, 85, 109, 136, ... Examples: Input: N = 4 Output: YesInput: 20 Output: No Approach: The Kth Centered triangular number can be expressed as: K^{th} Term = \frac{3*K^{2} + 3*K + 2}{2} In order to check if the given number N can be expressed as a Centered triangular number or not, we need to check if \frac{-3 + \sqrt{24*N - 15}}{6} gives an integer or not. Below is the implementation of the above approach: C++ // C++ implementation to check // whether a given number is a // Centered triangular number or not #include <bits/stdc++.h> using namespace std; // Function to check if the // number is a Centered // Triangular Number bool isCenteredtriangular(int N) { float K = (-3 + sqrt(24 * N - 15)) / 6; // Condition for K to be // an integer return (K - (int)K) == 0; } // Driver Code int main() { int N = 85; if (isCenteredtriangular(N)) { cout << "Yes"; } else { cout << "No"; } return 0; } Java // Java implementation to check that // a number is a centered triangular // number or not import java.lang.Math; class GFG{ // Function to check that the number // is a centered triangular number public static boolean isCenteredTriangular(int N) { double K = (-3 + Math.sqrt(24 * N - 15)) / 6; // Condition to check if the number // is a centered triangular number return (K - (int)K) == 0; } // Driver Code public static void main(String[] args) { int N = 85; // Function call if (isCenteredTriangular(N)) { System.out.println("Yes"); } else { System.out.println("No"); } } } // This code is contributed by ShubhamCoder Python3 # Python3 implementation to check # whether a given number is a # Centered triangular number or not import math # Function to check if the # number is a Centered # Triangular Number def isCenteredtriangular(N): K = (-3 + math.sqrt(24 * N - 15)) / 6 # Condition for K to be # an integer if (K - int(K)) == 0: return True return False # Driver Code # Given Number N = 85 # Function call if (isCenteredtriangular(N)): print("Yes") else: print("No") # This code is contributed by shubhamsingh10 C# // C# implementation to check whether // a given number is a centered // triangular number or not using System; class GFG{ // Function to check if the number // is a centered triangular number static bool isCenteredtriangular(int N) { double K = (-3 + Math.Sqrt(24 * N - 15)) / 6; // Condition for K to be // an integer return (K - (int)K) == 0; } // Driver Code static public void Main () { int N = 85; if (isCenteredtriangular(N)) { Console.Write("Yes"); } else { Console.Write("No"); } } } // This code is contributed by shubhamsingh10 JavaScript <script> // JavaScript implementation to check // whether a given number is a // Centered triangular number or not // Function to check if the // number is a Centered // Triangular Number function isCenteredtriangular(N) { var K = (-3 + Math.sqrt(24 * N - 15)) / 6; // Condition for K to be // an integer return (K - parseInt(K)) == 0; } // Driver Code var N = 85; if (isCenteredtriangular(N)) { document.write("Yes"); } else { document.write("No"); } </script> Output: Yes Time Complexity: O(logN) because inbuilt sqrt function is being used Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Program to check if N is a Centered Triangular Number spp____ Follow Improve Article Tags : Mathematical DSA Practice Tags : Mathematical Similar Reads Program to check if N is a Centered Tridecagonal Number Given a number N, the task is to check if N is a Centered Tridecagonal Number or not. If the number N is a Centered Tridecagonal Number then print "Yes" else print "No". Centered tridecagonal number represents a dot at the center and other dots surrounding the center dot in the successive tridecagon 4 min read Program to check if N is a Centered Cubic Number Given a number N, the task is to check if N is a centered cubic number or not. A centered cubic number counts the number of points which are formed by a point that is surrounded by concentric cubical layers in 3D with i2 points on the square faces of the i-th layer. The first few Centered cube numbe 7 min read Program to check if N is a triacontagonal number Given a number N, the task is to check if the number is a Triacontagonal number or not. A Triacontagonal number is a class of figurate number. It has 30 â sided polygon called triacontagon. The N-th triacontagonal number countâs the 30 number of dots and all other dots are surrounding with a common 4 min read Program to check if N is a Centered Hexagonal Number Given an integer N, the task is to check if N is a Centered Hexagonal Number or not. If the number N is a Centered Hexagonal Number then print "Yes" else print "No". Centered hexagonal number are figurate numbers and are in the form of the Hexagon. The Centered Hexagonal number is different from Hex 4 min read Program to check if N is a Centered Octagonal Number Given an integer N, the task is to check if it is a Centered Octagonal number or not. If the number N is an Centered Octagonal Number then print "Yes" else print "No". Centered Octagonal number represents an octagon with a dot in the centre and others dots surrounding the centre dot in the successiv 4 min read Program to check if N is a Centered Decagonal Number Given an integer N, the task is to check if N is a Centered Decagonal Number or not. If the number N is a Centered Decagonal Number then print "Yes" else print "No". Centered Decagonal Number is centered figurative number that represents a decagon with dot in center and all other dot surrounding it 4 min read Program to check if N is a Centered heptagonal number Given an integer N, the task is to check if it is a Centered heptagonal number or not. Centered heptagonal number is centered figure number that represents a heptagon with dot in center and all other dot surrounding in heptagonal form..The first few Centered heptagonal number are 1, 8, 22, 43, 71, 1 4 min read Program to check if N is a Centered dodecagonal number Given an integer N, the task is to check if N is a Centered Dodecagonal Number or not. If the number N is a Centered Dodecagonal Number then print "Yes" else print "No". Centered Dodecagonal Number represents a dot in the center and other dots surrounding it in successive Dodecagonal Number(12 sided 4 min read Program to check if N is a Centered Octadecagonal number Given an integer N, the task is to check if it is a Centered Octadecagonal number or not. Print "yes" if it is otherwise output is "no". Centered Octadecagonal number represents a dot in the centre and others dot are arranged around it in successive layers of octadecagon(18 sided polygon). The first 4 min read Program to check if N is a Centered nonadecagonal number Given an integer N, the task is to check if it is a Centered nonadecagonal number or not. Centered nonadecagonal number represents a dot in the centre and other dots surrounding it in successive nonadecagon(19 sided polygon) layers.The first few Centered nonadecagonal numbers are 1, 20, 58, 115, 191 4 min read Like