📜  求第一个N中心十二边形的总和

📅  最后修改于: 2021-04-23 17:00:33             🧑  作者: Mango

给定数N,任务是找到第一个N中心十二边形数的总和。

例子:

方法:

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

下面是上述方法的实现:

C++
// C++ program to find the sum
// of the first N Centred
// Dodecagonal number
#include 
using namespace std;
 
// Function to find the N-th 
// Centered Dodecagonal number
int Centered_Dodecagonal_num(int n)
{
     
    // Formula to calculate nth 
    // Centered_Dodecagonal number
    return 6 * n * (n - 1) + 1;
}
 
// Function to find the sum of the first
// N Centered_Dodecagonal number
int sum_Centered_Dodecagonal_num(int n)
{
     
    // Variable to store the sum
    int summ = 0;
     
    // Iterating from 1 to N
    for(int i = 1; i < n + 1; i++)
    {
 
       // Finding the sum
       summ += Centered_Dodecagonal_num(i);
    }
    return summ;
}
 
// Driver code
int main()
{
    int n = 5;
     
    cout << sum_Centered_Dodecagonal_num(n);
}
 
// This code is contributed by coder001


Java
// Java program to find the sum of the 
// first N centred dodecagonal number
class GFG {
     
// Function to find the N-th
// centered dodecagonal number
static int Centered_Dodecagonal_num(int n)
{
         
    // Formula to calculate nth
    // Centered_Dodecagonal number
    return 6 * n * (n - 1) + 1;
}
     
// Function to find the sum of the first
// N Centered_Dodecagonal number
static int sum_Centered_Dodecagonal_num(int n)
{
         
    // Variable to store the sum
    int summ = 0;
         
    // Iterating from 1 to N
    for(int i = 1; i < n + 1; i++)
    {
         
       // Finding the sum
       summ += Centered_Dodecagonal_num(i);
    }
    return summ;
}
     
// Driver code
public static void main (String[] args)
{
    int n = 5;
         
    System.out.print(sum_Centered_Dodecagonal_num(n));
}
}
 
// This code is contributed by AnkitRai01


Python3
# Python3 program to find the sum
# of the first N centred
# Dodecagonal number
 
# Function to find the
# N-th Centered Dodecagonal
# number
def Centered_Dodecagonal_num(n):
 
    # Formula to calculate 
    # nth Centered_Dodecagonal
    # number
    return 6 * n * (n - 1) + 1
     
   
# Function to find the
# sum of the first N
# Centered_Dodecagonal
# number
def sum_Centered_Dodecagonal_num(n) :
     
    # Variable to store the
    # sum
    summ = 0
     
    # Iterating from 1 to N
    for i in range(1, n + 1):
 
        # Finding the sum
        summ += Centered_Dodecagonal_num(i)
     
    return summ
   
# Driver code
if __name__ == '__main__' :
           
    n = 5
     
    print(sum_Centered_Dodecagonal_num(n))


C#
// C# program to find the sum of the
// first N centred dodecagonal number
using System;
 
class GFG{
     
// Function to find the N-th
// centered dodecagonal number
static int Centered_Dodecagonal_num(int n)
{
         
    // Formula to calculate nth
    // Centered_Dodecagonal number
    return 6 * n * (n - 1) + 1;
}
     
// Function to find the sum of the first
// N Centered_Dodecagonal number
static int sum_Centered_Dodecagonal_num(int n)
{
         
    // Variable to store the sum
    int summ = 0;
         
    // Iterating from 1 to N
    for(int i = 1; i < n + 1; i++)
    {
        
       // Finding the sum
       summ += Centered_Dodecagonal_num(i);
    }
    return summ;
}
     
// Driver code
public static void Main()
{
    int n = 5;
         
    Console.Write(sum_Centered_Dodecagonal_num(n));
}
}
 
// This code is contributed by Code_Mech


Javascript


输出:
245

时间复杂度: O(N)。