📜  求第一个N中心七边形数的总和

📅  最后修改于: 2021-05-04 15:50:24             🧑  作者: Mango

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

例子:

方法:

  1. 最初,我们需要创建一个函数,该函数将帮助我们计算第N中心的七边形数。
  2. 现在,运行一个从1到N的循环,以找到i居中的七边形数。
  3. 将所有以上计算的居中七边形数相加。
  4. 最后,显示前N个居中的七边形数字的总和。

下面是上述方法的实现:

C++
// C++ program to find the sum of the
// first N centered heptagonal numbers
#include
using namespace std;
 
// Function to find the N-th centered
// heptagonal number
int center_heptagonal_num(int n)
{
 
    // Formula to calculate
    // nth centered heptagonal
    // number
    return (7 * n * n - 7 * n + 2) / 2;
}
 
// Function to find the sum of the first
// N centered heptagonal numbers
int sum_center_heptagonal_num(int n)
{
 
    // Variable to store
    // the sum
    int summ = 0;
 
    // Iterating through the range
    // 1 to N
    for(int i = 1; i < n + 1; i++)
    {
       summ += center_heptagonal_num(i);
    }
    return summ;
}
 
// Driver Code
int main()
{
    int n = 5;
 
    cout << (sum_center_heptagonal_num(n));
    return 0;
}
 
// This code is contributed by PratikBasu


Java
// Java program to find the sum of the
// first N centered heptagonal numbers
class GFG{
     
// Function to find the N-th centered
// heptagonal number
public static int center_heptagonal_num(int n)
{
 
    // Formula to calculate
    // nth centered heptagonal
    // number
    return (7 * n * n - 7 * n + 2) / 2;
}
 
// Function to find the sum of the first
// N centered heptagonal numbers
public static int sum_center_heptagonal_num(int n)
{
 
    // Variable to store
    // the sum
    int summ = 0;
 
    // Iterating through the range
    // 1 to N
    for(int i = 1; i < n + 1; i++)
    {
        summ += center_heptagonal_num(i);
    }
    return summ;
}
 
// Driver Code
public static void main(String args[])
{
    int n = 5;
 
    System.out.print(sum_center_heptagonal_num(n));
}
}
 
// This code is contributed by Code_Mech


Python3
# Python3 program to find the sum
# of the first N centered
# heptagonal numbers
 
# Function to find N-th
# centered heptagonal
# number
def center_heptagonal_num(n):
  
    # Formula to calculate 
    # nth centered heptagonal
    # number
    return (7 * n * n - 7 * n + 2) // 2
     
   
# Function to find the
# sum of the first N
# centered heptagonal
# numbers
def sum_center_heptagonal_num(n) :
     
    # Variable to store
    # the sum
    summ = 0
     
    # Iterate through the range
    # 1 to N
    for i in range(1, n + 1):
        summ += center_heptagonal_num(i)
     
    return summ
   
# Driver code
if __name__ == '__main__' :
           
    n = 5
     
    print(sum_center_heptagonal_num(n))


C#
// C# program to find the sum of the
// first N centered heptagonal numbers
using System;
 
class GFG{
     
// Function to find the N-th centered
// heptagonal number
public static int center_heptagonal_num(int n)
{
 
    // Formula to calculate
    // nth centered heptagonal
    // number
    return (7 * n * n - 7 * n + 2) / 2;
}
 
// Function to find the sum of the first
// N centered heptagonal numbers
public static int sum_center_heptagonal_num(int n)
{
 
    // Variable to store
    // the sum
    int summ = 0;
 
    // Iterating through the range
    // 1 to N
    for(int i = 1; i < n + 1; i++)
    {
       summ += center_heptagonal_num(i);
    }
    return summ;
}
 
// Driver Code
public static void Main()
{
    int n = 5;
 
    Console.Write(sum_center_heptagonal_num(n));
}
}
 
// This code is contributed by Akanksha_Rai


Javascript

 
// This code is contributed by divyeshrabadiya07.


输出:
145

时间复杂度: O(N)。