给定一个数 n,任务是找到第 n 个十七边形数。
十七边形数是一类数字。它有一个十七边形的多边形,称为七边形。第n个十七边形数是17个点,所有其他点都围绕一个共同的共享角并形成一个图案。
例子:
Input : 5
Output :155
Input :9
Output :549
计算第n个十七边形数的公式:
C++
// C++ program to find Nth
// heptadecagonal number
#include
using namespace std;
// Function to calculate heptadecagonal
// number
int heptadecagonalNum(long int n)
{
return ((15 * n * n) - 13 * n) / 2;
}
// Driver Code
int main()
{
long int n = 3;
cout << n << "th Heptadecagonal number : ";
cout << heptadecagonalNum(n);
cout << endl;
n = 8;
cout << n << "th Heptadecagonal number : ";
cout << heptadecagonalNum(n);
return 0;
}
Java
// Java program to find Nth heptadecagonal number
import java.io.*;
class GFG {
// Function to calculate heptadecagonal
// number
static long heptadecagonalNum(long n)
{
return ((15 * n * n) - 13 * n) / 2;
}
// Driver Code
public static void main (String[] args)
{
long n = 3;
System.out.print( n + "th Heptadecagonal"
+ " number : ");
System.out.println( heptadecagonalNum(n));
n = 8;
System.out.print( n + "th Heptadecagonal"
+ " number : ");
System.out.print( heptadecagonalNum(n));
}
}
// This code is contributed by anuj_67.
Python3
# Python program to find Nth
# heptadecagonal number
# Function to calculate
# heptadecagonal number
def heptadecagonalNum(n):
# Formula to calculate nth
# heptadecagonal number
return ((15 * n * n) - 13 * n) // 2
# Driver Code
n = 3
print("%sth Heptadecagonal number : " %n,
heptadecagonalNum(n))
n = 8
print("%sth Heptadecagonal number: " %n,
heptadecagonalNum(n))
# This code is contributed by ajit
C#
// C# program to find Nth
// heptadecagonal number
using System;
class GFG {
// Function to calculate
// heptadecagonal number
static long heptadecagonalNum(long n)
{
return ((15 * n * n) -
13 * n) / 2;
}
// Driver Code
public static void Main ()
{
long n = 3;
Console.Write( n + "th Heptadecagonal"
+ " number : ");
Console.WriteLine( heptadecagonalNum(n));
n = 8;
Console.Write( n + "th Heptadecagonal"
+ " number : ");
Console.WriteLine( heptadecagonalNum(n));
}
}
// This code is contributed by anuj_67.
PHP
Javascript
输出
3th Heptadecagonal number : 48
8th Heptadecagonal number : 428
时间复杂度: O(1)
辅助空间: O(1)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。