给定数字N ,任务是找到前N个中心五角形数的总和。
The first few Centered Pentagonal Number are 1, 6, 16, 31, 51, 76, 106 …
例子:
Input: N = 3
Output: 23
Explanation:
1, 6 and 16 are the first three
Centered Pentagonal number.
Input: N = 5
Output: 105
方法:想法是首先创建一个函数,该函数将帮助我们在恒定时间内找到居中的五边形数。本文已经讨论了此函数的实现。创建此函数后,请按照以下步骤操作:
- 运行从1到N的循环,以找到第i个中心五角形数。
- 将所有以上计算的居中五角形数字相加。
- 然后,显示N个中心五角形数字的总和。
下面是上述方法的实现:
C++
// C++ program to find the sum of the
// first N centered pentagonal numbers
#include
using namespace std;
// Function to find the
// Centered_Pentagonal number
int Centered_Pentagonal_num(int n)
{
// Formula to calculate
// nth Centered_Pentagonal
// number & return it
// into main function.
return (5 * n * n - 5 * n + 2) / 2;
}
// Function to find the sum of the first
// N Centered_Pentagonal numbers
int sum_Centered_Pentagonal_num(int n)
{
// To get the sum
int summ = 0;
// Iterating through the range
// 1 to N
for(int i = 1; i < n + 1; i++)
{
summ += Centered_Pentagonal_num(i);
}
return summ;
}
// Driver Code
int main()
{
int n = 5;
// Display first Nth
// Centered_Pentagonal number
cout << (sum_Centered_Pentagonal_num(n));
return 0;
}
// This code is contributed by PratikBasu
Java
// Java program to find the sum of the
// first N centered pentagonal numbers
class GFG{
// Function to find the
// Centered_Pentagonal number
static int Centered_Pentagonal_num(int n)
{
// Formula to calculate
// nth Centered_Pentagonal
// number & return it
// into main function.
return (5 * n * n - 5 * n + 2) / 2;
}
// Function to find the sum of the first
// N Centered_Pentagonal numbers
static int sum_Centered_Pentagonal_num(int n)
{
// To get the sum
int summ = 0;
// Iterating through the range
// 1 to N
for(int i = 1; i < n + 1; i++)
{
summ += Centered_Pentagonal_num(i);
}
return summ;
}
// Driver Code
public static void main(String[] args)
{
int n = 5;
// Display first Nth
// Centered_Pentagonal number
System.out.print((sum_Centered_Pentagonal_num(n)));
}
}
// This code is contributed by sapnasingh4991
Python3
# Python3 program to find the sum of
# the first N Centered
# Pentagonal number
# Function to find the
# Centered_Pentagonal number
def Centered_Pentagonal_num(n):
# Formula to calculate
# nth Centered_Pentagonal
# number & return it
# into main function.
return (5 * n * n -
5 * n + 2) // 2
# Function to find the
# sum of the first N
# Centered_Pentagonal
# numbers
def sum_Centered_Pentagonal_num(n) :
# To get the sum
summ = 0
for i in range(1, n + 1):
# Function to get the
# Centered_Pentagonal_num
summ += Centered_Pentagonal_num(i)
return summ
# Driver Code
if __name__ == '__main__' :
n = 5
# display first Nth
# Centered_Pentagonal number
print(sum_Centered_Pentagonal_num(n))
C#
// C# program to find the sum of the
// first N centered pentagonal numbers
using System;
class GFG{
// Function to find the
// Centered_Pentagonal number
static int Centered_Pentagonal_num(int n)
{
// Formula to calculate
// nth Centered_Pentagonal
// number & return it
// into main function.
return (5 * n * n - 5 * n + 2) / 2;
}
// Function to find the sum of the first
// N Centered_Pentagonal numbers
static int sum_Centered_Pentagonal_num(int n)
{
// To get the sum
int summ = 0;
// Iterating through the range
// 1 to N
for(int i = 1; i < n + 1; i++)
{
summ += Centered_Pentagonal_num(i);
}
return summ;
}
// Driver code
public static void Main(String[] args)
{
int n = 5;
// Display first Nth
// Centered_Pentagonal number
Console.Write((sum_Centered_Pentagonal_num(n)));
}
}
// This code is contributed by amal kumar choubey
Javascript
输出:
105