Given a number N, the task is to find the sum of the first N Icosidigonal Numbers.
The first few Icosidigonal numbers are 1, 22, 63, 124, 205, 306 …
Examples:
Input: N = 3
Output: 86
Explanation:
1, 22 and 63 are the first three Icosidigonal numbers.
Input: N = 6
Output: 721
Approach:
- Initially, we need to create a function which will help us to calculate the Nthicosidigonal number.
- Now, run a loop starting from 1 to N, to find ithicosidigonal numbers.
- Add all the above calculated icosidigonal numbers.
- Finally, display the sum of the first N Icosidigonal numbers.
Below is the implementation of the above approach:
C++
// C++ program to find the sum of the // first N icosidigonal numbers #include <bits/stdc++.h> using namespace std;
// Function to find the // N-th icosidigonal number int Icosidigonal_num( int n)
{ // Formula to calculate
// nth icosidigonal number
return (20 * n * n - 18 * n) / 2;
} // Function to find the sum of the // first N icosidigonal number int sum_Icosidigonal_num( int n)
{ // Variable to store the sum
int summ = 0;
// Iterating in the range 1 to N
for ( int i = 1; i < n + 1; i++)
{
// Finding the sum
summ += Icosidigonal_num(i);
}
return summ;
} // Driver code int main()
{ int n = 6;
// Display first Nth
// icosidigonal numbers
cout << sum_Icosidigonal_num(n);
} // This code is contributed by coder001 |
Java
// Java program to find the sum of the // first N icosidigonal numbers class GFG{
// Function to find the // N-th icosidigonal number public static int Icosidigonal_num( int n)
{ // Formula to calculate
// nth icosidigonal number
return ( 20 * n * n - 18 * n) / 2 ;
} // Function to find the sum of the // first N icosidigonal number public static int sum_Icosidigonal_num( int n)
{ // Variable to store the sum
int summ = 0 ;
// Iterating in the range 1 to N
for ( int i = 1 ; i < n + 1 ; i++)
{
// Finding the sum
summ += Icosidigonal_num(i);
}
return summ;
} // Driver code public static void main(String[] args)
{ int n = 6 ;
// Display first Nth
// icosidigonal numbers
System.out.println(sum_Icosidigonal_num(n));
} } // This code is contributed by divyeshrabadiya07 |
Python3
# Python3 program to find the # sum of the first N # Icosidigonal numbers # Function to find the # N-th Icosidigonal # number def Icosidigonal_num(n):
# Formula to calculate
# nth Icosidigonal
# number
return ( 20 * n * n -
18 * n) / / 2
# Function to find the # sum of the first N # Icosidigonal number def sum_Icosidigonal_num(n) :
# Variable to store
# the sum
summ = 0
# Iterating in the range
# 1 to N
for i in range ( 1 , n + 1 ):
summ + = Icosidigonal_num(i)
return summ
# Driver code if __name__ = = '__main__' :
n = 6
print (sum_Icosidigonal_num(n))
|
C#
// C# program to find the sum of the // first N icosidigonal numbers using System;
class GFG{
// Function to find the // N-th icosidigonal number static int Icosidigonal_num( int n)
{ // Formula to calculate
// nth icosidigonal number
return (20 * n * n - 18 * n) / 2;
} // Function to find the sum of the // first N icosidigonal number static int sum_Icosidigonal_num( int n)
{ // Variable to store the sum
int summ = 0;
// Iterating in the range 1 to N
for ( int i = 1; i < n + 1; i++)
{
// Finding the sum
summ += Icosidigonal_num(i);
}
return summ;
} // Driver code public static void Main( string [] args)
{ int n = 6;
// Display first Nth
// icosidigonal numbers
Console.WriteLine(sum_Icosidigonal_num(n));
} } // This code is contributed by AnkitRai01 |
Javascript
<script> // Javascript program to find the sum of the
// first N icosidigonal numbers
// Function to find the
// N-th icosidigonal number
function Icosidigonal_num(n)
{
// Formula to calculate
// nth icosidigonal number
return (20 * n * n - 18 * n) / 2;
}
// Function to find the sum of the
// first N icosidigonal number
function sum_Icosidigonal_num(n)
{
// Variable to store the sum
let summ = 0;
// Iterating in the range 1 to N
for (let i = 1; i < n + 1; i++)
{
// Finding the sum
summ += Icosidigonal_num(i);
}
return summ;
}
let n = 6;
// Display first Nth
// icosidigonal numbers
document.write(sum_Icosidigonal_num(n));
// This code is contributed by divyesh072019. </script> |
Output:
721
Time complexity: O(N).
Auxiliary space: O(1) since it is using constant space for variables
Article Tags :
Recommended Articles