📜  求出前N个十二边形数的总和

📅  最后修改于: 2021-05-07 00:21:14             🧑  作者: Mango

给定数字N ,任务是找到前N个十二边形数的总和。

例子:

方法:

  1. 最初,我们需要创建一个函数,该函数将帮助我们计算第N十二边形数。
  2. 运行一个从1到N的循环,以找到第十二个对角线数。
  3. 将所有以上计算得出的十二边形数相加。
  4. 最后,显示前N个十二边形数字的总和。

下面是上述方法的实现:

C++
// C++ program to find the sum of
// the first N dodecagonal numbers
#include 
using namespace std;
 
// Function to find the N-th
// dodecagonal number
int Dodecagonal_num(int n)
{
 
    // Formula to calculate N-th
    // dodecagonal number
    return (5 * n * n - 4 * n);
}
 
// Function to find the sum of
// the first N dodecagonal numbers
int sum_Dodecagonal_num(int n)
{
 
    // Variable to get the sum
    int summ = 0;
 
    // Iterating through the
    // first N numbers
    for(int i = 1; i < n + 1; i++)
    {
         
        // Compute the sum
        summ += Dodecagonal_num(i);
    }
    return summ;
}
 
// Driver Code
int main()
{
    int n = 5;
 
    // Display first Nth
    // centered_decagonal number
    cout << (sum_Dodecagonal_num(n));
    return 0;
}
 
// This code is contributed by PrinciRaj1992


Java
// Java program to find the sum of
// the first N dodecagonal numbers
class GFG {
     
// Function to find the N-th
// dodecagonal number
static int Dodecagonal_num(int n)
{
 
    // Formula to calculate N-th
    // dodecagonal number
    return (5 * n * n - 4 * n);
}
 
// Function to find the sum of
// the first N dodecagonal numbers
static int sum_Dodecagonal_num(int n)
{
 
    // Variable to get the sum
    int summ = 0;
 
    // Iterating through the
    // first N numbers
    for(int i = 1; i < n + 1; i++)
    {
        
       // Compute the sum
       summ += Dodecagonal_num(i);
    }
    return summ;
}
 
// Driver Code
public static void main(String[] args)
{
    int n = 5;
 
    // Display first Nth
    // centered_decagonal number
    System.out.println(sum_Dodecagonal_num(n));
}
}
 
// This code is contributed by sapnasingh4991


Python3
# Python3 program to find the
# sum of the first N
# Dodecagonal numbers
 
# Function to find the N-th
# Dodecagonal number
def Dodecagonal_num(n):
 
    # Formula to calculate 
    # N-th Dodecagonal
    # number 
    return (5 * n * n - 4 * n)
     
   
# Function to find the
# sum of the first N
# Dodecagonal numbers
def sum_Dodecagonal_num(n) :
     
    # Variable to get the sum
    summ = 0
     
    # Iterating through the
    # first N numbers
    for i in range(1, n + 1):
 
        # Compute the sum
        summ += Dodecagonal_num(i)
     
    return summ
   
# Driver Code
if __name__ == '__main__' :
           
    n = 5
     
    print(sum_Dodecagonal_num(n))


C#
// C# program to find the sum of
// the first N dodecagonal numbers
using System;
 
class GFG {
     
// Function to find the N-th
// dodecagonal number
static int Dodecagonal_num(int n)
{
 
    // Formula to calculate N-th
    // dodecagonal number
    return (5 * n * n - 4 * n);
}
 
// Function to find the sum of
// the first N dodecagonal numbers
static int sum_Dodecagonal_num(int n)
{
 
    // Variable to get the sum
    int summ = 0;
 
    // Iterating through the
    // first N numbers
    for(int i = 1; i < n + 1; i++)
    {
         
        // Compute the sum
        summ += Dodecagonal_num(i);
    }
    return summ;
}
 
// Driver Code
public static void Main(String[] args)
{
    int n = 5;
 
    // Display first Nth
    // centered_decagonal number
    Console.WriteLine(sum_Dodecagonal_num(n));
}
}
 
// This code is contributed by sapnasingh4991


Javascript


输出:
215

时间复杂度: O(N)。