Número octaédrico centrado

Nos dan un número n, necesitamos encontrar el número octaédrico centrado en el n-ésimo.
Descripción: Un número octaédrico centrado es un número figurado. Cuenta el número de puntos de una red tridimensional de enteros que se encuentran dentro de un octaedro centrado en el origen. Los mismos números son casos especiales de los números de Delannoy , que cuentan ciertos caminos reticulares bidimensionales.
Los primeros números octaédricos centrados (donde n = 0, 1, 2, 3…….) son: 
1, 7, 25, 63, 129, 231, 377, 575, 833, 1159…………………… ……. 
Fórmula matemática para el número octaédrico centrado  en n :
 

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

Ejemplos: 
 

Input : n = 6
Output : 377

Input : n = 15
Output : 4991

C++

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

C

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

Java

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

Python3

# Python 3 Program to find nth
# Centered octahedral number
 
# Centered octahedral
# number function
def centeredOctahedral(n) :
     
    # Formula to calculate nth
    # Centered octahedral number
    # return it into main function.
    return (2 * n + 1) * (
            2 * n * n +
            2 * n + 3) // 3
 
# Driver Code
if __name__ == '__main__' :
         
    n = 3
    print(centeredOctahedral(n))
    n = 9
    print(centeredOctahedral(n))
 
# This code is contributed ajit

C#

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

PHP

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

Javascript

<script>
 
// Javascript Program to find nth
// Centered octahedral number
 
// Function to find
// Centered octahedral number
function centeredOctahedral(n)
{
     
    // Formula to calculate nth
    // Centered octahedral number
    // and return it into main function.
     
    return (2 * n + 1) *
           (2 * n * n + 2 * n + 3) / 3;
}
 
 
// Driver Code
 
var n = 3;
document.write(centeredOctahedral(n));
document.write("<br>");
 
n = 9;
document.write(centeredOctahedral(n));
 
// This code is contributed by Kirti
     
</script>
Producción : 

63
1159

 

Complejidad de tiempo: O(1)
Espacio auxiliar: O(1)
Referencia: 
https://en.wikipedia.org/wiki/Centered_octahedral_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 *