📜  求第N个余角数的总和

📅  最后修改于: 2021-05-07 08:32:42             🧑  作者: Mango

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

例子:

方法:

  1. 最初,我们需要创建一个函数,该函数将帮助我们计算第N个余角线数。
  2. 现在,运行一个从1到N的循环以找到所有余角线数的总和。
  3. 现在,添加以上所有计算出的余弦数。
  4. 最后,显示第一个N余弦数的总和。

下面是上述方法的实现:

C++
// C++ program to find the sum of
// the first N icosagonal number
#include
using namespace std;
 
// Function to calculate the
// N-th icosagonal number
int Icosagonal_num(int n)
{
    // Formula to calculate
    // nth icosagonal number
    // & return it
    return (18 * n * n - 16 * n) / 2;
}
     
// Function to find the
// sum of the first N
// icosagonal numbers
int sum_Icosagonal_num(int n)
{
    // Variable to store
    // the sum
    int summ = 0;
         
    // Loop to iterate through
    // the first N values and
    // find the sum of first N
    // icosagonal numbers
    for(int i = 1; i <= n; i++)
    {
         
        // Function to get the
        // Icosagonal_num
        summ += Icosagonal_num(i);
    }
    return summ;
}
 
// Driver code
int main()
{
    int n = 5;
     
    // Display the sum of
    // first N icosagonal number
    cout << sum_Icosagonal_num(n) << endl;
}
 
// This code is contributed by rutvik_56


Java
// Java program to find the sum of
// the first N icosagonal number
class GFG{
     
// Function to calculate the
// N-th icosagonal number
public static int Icosagonal_num(int n)
{
     
    // Formula to calculate
    // nth icosagonal number
    // & return it
    return (18 * n * n - 16 * n) / 2;
}
     
// Function to find the
// sum of the first N
// icosagonal numbers
public static int sum_Icosagonal_num(int n)
{
     
    // Variable to store
    // the sum
    int summ = 0;
         
    // Loop to iterate through
    // the first N values and
    // find the sum of first N
    // icosagonal numbers
    for(int i = 1; i <= n; i++)
    {
         
       // Function to get the
       // Icosagonal_num
       summ += Icosagonal_num(i);
    }
    return summ;
}
 
// Driver code
public static void main(String[] args)
{
    int n = 5;
     
    // Display the sum of
    // first N icosagonal number
    System.out.println(sum_Icosagonal_num(n));
}
}
 
// This code is contributed by divyeshrabadiya07


Python3
# Python program to find the
# sum of the first N 
# Icosagonal number
 
# Function to calculate the
# N-th Icosagonal number
def Icosagonal_num(n):
 
    # Formula to calculate 
    # nth Icosagonal
    # number & return it 
    return (18 * n * n -
            16 * n) // 2
     
   
# Function to find the
# sum of the first N
# Icosagonal numbers
def sum_Icosagonal_num(n) :
     
    # Variable to store
    # the sum
    summ = 0
     
    # Loop to iterate through
    # the first N values and
    # find the sum of first N
    # Icosagonal numbers
    for i in range(1, n + 1):
 
        # function to get the
        # Icosagonal_num
        summ += Icosagonal_num(i)
     
    return summ
   
# Driver Code
if __name__ == '__main__' :
           
    n = 5
     
    # Display the sum of
    # first N Icosagonal number
    print(sum_Icosagonal_num(n))


C#
// C# program to find the sum of
// the first N icosagonal number
using System;
 
class GFG{
     
// Function to calculate the
// N-th icosagonal number
public static int Icosagonal_num(int n)
{
     
    // Formula to calculate
    // nth icosagonal number
    // & return it
    return (18 * n * n - 16 * n) / 2;
}
     
// Function to find the
// sum of the first N
// icosagonal numbers
public static int sum_Icosagonal_num(int n)
{
     
    // Variable to store
    // the sum
    int summ = 0;
         
    // Loop to iterate through
    // the first N values and
    // find the sum of first N
    // icosagonal numbers
    for(int i = 1; i <= n; i++)
    {
 
       // Function to get the
       // Icosagonal_num
       summ += Icosagonal_num(i);
    }
    return summ;
}
 
// Driver code
public static void Main()
{
    int n = 5;
     
    // Display the sum of
    // first N icosagonal number
    Console.WriteLine(sum_Icosagonal_num(n));
}
}
 
// This code is contributed by Code_Mech


Javascript


输出:
375

时间复杂度: O(N)