número dodecagonal – Part 1

Dado un número n, encuentre el n-ésimo número dodecagonal. Los números dodecagonales representan Dodecagonal (Un polígono con 12 lados). 
Algunos de los números dodecagonales son: 
1, 12, 33, 64, 105, 156, 217, 288, 369, 460, 561, 672, 793, 924………………………..
Ejemplos: 
 

Input : n = 4
Output : 64

Input : n = 9
Output : 369

Fórmula del n-ésimo término del número dodecagonal: 
 

n-th Dodecagonal number = 5n2 - 4n

A continuación se muestra la implementación del enésimo número dodecagonal: 
 

C++

// CPP Program to find the
// nth Dodecagonal number
#include <bits/stdc++.h>
using namespace std;
 
// function for Dodecagonal
// number
int Dodecagonal_number(int n)
{
    // formula for find Dodecagonal
    // nth term
    return 5 * n * n - 4 * n;
}
 
// Driver Code
int main()
{
 
    int n = 7;
    cout << Dodecagonal_number(n) << endl;
 
    n = 12;
    cout << Dodecagonal_number(n) << endl;
 
    return 0;
}

C

// C Program to find the
// nth Dodecagonal number
#include <stdio.h>
 
// function for Dodecagonal
// number
int Dodecagonal_number(int n)
{
    // formula for find Dodecagonal
    // nth term
    return 5 * n * n - 4 * n;
}
 
// Driver Code
int main()
{
 
    int n = 7;
    printf("%d\n",Dodecagonal_number(n));
 
    n = 12;
    printf("%d\n",Dodecagonal_number(n));
 
    return 0;
}
 
// This code is contributed by kothavvsaakash

Java

// Java program to find the
// nth Dodecagonal number
import java.util.*;
 
class GFG
{
 
    // function for
    // Dodecagonal number
    static int Dodecagonal_number(int n)
    {
        // formula for find
        // Dodecagonal nth term
        return 5 * n * n - 4 * n;
    }
     
    // Driver Code
    public static void main(String[] args)
    {
 
    int n = 7;
    System.out.println(Dodecagonal_number(n));
 
    n = 12;
    System.out.println(Dodecagonal_number(n));
 
    }
}
 
// This code is contributed by Anuj_67

Python3

# Python program to find
# nth Dodecagonal number
 
# Function to calculate
# Dodecagonal number
def Dodecagonal_number(n):
 
    # Formula to calculate nth
    # Dodecagonal number
     
    return  5 * n * n - 4 * n
 
# Driver Code
n = 7
print(Dodecagonal_number(n))
 
n = 12
print(Dodecagonal_number(n))
                     
# This code is contributed by aj_36.

C#

// C# program to find the nth Dodecagonal
// number
using System;
 
class GFG {
 
    // function for Dodecagonal
    // number
    static int Dodecagonal_number(int n)
    {
        // formula for find Dodecagonal
        // nth term
        return 5 * n * n - 4 * n;
    }
     
    // Driver Code
    static void Main()
    {
     
        int n = 7;
        Console.WriteLine(Dodecagonal_number(n));
     
        n = 12;
        Console.WriteLine(Dodecagonal_number(n));
     
    }
}
 
// This code is contributed by Anuj_67

PHP

<?php
// PHP Program to find the
// nth Dodecagonal number
 
// function for Dodecagonal
// number
function Dodecagonal_number($n)
{
     
    // formula for find Dodecagonal
    // nth term
    return 5 * $n * $n - 4 * $n;
}
 
// Driver code
    $n = 7;
    echo Dodecagonal_number($n), "\n";
 
    $n = 12;
    echo Dodecagonal_number($n), "\n";
 
     
// This code is contributed by aj_36
?>

Javascript

<script>
// Javascript Program to find the
// nth Dodecagonal number
 
// function for Dodecagonal
// number
function Dodecagonal_number(n)
{
 
    // formula for find Dodecagonal
    // nth term
    return 5 * n * n - 4 * n;
}
 
// Driver Code
 
let n = 7;
document.write(Dodecagonal_number(n) + "<br>");
 
n = 12;
document.write(Dodecagonal_number(n) + "<br>");
 
// This code is contributed by subham348.
</script>
Producción : 

217
672

 

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