Número octadecagonal centrado

Dado un número n, encuentre el n-ésimo número octadecagonal centrado. 
El Número Octadecagonal Centrado representa un punto en el centro y otros puntos están dispuestos alrededor de él en capas sucesivas de octadecágono (polígono de 18 lados).
 

center_octadecagon_num

Ejemplos: 
 

Input :  2
Output : 19

Input : 6
Output : 271

En matemáticas, el número octadecagonal centrado para el término n viene dado por: 
 

CO_{n}= 9n^2 -9n+1

A continuación se muestra la implementación básica de la idea anterior:
 

C++

// C++ Program to find the
// nth centered octadecagonal
// number
#include <bits/stdc++.h>
using namespace std;
 
// centered octadecagon  function
int center_octadecagon_num(long int n)
{
    // Formula to calculate nth
    // centered octadecagonal number
    return 9 * n * n - 9 * n + 1;
}
 
// Driver Code
int main()
{
    long int n = 3;
    cout << n << "th centered octadecagonal number : "
                  << center_octadecagon_num(n);
    cout << endl;
    n = 13;
    cout << n << "th centered octadecagonal number : "
                 << center_octadecagon_num(n);
 
    return 0;
}

C

// C Program to find the
// nth centered octadecagonal
// number
#include <stdio.h>
 
// centered octadecagon  function
int center_octadecagon_num(long int n)
{
    // Formula to calculate nth
    // centered octadecagonal number
    return 9 * n * n - 9 * n + 1;
}
 
// Driver Code
int main()
{
    long int n = 3;
    printf("%ldth centered octadecagonal number : %d\n",n,center_octadecagon_num(n));
 
    n = 13;
    printf("%ldth centered octadecagonal number : %d\n",n,center_octadecagon_num(n));
 
    return 0;
}
 
// This code is contributed by kothavvsaakash.

Java

// Java Program to find the
// nth centered octadecagonal
// number
import java.io.*;
 
class GFG
{
     
// centered octadecagon function
static int center_octadecagon_num(int n)
{
         
    // Formula to calculate nth
    // centered octadecagonal number
    return 9 * n * n - 9 * n + 1;
}
 
// Driver Code
public static void main (String[] args)
{
int n = 3;
System.out.print(n + "th centered " +
           "octadecagonal number : ");
System.out.println(center_octadecagon_num(n));
 
n = 13;
System.out.print(n + "th centered " +
           "octadecagonal number : ");
System.out.println(center_octadecagon_num(n));
}
}
 
// This code is contributed by ajit

Python3

# Program to find nth
# centered octadecagonal number
 
# Centered octadecagonal
# number function
def center_octadecagon_num(n) :
     
    # Formula to calculate
    # nth centered octadecagonal
    # number & return it
    # into main function.
    return(9 * n * n -
           9 * n + 1)
 
# Driver Code
if __name__ == '__main__' :
         
    n = 3
    print(n,"rd centered octadecagonal " +
                                "number : ",
                center_octadecagon_num(n))
 
    n = 13
    print(n,"th centered octadecagonal " +
                              "number : ",
                center_octadecagon_num(n))
             
# This code is contributed
# by akt_mit

C#

// C# Program to find the
// nth centered octadecagonal
// number
using System;
 
class GFG
{
     
// centered octadecagon function
static int center_octadecagon_num(int n)
{
         
    // Formula to calculate nth
    // centered octadecagonal number
    return 9 * n * n - 9 * n + 1;
}
 
// Driver Code
static public void Main ()
{
     
    int n = 3;
    Console.Write( n + "th centered " +
            "octadecagonal number : ");
    Console.WriteLine( center_octadecagon_num(n));
 
    n = 13;
    Console.Write( n + "th centered " +
            "octadecagonal number : ");
    Console.WriteLine(center_octadecagon_num(n));
}
}
 
// This code is contributed by aj_36.

PHP

<?php
// PHP Program to find the
// nth centered octadecagonal
// number
 
// centered octadecagon function
function center_octadecagon_num($n)
{
    // Formula to calculate nth
    // centered octadecagonal number
    return (9 * $n * $n -
            9 * $n + 1);
}
 
// Driver Code
$n = 3;
echo $n , "th centered octadecagonal " .
                            "number : ",
             center_octadecagon_num($n);
echo "\n";
 
$n = 13;
echo $n , "th centered octadecagonal " .
                            "number : ",
             center_octadecagon_num($n);
 
// This code is contributed by m_kit
?>

Javascript

<script>
 
// Javascript program to find the
// nth centered octadecagonal
// number
 
// centered octadecagon function
function center_octadecagon_num(n)
{
     
    // Formula to calculate nth
    // centered octadecagonal number
    return 9 * n * n - 9 * n + 1;
}
 
 
// Driver Code
var n = 3;
document.write(n + "th centered " +
           "octadecagonal number : ");
document.write(center_octadecagon_num(n) + "<br>");
 
n = 13;
document.write(n + "th centered " +
           "octadecagonal number : ");
document.write(center_octadecagon_num(n));
 
// This code is contributed by Kirti
     
</script>

Producción : 

3th centered octadecagonal number : 55
13th centered octadecagonal number : 1405

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 *