Skip to content
Related Articles

Related Articles

Improve Article

Find the sum of the first Nth Icosagonal Numbers

  • Last Updated : 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: 
 

  1. Initially, we need to create a function which will help us to calculate the N-th Icosagonal number.
  2. Now, Run a loop starting from 1 to N, to find the sum of all the Icosagonal number.
  3. Now, add all the above calculated Icosagonal numbers.
  4. Finally, display the sum of 1st N Icosagonal numbers.

Below is the implementation of the above approach: 
 

C++




// 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 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




<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>
Output: 
375

 

Time complexity: O(N)
 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.




My Personal Notes arrow_drop_up
Recommended Articles
Page :