Número dodecaédrico – Part 1

Dado un número n, la tarea es encontrar el n-ésimo número dodecaédrico. 
Un número dodecaédrico pertenece a un número figurado y lo representa dodecaedro. 
Los primeros números dodecaédricos (donde n = 0, 1, 2, 3…….) son: 0, 1, 20, 84 y así sucesivamente.

Ejemplos: 

Input : 2
Output : 20

Input :8
Output :2024

Fórmula matemática para el enésimo número dodecaédrico:  

D_{n}=n(3n-1)(3n-2)/2

C++

// C++ program to find nth
// dodecahedral number
#include <bits/stdc++.h>
using namespace std;
 
// Function to find
// dodecahedral number
int dodecahedral_num(int n)
{
    // Formula to calculate nth
    // dodecahedral number
    // and return it into main function.
    return n * (3 * n - 1) * (3 * n - 2) / 2;
}
 
// Driver Code
int main()
{
    int n = 5;
    // print result
    cout << n << "th Dodecahedral number: ";
    cout << dodecahedral_num(n);
    return 0;
}

C

// C program to find nth
// dodecahedral number
#include <stdio.h>
 
// Function to find
// dodecahedral number
int dodecahedral_num(int n)
{
    // Formula to calculate nth
    // dodecahedral number
    // and return it into main function.
    return n * (3 * n - 1) * (3 * n - 2) / 2;
}
 
// Driver Code
int main()
{
    int n = 5;
    // print result
    printf("%dth Dodecahedral number: ",n);
    printf("%d",dodecahedral_num(n));
    return 0;
}
 
// This code is contributed by kothavvsaakash.

Java

// Java program to find nth dodecahedral
// number
import java.io.*;
 
class GFG {
 
    // Function to find dodecahedral number
    static int dodecahedral_num(int n)
    {
         
        // Formula to calculate nth
        // dodecahedral number
        // and return it into main function.
        return n * (3 * n - 1) *
                           (3 * n - 2) / 2;
    }
     
    // Driver Code
    public static void main (String[] args)
    {
        int n = 5;
        // print result
        System.out.print( n + "the Dodecahedral"
                                  + " number:");
        System.out.println( dodecahedral_num(n));
    }
}
 
// This code is contributed by anuj_67.

Python3

# Python3 program to find
# nth dodecahedral number
 
# Function to calculate
# dodecahedral number
 
def dodecahedral_num(n):
 
    # Formula to calculate nth
    # dodecahedral number
     
    return n * (3 * n - 1) * (3 * n - 2) // 2
 
# Driver Code
n = 5
print("%sth Dodecahedral number :" %n,
                    dodecahedral_num(n))
                     
# This code is contributed by ajit.                

C#

// C# program to find nth dodecahedral
// number
using System;
 
class GFG {
 
    // Function to find dodecahedral number
    static int dodecahedral_num(int n)
    {
         
        // Formula to calculate nth
        // dodecahedral number
        // and return it into main function.
        return n * (3 * n - 1) *
                        (3 * n - 2) / 2;
    }
     
    // Driver Code
    public static void Main ()
    {
        int n = 5;
 
        // print result
        Console.Write( n + "the Dodecahedral"
                                + " number:");
        Console.WriteLine( dodecahedral_num(n));
    }
}
 
// This code is contributed by anuj_67.

PHP

<?php
// PHP program to find nth
// dodecahedral number
 
 
// Function to find
// dodecahedral number
function dodecahedral_num( $n)
{
    // Formula to calculate nth
    // dodecahedral number
    // and return it into main function.
    return $n * (3 * $n - 1) *
                (3 * $n - 2) / 2;
}
 
// Drivers Code
$n = 5;
 
// print result
echo $n, "th Dodecahedral number: ";
echo dodecahedral_num($n);
 
// This code is contributed by vt_m
?>

Javascript

<script>
 
// Javascript program to find nth dodecahedral
// number
 
// Function to find dodecahedral number
function dodecahedral_num(n)
{
     
    // Formula to calculate nth
    // dodecahedral number and
    // return it into main function.
    return n * (3 * n - 1) *
               (3 * n - 2) / 2;
}
 
// Driver code
var n = 5;
 
// print result
document.write(n + "th Dodecahedral" +
               " number:");
document.write(dodecahedral_num(n));
 
// This code is contributed by Khushboogoyal499
 
</script>
Producción : 

5th Dodecahedral number: 455

 

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

Referencia: https://en.wikipedia.org/wiki/Dodecahedral_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 *