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:
- 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++
// 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> |
375
Time complexity: O(N)
Auxiliary Space: O(1) since constant space for variables is used
Another Approach:
1. Define a function icosagonal that takes an integer n and returns the nth icosagonal number using the formula n * (9 * n – 7).
2. Define a function sum_icosagonal that takes an integer n and returns the sum of the first n icosagonal numbers by calling icosagonal function for each number from 1 to n and adding up the results.
3. In main function, set the value of n to 5.
4. Call sum_icosagonal function with n as argument to calculate the sum of the first 5 icosagonal numbers.
5. Print the result using printf function.
C
#include <stdio.h> int icosagonal( int n) { return n * (9 * n - 8); } int sum_icosagonal( int n) { int sum = 0; for ( int i = 1; i <= n; i++) { sum += icosagonal(i); } return sum; } int main() { int n = 5; // set the value of n to 5 int sum = sum_icosagonal(n); printf ( "The sum of the first %d icosagonal numbers is %d\n" , n, sum); return 0; } |
C++
#include <bits/stdc++.h> using namespace std; int icosagonal( int n) { return n * (9 * n - 8); } int sum_icosagonal( int n) { int sum = 0; for ( int i = 1; i <= n; i++) { sum += icosagonal(i); } return sum; } int main() { int n = 5; // set the value of n to 5 int sum = sum_icosagonal(n); cout<< "The sum of the first " <<n<< " icosagonal numbers is " <<sum<<endl; return 0; } |
Python3
# Python program to find the sum # of first n icosagonal numbers # function to find icosagonal # numbers def icosagonal(n): return n * ( 9 * n - 8 ) # function to find # sum of n icosagonal # numbers def sum_icosagonal(n): _sum = 0 for i in range ( 1 ,n + 1 ): _sum + = icosagonal(i) return _sum # Driver Code n = 5 # Storing the result res = sum_icosagonal(n) # Printing the result print ( "The sum of first " ,n, " icosagonal Numbers is " ,res) # This code is contributed by - Dwaipayan Bandyopadhyay |
Java
import java.io.*; class GFG { public static int icosagonal( int n) { return n * ( 9 * n - 8 ); } public static int sum_icosagonal( int n) { int sum = 0 ; for ( int i = 1 ; i <= n; i++) { sum += icosagonal(i); } return sum; } public static void main (String[] args) { int n = 5 ; // set the value of n to 5 int sum = sum_icosagonal(n); System.out.print( "The sum of the first " +n); System.out.print( " icosagonal numbers is " +sum); } } // This code is contributed by shivanisinghss2110 |
Javascript
// Javascript code addition // function to find icosagonal numbers function icosagonal(n) { return n * (9 * n - 8); } // function to find sum of n icosagonal numbers function sum_icosagonal(n) { let sum = 0; for (let i = 1; i <= n; i++) { sum += icosagonal(i); } return sum; } // Driver code const n = 5; // Storing the result const res = sum_icosagonal(n); // Printing the result console.log(`The sum of first ${n} icosagonal numbers is ${res}`); // The code is contributed by Nidhi goel. |
C#
using System; class Program { static int icosagonal( int n) { return n * (9 * n - 8); } static int sum_icosagonal( int n) { int sum = 0; for ( int i = 1; i <= n; i++) { sum += icosagonal(i); } return sum; } static void Main() { int n = 5; // set the value of n to 5 int sum = sum_icosagonal(n); Console.WriteLine( "The sum of the first {0} icosagonal numbers is {1}" , n, sum); } } |
The sum of the first 5 icosagonal numbers is 375
The time complexity of this program is O(n), where n is the input value.
The space complexity is O(1) because we only need to store a few variables in memory.