Find the sum of the first Nth Icosagonal Numbers - GeeksforGeeks

17 Mar, 2021
Given a number N, the task is to find the sum of first N Icosagonal Numbers. 
The first few Icosagonal numbers are 1, 20, 57, 112, 185, 276...
Examples:  
Input: N = 3 Output: 78 Explanation: 1, 20 and 57 are the first three Icosagonal number.Input: N = 5 Output: 375  
 
Approach:  
Initially, we need to create a function which will help us to calculate the N-th Icosagonal number.Now, Run a loop starting from 1 to N, to find the sum of all the Icosagonal number.Now, add all the above calculated Icosagonal numbers.Finally, display the sum of 1st N Icosagonal numbers.
Initially, we need to create a function which will help us to calculate the N-th Icosagonal number.
Now, Run a loop starting from 1 to N, to find the sum of all the Icosagonal number.
Now, add all the above calculated Icosagonal numbers.
Finally, display the sum of 1st N Icosagonal numbers.
Below is the implementation of the above approach:  
C++
Java
Python3
C#
Javascript
// C++ program to find the sum of// the first N icosagonal number#include<bits/stdc++.h>using namespace std; // Function to calculate the// N-th icosagonal numberint 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 numbersint 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 codeint 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 program to find the sum of// the first N icosagonal numberclass GFG{     // Function to calculate the// N-th icosagonal numberpublic 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 numberspublic 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 codepublic 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       
# Python program to find the# sum of the first N # Icosagonal number # Function to calculate the# N-th Icosagonal numberdef 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 numbersdef 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 Codeif __name__ == '__main__' :               n = 5         # Display the sum of    # first N Icosagonal number    print(sum_Icosagonal_num(n))
// C# program to find the sum of// the first N icosagonal numberusing System; class GFG{     // Function to calculate the// N-th icosagonal numberpublic 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 numberspublic 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 codepublic 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
<script>     // Javascript program to find the sum of      // the first N icosagonal number         // Function to calculate the     // N-th icosagonal number     function 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     function sum_Icosagonal_num(n)    {        // Variable to store         // the sum         let summ = 0;         // Loop to iterate through         // the first N values and         // find the sum of first N         // icosagonal numbers         for(let i = 1; i <= n; i++)        {             // Function to get the             // Icosagonal_num             summ += Icosagonal_num(i);         }        return summ;    }             let n = 5;            // Display the sum of     // first N icosagonal number    document.write(sum_Icosagonal_num(n));     </script>
375
 
Time complexity: O(N) 
divyeshrabadiya07
Code_Mech
rutvik_56
Akanksha_Rai
divyesh072019
Mathematical
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Merge two sorted arrays
Modulo Operator (%) in C/C++ with Examples
Prime Numbers
Program to find sum of elements in a given array
Program for Decimal to Binary Conversion
Program for factorial of a number
Operators in C / C++
The Knight's tour problem | Backtracking-1
Find minimum number of coins that make a given value
Euclidean algorithms (Basic and Extended)