Icosagonal number
Last Updated :
19 May, 2022
Given a number n, the task is to find the nth Icosagonal number.
An Icosagonal number is the 20-gon is a twenty-sided polygon. The number derived from the figurative class. There are different pattern series number in this number. The dots are countable, arrange in a specific way of position and create a diagram. All the dots have a common dots points, all others dots are connected to this points and except this common point the dots connected to their i-th dots with their respective successive layer.
Examples :
Input : 3
Output :57
Input :8
Output :512
Formula for nth icosagonal number:
C++
#include <bits/stdc++.h>
using namespace std;
int icosagonal_poly( long int n)
{
return (18 * n * n - 16 * n) / 2;
}
int main()
{
long int n = 7;
cout << n << "th Icosagonal number :"
<< icosagonal_poly(n);
return 0;
}
|
C
#include <stdio.h>
int icosagonal_poly( long int n)
{
return (18 * n * n - 16 * n) / 2;
}
int main()
{
long int n = 7;
printf ( "%ldth Icosagonal number : %d" ,n,icosagonal_poly(n));
return 0;
}
|
Java
import java.io.*;
class GFG {
static int icosagonal_poly( int n)
{
return ( 18 * n * n - 16 * n) / 2 ;
}
public static void main (String[] args) {
int n = 7 ;
System.out.print (n + "th Icosagonal number :" );
System.out.println(icosagonal_poly(n));
}
}
|
Python 3
def icosagonal_poly(n) :
return ( 18 * n * n -
16 * n) / / 2
if __name__ = = '__main__' :
n = 7
print (n, "th Icosagonal number : " ,
icosagonal_poly(n))
|
C#
using System;
class GFG
{
static int icosagonal_poly( int n)
{
return (18 * n * n -
16 * n) / 2;
}
static public void Main ()
{
int n = 7;
Console.Write(n + "th Icosagonal " +
"number :" );
Console.WriteLine(icosagonal_poly(n));
}
}
|
PHP
<?php
function icosagonal_poly( $n )
{
return (18 * $n *
$n - 16 * $n ) / 2;
}
$n = 7;
echo $n , "th Icosagonal number :" ,
icosagonal_poly( $n );
?>
|
Javascript
<script>
function icosagonal_poly(n)
{
return (18 * n * n - 16 * n) / 2;
}
let n = 7;
document.write(n + "th Icosagonal number :" );
document.write(icosagonal_poly(n));
</script>
|
Output :
7th Icosagonal number :385
Time Complexity: O(1)
Auxiliary Space: O(1)
Reference: https://en.wikipedia.org/wiki/Polygonal_number
Share your thoughts in the comments
Please Login to comment...