给定一个数字 n,任务是找到第 n 个二十边形数字。
二十边形数是 20-gon 是一个二十边形的多边形。从比喻类派生的数字。这个号码有不同的图案系列号码。这些点是可数的,以特定的位置方式排列并创建一个图表。所有的点都有一个公共点,所有其他点都连接到这个点,除了这个公共点之外,点连接到它们各自的连续层的第 i 个点。
例子 :
Input : 3
Output :57
Input :8
Output :512
第 n 个二十边形数的公式:
C++
// C++ program to find
// nth Icosagonal number
#include
using namespace std;
// Function to calculate Icosagonal number
int icosagonal_poly(long int n)
{
// Formula for finding
// nth Icosagonal number
return (18 * n * n - 16 * n) / 2;
}
// Drivers code
int main()
{
long int n = 7;
cout << n << "th Icosagonal number :"
<< icosagonal_poly(n);
return 0;
}
Java
// Java program to find
// nth Icosagonal number
import java.io.*;
class GFG {
// Function to calculate Icosagonal number
static int icosagonal_poly(int n)
{
// Formula for finding
// nth Icosagonal number
return (18 * n * n - 16 * n) / 2;
}
// Drivers code
public static void main (String[] args) {
int n = 7;
System.out.print (n + "th Icosagonal number :");
System.out.println(icosagonal_poly(n));
}
}
// This code is contributed by aj_36
Python 3
# Python 3 program to find
# nth Icosagonal number
# Function to calculate
# Icosagonal number
def icosagonal_poly(n) :
# Formula for finding
# nth Icosagonal number
return (18 * n * n -
16 * n) // 2
# Driver Code
if __name__ == '__main__' :
n = 7
print(n,"th Icosagonal number : ",
icosagonal_poly(n))
# This code is contributed m_kit
C#
// C# program to find
// nth Icosagonal number
using System;
class GFG
{
// Function to calculate
// Icosagonal number
static int icosagonal_poly(int n)
{
// Formula for finding
// nth Icosagonal number
return (18 * n * n -
16 * n) / 2;
}
// Driver code
static public void Main ()
{
int n = 7;
Console.Write(n + "th Icosagonal " +
"number :");
Console.WriteLine(icosagonal_poly(n));
}
}
// This code is contributed by ajit
PHP
Javascript
输出 :
7th Icosagonal number :385
时间复杂度: O(1)
辅助空间: O(1)
参考:https://en.wikipedia.org/wiki/Polygonal_number
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。