Programa para número icosaédrico centrado

Nos dan un número n, necesitamos encontrar el número icosaédrico centrado en el n-ésimo.
Descripción: Un número icosaédrico centrado es un número figurado centrado que representa un icosaedro .
Las primeras series de números icosaédricos centrados son: 
1, 13, 55, 147, 309, 561, 923, 1415, 2057, 2869, 3871, 5083, 6525, 8217……………….
Fórmula matemática para el número icosaédrico centrado  en el n :
 

\begin{math}CI_{n} = \frac{(2n+1)(5n^2+5n+3)}{3}

Ejemplos: 
 

Input : n = 4
Output : 309

Input : n = 12
Output : 6525

A continuación se muestra la implementación de la fórmula anterior. 
 

C++

// C++ Program to find nth
// Centered icosahedral number
#include <bits/stdc++.h>
using namespace std;
 
// Function to find
// Centered icosahedral number
int centeredIcosahedralNum(int n)
{
    // Formula to calculate nth
    // Centered icosahedral number
    // and return it into main function.
    return (2 * n + 1) * (5 * n * n + 5 * n + 3) / 3;
}
 
// Driver Code
int main()
{
    int n = 10;
    cout << centeredIcosahedralNum(n) << endl;
 
    n = 12;
    cout << centeredIcosahedralNum(n) << endl;
 
    return 0;
}

C

// C Program to find nth
// Centered icosahedral number
#include <stdio.h>
 
// Function to find
// Centered icosahedral number
int centeredIcosahedralNum(int n)
{
    // Formula to calculate nth
    // Centered icosahedral number
    // and return it into main function.
    return (2 * n + 1) * (5 * n * n + 5 * n + 3) / 3;
}
 
// Driver Code
int main()
{
    int n = 10;
    printf("%d\n",centeredIcosahedralNum(n));
 
    n = 12;
    printf("%d\n",centeredIcosahedralNum(n));
 
    return 0;
}
 
// This code is contributed by kothavvsaakash.

Java

// Java Program to find nth
// Centered icosahedral number
// Java Program to find nth Centered
// icosahedral number
 
import java.io.*;
 
class GFG {
     
    // Function to find Centered
    // icosahedral number
    static int centeredIcosahedralNum(int n)
    {
         
        // Formula to calculate nth Centered
        // icosahedral number and return it
        // into main function.
        return (2 * n + 1) * (5 * n * n +
                             5 * n + 3) / 3;
    }
     
    // Driver Code
    public static void main (String[] args)
    {
        int n = 10;
        System.out.println(
                   centeredIcosahedralNum(n));
     
        n = 12;
        System.out.println(
                   centeredIcosahedralNum(n));
    }
}
 
// This code is contributed by anuj_67.

Python3

# Python program to find nth
# Centered icosahedral number
 
# Function to calculate
# Centered icosahedral number
 
def centeredIcosahedralNum(n):
 
    # Formula to calculate nth
    # Centered icosahedral number
    return ((2 * n + 1) *
            (5 * n * n + 5 * n + 3) // 3)
 
# Driver Code
n = 10
print(centeredIcosahedralNum(n))
 
n = 12
print(centeredIcosahedralNum(n))
                     
# This code is contributed by ajit.                

C#

// C# Program to find nth
// Centered icosahedral number
// Java Program to find nth Centered
// icosahedral number
 
using System;
 
class GFG {
     
    // Function to find Centered
    // icosahedral number
    static int centeredIcosahedralNum(int n)
    {
         
        // Formula to calculate nth Centered
        // icosahedral number and return it
        // into main function.
        return (2 * n + 1) * (5 * n * n +
                            5 * n + 3) / 3;
    }
     
    // Driver Code
    public static void Main ()
    {
        int n = 10;
        Console.WriteLine(
                centeredIcosahedralNum(n));
     
        n = 12;
        Console.WriteLine(
                centeredIcosahedralNum(n));
    }
}
 
// This code is contributed by anuj_67.

PHP

<?php
// PHP Program to find nth
// Centered icosahedral number
 
// Function to find
// Centered icosahedral number
function centeredIcosahedralNum($n)
{
    // Formula to calculate nth
    // Centered icosahedral number
    // and return it into main function.
    return (2 * $n + 1) * (5 *
            $n * $n + 5 * $n + 3) / 3;
}
 
// Driver Code
$n = 10;
echo centeredIcosahedralNum($n),"\n";
 
$n = 12;
echo centeredIcosahedralNum($n),"\n";
 
// This code is contributed by m_kit
?>

Javascript

<script>
 
// Javascript Program to find nth
// Centered icosahedral number
 
// Function to find
// Centered icosahedral number
function centeredIcosahedralNum(n)
{
 
    // Formula to calculate nth
    // Centered icosahedral number
    // and return it into main function.
    return parseInt((2 * n + 1) * (5 * n * n + 5 * n + 3) / 3);
}
 
// Driver Code
let n = 10;
document.write(centeredIcosahedralNum(n) + "<br>");
 
n = 12;
document.write(centeredIcosahedralNum(n));
 
// This code is contributed by souravmahato348.
</script>
Producción : 

3871
6525

 

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

Referencia: 
https://en.wikipedia.org/wiki/Centered_icosahedral_number
 

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 *