Given a number N, the task is to find the sum of first N Centered Dodecagonal Number.
The first few Centered Dodecagonal Numbers are 1, 13, 37, 73, 121, 181 …
Examples:
Input: N = 3
Output: 51
Explanation:
1, 13 and 37 are the first three centered Dodecagonal number.
Input: N = 5
Output: 245
Approach:
- Initially, create a function which will help us to calculate the NthCentered Dodecagonal number.
- Run a loop starting from 1 to N, to find i-th Centered Dodecagonal number.
- Add all the above calculated Centered Dodecagonal numbers.
- Finally, display the sum of the first N Centered Dodecagonal numbers.
Below is the implementation of the above approach:
C++
// C++ program to find the sum // of the first N Centered // Dodecagonal number #include <bits/stdc++.h> using namespace std;
// Function to find the N-th // Centered Dodecagonal number int Centered_Dodecagonal_num( int n)
{ // Formula to calculate nth
// Centered_Dodecagonal number
return 6 * n * (n - 1) + 1;
} // Function to find the sum of the first // N Centered_Dodecagonal number int sum_Centered_Dodecagonal_num( int n)
{ // Variable to store the sum
int summ = 0;
// Iterating from 1 to N
for ( int i = 1; i < n + 1; i++)
{
// Finding the sum
summ += Centered_Dodecagonal_num(i);
}
return summ;
} // Driver code int main()
{ int n = 5;
cout << sum_Centered_Dodecagonal_num(n);
} // This code is contributed by coder001 |
Java
// Java program to find the sum of the // first N centered dodecagonal number class GFG {
// Function to find the N-th // centered dodecagonal number static int Centered_Dodecagonal_num( int n)
{ // Formula to calculate nth
// Centered_Dodecagonal number
return 6 * n * (n - 1 ) + 1 ;
} // Function to find the sum of the first // N Centered_Dodecagonal number static int sum_Centered_Dodecagonal_num( int n)
{ // Variable to store the sum
int summ = 0 ;
// Iterating from 1 to N
for ( int i = 1 ; i < n + 1 ; i++)
{
// Finding the sum
summ += Centered_Dodecagonal_num(i);
}
return summ;
} // Driver code public static void main (String[] args)
{ int n = 5 ;
System.out.print(sum_Centered_Dodecagonal_num(n));
} } // This code is contributed by AnkitRai01 |
Python3
# Python3 program to find the sum # of the first N centered # Dodecagonal number # Function to find the # N-th Centered Dodecagonal # number def Centered_Dodecagonal_num(n):
# Formula to calculate
# nth Centered_Dodecagonal
# number
return 6 * n * (n - 1 ) + 1
# Function to find the # sum of the first N # Centered_Dodecagonal # number def sum_Centered_Dodecagonal_num(n) :
# Variable to store the
# sum
summ = 0
# Iterating from 1 to N
for i in range ( 1 , n + 1 ):
# Finding the sum
summ + = Centered_Dodecagonal_num(i)
return summ
# Driver code if __name__ = = '__main__' :
n = 5
print (sum_Centered_Dodecagonal_num(n))
|
C#
// C# program to find the sum of the // first N centered dodecagonal number using System;
class GFG{
// Function to find the N-th // centered dodecagonal number static int Centered_Dodecagonal_num( int n)
{ // Formula to calculate nth
// Centered_Dodecagonal number
return 6 * n * (n - 1) + 1;
} // Function to find the sum of the first // N Centered_Dodecagonal number static int sum_Centered_Dodecagonal_num( int n)
{ // Variable to store the sum
int summ = 0;
// Iterating from 1 to N
for ( int i = 1; i < n + 1; i++)
{
// Finding the sum
summ += Centered_Dodecagonal_num(i);
}
return summ;
} // Driver code public static void Main()
{ int n = 5;
Console.Write(sum_Centered_Dodecagonal_num(n));
} } // This code is contributed by Code_Mech |
Javascript
<script> // Javascript program to find the sum
// of the first N Centered
// Dodecagonal number
// Function to find the N-th
// Centered Dodecagonal number
function Centered_Dodecagonal_num(n)
{
// Formula to calculate nth
// Centered_Dodecagonal number
return 6 * n * (n - 1) + 1;
}
// Function to find the sum of the first
// N Centered_Dodecagonal number
function sum_Centered_Dodecagonal_num(n)
{
// Variable to store the sum
let summ = 0;
// Iterating from 1 to N
for (let i = 1; i < n + 1; i++)
{
// Finding the sum
summ += Centered_Dodecagonal_num(i);
}
return summ;
}
let n = 5;
document.write(sum_Centered_Dodecagonal_num(n));
</script> |
Output:
245
Time Complexity: O(N).
Auxiliary Space: O(1) since constant variables are being used
Article Tags :
Recommended Articles