📜  求第一个N中心五角形数的总和

📅  最后修改于: 2021-05-06 17:34:35             🧑  作者: Mango

给定数字N ,任务是找到前N个中心五角形数的总和。

例子:

方法:想法是首先创建一个函数,该函数将帮助我们在恒定时间内找到居中的五边形数。本文已经讨论了此函数的实现。创建此函数后,请按照以下步骤操作:

  1. 运行从1到N的循环以找到i中心五角形数。
  2. 将所有以上计算的居中五角形数字相加。
  3. 然后,显示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