Número triangular centrado – Part 1

Dado un número entero n, encuentra el n-ésimo número triangular centrado. 
Número triangular centrado es un número poligonal centrado que representa un triángulo con un punto en el centro y todos los demás puntos que rodean el centro en capas triangulares sucesivas [Fuente: Wiki ]

Representación pictórica: 

centered Trigunal number pic

Las primeras series numéricas triangulares centradas son: 
1, 4, 10, 19, 31, 46, 64, 85, 109, 136, 166, 199, 235, 274, 316, 361, 409, 460……………… ………..

Ejemplos: 

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

Enfoque 
n-ésimo Término del número triangular centrado está dado por:  

CT_{n}=(3n^2+3n+2)/2

Implementación básica del enfoque anterior: 

C++

// C++ Program to find the
// nth Centered Triangular number
#include <iostream>
using namespace std;
 
// function for Centered
// Triangular number
int Centered_Triangular_num(int n)
{
    // formula for find Centered
    // Triangular number nth term
    return (3 * n * n + 3 * n + 2) / 2;
}
 
// Driver Code
int 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;
}

C

// C Program to find the
// nth Centered Triangular number
#include <stdio.h>
 
// function for Centered
// Triangular number
int Centered_Triangular_num(int n)
{
    // formula for find Centered
    // Triangular number nth term
    return (3 * n * n + 3 * n + 2) / 2;
}
 
// Driver Code
int main()
{
    // For 3rd Centered Triangular number
    int n = 3;
    printf("%d\n",Centered_Triangular_num(n));
 
    // For 12th Centered Triangular number
    n = 12;
    printf("%d\n",Centered_Triangular_num(n));
 
    return 0;
}
 
// This code is contributed by kothavvsaakash.

Java

// Java Program to find
// the nth Centered
// Triangular number
import java.io.*;
 
class GFG
{
     
// function for Centered
// Triangular number
static int Centered_Triangular_num(int n)
{
    // formula for find Centered
    // Triangular number nth term
    return (3 * n * n +
            3 * n + 2) / 2;
}
 
// Driver Code
public static void main (String[] args)
{
 
// For 3rd Centered
// Triangular number
int n = 3;
System.out.println(Centered_Triangular_num(n));
 
// For 12th Centered
// Triangular number
n = 12;
System.out.println(Centered_Triangular_num(n));
}
}
 
// This code is contributed by ajit

Python3

# 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 Code
if __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#

// C# Program to find
// the nth Centered
// Triangular number
using System;
 
class GFG
{
 
// function for Centered
// Triangular number
static int Centered_Triangular_num(int n)
{
    // formula for find Centered
    // Triangular number nth term
    return (3 * n * n +
            3 * n + 2) / 2;
}
 
// Driver Code
static public void Main ()
{
 
// For 3rd Centered
// Triangular number
int n = 3;
Console.WriteLine(Centered_Triangular_num(n));
 
// For 12th Centered
// Triangular number
n = 12;
Console.WriteLine(Centered_Triangular_num(n));
}
}
 
// This code is contributed by akt_mit

PHP

<?php
// PHP 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
    $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
?>

Javascript

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

Producción : 

19
235

Tiempo Complejidad: O(1)
Espacio Auxiliar: O(1)

Publicación traducida automáticamente

Artículo escrito por jit_t y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *