Find the sum of the first Nth Icosagonal Numbers

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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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)) 

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.