Program to check if N is a Centered Triangular Number

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: Yes

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

Yes

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : SHUBHAMSINGH10

Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.