Número dodecagonal centrado

Dado un número n, encuentre el n-ésimo número dodecagonal centrado. 
El número dodecagonal centrado representa un punto en el centro y otros puntos que lo rodean en sucesivas capas dodecagonales (polígono de 12 lados). 
Ejemplos: 
 

Input :  3
Output : 37

Input : 7
Output :253 

centered dodecagonal number

Los primeros números dodecagonales centrados son: 
1, 13, 37, 73, 121, 181, 253, 337, 433, 541, 661…………………..
La fórmula para el enésimo número dodecagonal centrado:
 

CDg_{n}= 6n(n-1)+1

C++

// C++ Program to find
// nth centered
// Dodecagonal number
#include <bits/stdc++.h>
using namespace std;
 
// Function to calculate Centered
// Dodecagonal number
int centeredDodecagonal(long int n)
{
    // Formula to calculate nth
    // centered Dodecagonal number
    return 6 * n * (n - 1) + 1;
}
 
// Driver Code
int main()
{
    long int n = 2;
    cout << centeredDodecagonal(n);
    cout << endl;
    n = 9;
    cout << centeredDodecagonal(n);
 
    return 0;
}

C

// C Program to find
// nth centered
// Dodecagonal number
#include <stdio.h>
 
// Function to calculate Centered
// Dodecagonal number
int centeredDodecagonal(long int n)
{
    // Formula to calculate nth
    // centered Dodecagonal number
    return 6 * n * (n - 1) + 1;
}
 
// Driver Code
int main()
{
    long int n = 2;
    printf("%d\n",centeredDodecagonal(n));
 
    n = 9;
    printf("%d\n",centeredDodecagonal(n));
 
    return 0;
}
 
// This code is contributed by kothavvsaakash.

Java

// Java Program to find nth
// centered dodecagonal number
import java.io.*;
 
class GFG{
     
// Function to calculate
// centered dodecagonal number
static long centeredDodecagonal(long n)
{
     
    // Formula to calculate nth
    // centered dodecagonal number
    return 6 * n * (n - 1) + 1;
}
 
// Driver Code
public static void main(String[] args)
{
    long n = 2;
    System.out.println(centeredDodecagonal(n));
 
    n = 9;
    System.out.println(centeredDodecagonal(n));
}
}
 
// This code is contributed by anuj_67

Python3

# Python3 program to find nth
# centered dodecagonal number
 
# Function to calculate
# centered dodecagonal number
def centeredDodecagonal(n) :
     
    # Formula to calculate nth
    # centered dodecagonal number
    return 6 * n * (n - 1) + 1;
 
# Driver code
n = 2
print(centeredDodecagonal(n));
 
n = 9
print(centeredDodecagonal(n));
 
# This code is contributed by grand_master

C#

// C# Program to find nth
// centered dodecagonal number
using System;
class GFG{
 
// Function to calculate
// centered dodecagonal number
static long centeredDodecagonal(long n)
{
     
    // Formula to calculate nth
    // centered dodecagonal number
    return 6 * n * (n - 1) + 1;
}
 
// Driver Code
public static void Main(String[] args)
{
    long n = 2;
    Console.WriteLine(centeredDodecagonal(n));
     
    n = 9;
    Console.WriteLine(centeredDodecagonal(n));
}
}
 
// This code is contributed by shivanisinghss2110

Javascript

<script>
 
// Javascript Program to find
// nth centered
// Dodecagonal number
 
// Function to calculate Centered
// Dodecagonal number
function centeredDodecagonal(n)
{
 
    // Formula to calculate nth
    // centered Dodecagonal number
    return 6 * n * (n - 1) + 1;
}
 
// Driver Code
let n = 2;
document.write(centeredDodecagonal(n));
document.write("<br>");
n = 9;
document.write(centeredDodecagonal(n));
 
// This code is contributed by rishavmahato348.
</script>
Producción : 

13
433

 

Complejidad temporal: O(1)
Espacio auxiliar: O(1)
Referencias 
http://oeis.org/A003154
 

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 *