Centered triangular number - GeeksforGeeks

03 Aug, 2021
Given an integer n, find the nth Centered triangular number. 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 [Source : Wiki ]
Pictorial Representation : 
The first few centered triangular number series are : 1, 4, 10, 19, 31, 46, 64, 85, 109, 136, 166, 199, 235, 274, 316, 361, 409, 460.............................
Examples: 
Input : n = 1
Output : 4
Explanation :
A dot in the centre and 3 dots forming the
triangle outside it, thus 4.

Input : n = 6 
Output : 64

Input : n = 10
Output : 166
Approach nth Term of centered triangular number is given by:  
Basic Implementation of the above approach: 
C++
Java
Python3
C#
PHP
Javascript
// C++ Program to find the// nth Centered Triangular number#include <iostream>using namespace std; // function for Centered// Triangular numberint Centered_Triangular_num(int n){    // formula for find Centered    // Triangular number nth term    return (3 * n * n + 3 * n + 2) / 2;} // Driver Codeint main(){    // For 3rd Centered Triangular number    int n = 3;    cout << Centered_Triangular_num(n) << endl;     // For 12th Centered Triangular number    n = 12;    cout << Centered_Triangular_num(n) << endl;     return 0;}
// Java Program to find// the nth Centered// Triangular numberimport java.io.*; class GFG{     // function for Centered// Triangular numberstatic int Centered_Triangular_num(int n){    // formula for find Centered    // Triangular number nth term    return (3 * n * n +            3 * n + 2) / 2;} // Driver Codepublic static void main (String[] args){ // For 3rd Centered// Triangular numberint n = 3;System.out.println(Centered_Triangular_num(n)); // For 12th Centered// Triangular numbern = 12;System.out.println(Centered_Triangular_num(n));}} // This code is contributed by ajit
# Program to find nth# Centered Triangular number def Centered_Triangular_num(n) :         # Formula to calculate nth    # Centered Triangular number    return (3 * n * n +            3 * n + 2) // 2 # Driver Codeif __name__ == '__main__' :     # For 3rd Centered    # Triangular number        n = 3    print(Centered_Triangular_num(n))         # For 12th Centered    # Triangular number    n = 12    print(Centered_Triangular_num(n))                                  # This code is contributed# by akt_mit
// C# Program to find// the nth Centered// Triangular numberusing System; class GFG{ // function for Centered// Triangular numberstatic int Centered_Triangular_num(int n){    // formula for find Centered    // Triangular number nth term    return (3 * n * n +            3 * n + 2) / 2;} // Driver Codestatic public void Main (){ // For 3rd Centered// Triangular numberint n = 3;Console.WriteLine(Centered_Triangular_num(n)); // For 12th Centered// Triangular numbern = 12;Console.WriteLine(Centered_Triangular_num(n));}} // This code is contributed by akt_mit
<?php// PHP Program to find the//nth Centered Triangular number // function for Centered// Triangular numberfunction Centered_Triangular_num($n){    // formula for find Centered    // Triangular number nth term    return (3 * $n * $n + 3 * $n + 2) / 2;} // Driver Code    // For 3rd Centered Triangular number    $n = 3;    echo Centered_Triangular_num($n), "\n" ;     // For 12th Centered Triangular number    $n = 12;    echo Centered_Triangular_num($n), "\n"; // This code is contributed by aj_36?>
<script>// javascript Program to find// the nth Centered// Triangular number     // function for Centered    // Triangular number    function Centered_Triangular_num(n)    {             // formula for find Centered        // Triangular number nth term        return (3 * n * n + 3 * n + 2) / 2;    }     // Driver Code             // For 3rd Centered        // Triangular number        var n = 3;        document.write(Centered_Triangular_num(n)+"<br/>");         // For 12th Centered        // Triangular number        n = 12;        document.write(Centered_Triangular_num(n)+"<br/>"); // This code is contributed by Rajput-Ji</script>
Output : 
19
235
Time Complexity: O(1)Auxiliary Space: O(1)
jit_t
Rajput-Ji
anikakapoor
manikarora059
sweetyty
series
triangular-number
Mathematical
Mathematical
series
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Merge two sorted arrays
Modulo Operator (%) in C/C++ with Examples
Prime Numbers
Program to find GCD or HCF of two numbers
Print all possible combinations of r elements in a given array of size n
Sieve of Eratosthenes
Operators in C / C++
Program for factorial of a number
The Knight's tour problem | Backtracking-1
Find minimum number of coins that make a given value