Dodecahedral number
Last Updated :
18 May, 2022
Given a number n, the task is to find n-th dodecahedral number.
A dodecahedral number belongs to a figurate number and represents it dodecahedron.
First few Dodecahedral number (where n = 0, 1, 2, 3…….) are : 0, 1, 20, 84 and so on.
Examples :
Input : 2
Output : 20
Input :8
Output :2024
Mathematics formula for nth dodecahedral number:
C++
#include <bits/stdc++.h>
using namespace std;
int dodecahedral_num( int n)
{
return n * (3 * n - 1) * (3 * n - 2) / 2;
}
int main()
{
int n = 5;
cout << n << "th Dodecahedral number: " ;
cout << dodecahedral_num(n);
return 0;
}
|
C
#include <stdio.h>
int dodecahedral_num( int n)
{
return n * (3 * n - 1) * (3 * n - 2) / 2;
}
int main()
{
int n = 5;
printf ( "%dth Dodecahedral number: " ,n);
printf ( "%d" ,dodecahedral_num(n));
return 0;
}
|
Java
import java.io.*;
class GFG {
static int dodecahedral_num( int n)
{
return n * ( 3 * n - 1 ) *
( 3 * n - 2 ) / 2 ;
}
public static void main (String[] args)
{
int n = 5 ;
System.out.print( n + "the Dodecahedral"
+ " number:" );
System.out.println( dodecahedral_num(n));
}
}
|
Python3
def dodecahedral_num(n):
return n * ( 3 * n - 1 ) * ( 3 * n - 2 ) / / 2
n = 5
print ( "%sth Dodecahedral number :" % n,
dodecahedral_num(n))
|
C#
using System;
class GFG {
static int dodecahedral_num( int n)
{
return n * (3 * n - 1) *
(3 * n - 2) / 2;
}
public static void Main ()
{
int n = 5;
Console.Write( n + "the Dodecahedral"
+ " number:" );
Console.WriteLine( dodecahedral_num(n));
}
}
|
PHP
<?php
function dodecahedral_num( $n )
{
return $n * (3 * $n - 1) *
(3 * $n - 2) / 2;
}
$n = 5;
echo $n , "th Dodecahedral number: " ;
echo dodecahedral_num( $n );
?>
|
Javascript
<script>
function dodecahedral_num(n)
{
return n * (3 * n - 1) *
(3 * n - 2) / 2;
}
var n = 5;
document.write(n + "th Dodecahedral" +
" number:" );
document.write(dodecahedral_num(n));
</script>
|
Output : 5th Dodecahedral number: 455
Time Complexity: O(1)
Auxiliary Space: O(1)
Reference: https://en.wikipedia.org/wiki/Dodecahedral_number
Share your thoughts in the comments
Please Login to comment...