给定数字N ,任务是找到第一个N个中心五角形数的总和。
The first few Centered Pentadecagonal Numbers are 1, 16, 46, 91, 151, 226, 316 …
例子:
Input: N = 3
Output: 63
Explanation:
1, 16 and 46 are the first three centered pentadecagonal numbers.
Input: N = 5
Output: 305
方法:
- 最初,我们需要创建一个函数,该函数将帮助我们计算第N个居中的五边形数。
- 现在,运行一个从1到N的循环,以找到第i个居中的五边形数。
- 将所有以上计算的居中五角形数相加。
- 最后,显示第一个N个居中的五边形数字的总和。
下面是上述方法的实现:
C++
// C++ program to find the sum of the
// first N centered pentadecagonal number
#include
using namespace std;
// Function to find the centered
// pentadecagonal number
int Centered_Pentadecagonal_num(int n)
{
// Formula to calculate
// N-th centered pentadecagonal
// number
return (15 * n * n - 15 * n + 2) / 2;
}
// Function to find the sum of
// the first N centered
// pentadecagonal numbers
int sum_Centered_Pentadecagonal_num(int n)
{
// Variable to store
// the sum
int summ = 0;
for(int i = 1; i < n + 1; i++)
{
summ += Centered_Pentadecagonal_num(i);
}
return summ;
}
// Driver Code
int main()
{
int n = 5;
cout << sum_Centered_Pentadecagonal_num(n);
return 0;
}
// This code is contributed by Rajput-Ji
Java
// Java program to find the sum of the
// first N centered pentadecagonal number
class GFG {
// Function to find the centered
// pentadecagonal number
static int Centered_Pentadecagonal_num(int n)
{
// Formula to calculate
// N-th centered pentadecagonal
// number
return (15 * n * n - 15 * n + 2) / 2;
}
// Function to find the sum of
// the first N centered
// pentadecagonal numbers
static int sum_Centered_Pentadecagonal_num(int n)
{
// Variable to store
// the sum
int summ = 0;
for(int i = 1; i < n + 1; i++)
{
summ += Centered_Pentadecagonal_num(i);
}
return summ;
}
// Driver Code
public static void main(String[] args)
{
int n = 5;
System.out.println(sum_Centered_Pentadecagonal_num(n));
}
}
// This code is contributed by sapnasingh4991
Python3
# Python3 program to find the sum
# of the first N centered
# Pentadecagonal number
# Function to find the
# Centered_Pentadecagonal
# number
def Centered_Pentadecagonal_num(n):
# Formula to calculate
# N-th Centered_Pentadecagonal
# number
return (15 * n * n -
15 * n + 2) // 2
# Function to find the
# sum of the first N
# Centered_Pentadecagonal
# numbers
def sum_Centered_Pentadecagonal_num(n) :
# Variable to store
# the sum
summ = 0
for i in range(1, n + 1):
summ += Centered_Pentadecagonal_num(i)
return summ
# Driver code
if __name__ == '__main__' :
n = 5
print(sum_Centered_Pentadecagonal_num(n))
C#
// C# program to find the sum of the
// first N centered pentadecagonal number
using System;
class GFG
{
// Function to find the centered
// pentadecagonal number
static int Centered_Pentadecagonal_num(int n)
{
// Formula to calculate
// N-th centered pentadecagonal
// number
return (15 * n * n - 15 * n + 2) / 2;
}
// Function to find the sum of
// the first N centered
// pentadecagonal numbers
static int sum_Centered_Pentadecagonal_num(int n)
{
// Variable to store
// the sum
int summ = 0;
for(int i = 1; i < n + 1; i++)
{
summ += Centered_Pentadecagonal_num(i);
}
return summ;
}
// Driver Code
public static void Main(String[] args)
{
int n = 5;
Console.WriteLine(sum_Centered_Pentadecagonal_num(n));
}
}
// This code is contributed by sapnasingh4991
Javascript
输出:
305
时间复杂度: O(N)