Skip to content
Related Articles

Related Articles

Improve Article

Find the sum of the first N Dodecagonal Numbers

  • Last Updated : 17 Mar, 2021

Given a number N the task is to find the sum of first N Dodecagonal Number.
 

The first few dodecagonal numbers are 1, 12, 33, 64, 105, 156, 217 … 
 

Examples: 
 

Input: N = 3 
Output: 46 
Explanation: 
1, 12 and 33 are the first three Dodecagonal numbers
Input: N = 5 
Output: 215 
 

 



Approach: 
 

  1. Initially, we need to create a function which will help us to calculate the Nth Dodecagonal number.
  2. Run a loop starting from 1 to N, to find ith Dodecagonal number.
  3. Add all the above calculated Dodecagonal numbers.
  4. Finally, display the sum of the first N Dodecagonal numbers.

Below is the implementation of the above approach: 
 

C++




// C++ program to find the sum of
// the first N dodecagonal numbers
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the N-th
// dodecagonal number
int Dodecagonal_num(int n)
{
 
    // Formula to calculate N-th
    // dodecagonal number
    return (5 * n * n - 4 * n);
}
 
// Function to find the sum of
// the first N dodecagonal numbers
int sum_Dodecagonal_num(int n)
{
 
    // Variable to get the sum
    int summ = 0;
 
    // Iterating through the
    // first N numbers
    for(int i = 1; i < n + 1; i++)
    {
         
        // Compute the sum
        summ += Dodecagonal_num(i);
    }
    return summ;
}
 
// Driver Code
int main()
{
    int n = 5;
 
    // Display first Nth
    // centered_decagonal number
    cout << (sum_Dodecagonal_num(n));
    return 0;
}
 
// This code is contributed by PrinciRaj1992

Java




// Java program to find the sum of
// the first N dodecagonal numbers
class GFG {
     
// Function to find the N-th
// dodecagonal number
static int Dodecagonal_num(int n)
{
 
    // Formula to calculate N-th
    // dodecagonal number
    return (5 * n * n - 4 * n);
}
 
// Function to find the sum of
// the first N dodecagonal numbers
static int sum_Dodecagonal_num(int n)
{
 
    // Variable to get the sum
    int summ = 0;
 
    // Iterating through the
    // first N numbers
    for(int i = 1; i < n + 1; i++)
    {
        
       // Compute the sum
       summ += Dodecagonal_num(i);
    }
    return summ;
}
 
// Driver Code
public static void main(String[] args)
{
    int n = 5;
 
    // Display first Nth
    // centered_decagonal number
    System.out.println(sum_Dodecagonal_num(n));
}
}
 
// This code is contributed by sapnasingh4991

Python3




# Python3 program to find the
# sum of the first N
# Dodecagonal numbers
 
# Function to find the N-th
# Dodecagonal number
def Dodecagonal_num(n):
 
    # Formula to calculate 
    # N-th Dodecagonal
    # number 
    return (5 * n * n - 4 * n)
     
   
# Function to find the
# sum of the first N
# Dodecagonal numbers
def sum_Dodecagonal_num(n) :
     
    # Variable to get the sum
    summ = 0
     
    # Iterating through the
    # first N numbers
    for i in range(1, n + 1):
 
        # Compute the sum
        summ += Dodecagonal_num(i)
     
    return summ
   
# Driver Code
if __name__ == '__main__' :
           
    n = 5
     
    print(sum_Dodecagonal_num(n))

C#




// C# program to find the sum of
// the first N dodecagonal numbers
using System;
 
class GFG {
     
// Function to find the N-th
// dodecagonal number
static int Dodecagonal_num(int n)
{
 
    // Formula to calculate N-th
    // dodecagonal number
    return (5 * n * n - 4 * n);
}
 
// Function to find the sum of
// the first N dodecagonal numbers
static int sum_Dodecagonal_num(int n)
{
 
    // Variable to get the sum
    int summ = 0;
 
    // Iterating through the
    // first N numbers
    for(int i = 1; i < n + 1; i++)
    {
         
        // Compute the sum
        summ += Dodecagonal_num(i);
    }
    return summ;
}
 
// Driver Code
public static void Main(String[] args)
{
    int n = 5;
 
    // Display first Nth
    // centered_decagonal number
    Console.WriteLine(sum_Dodecagonal_num(n));
}
}
 
// This code is contributed by sapnasingh4991

Javascript




<script>
 
    // Javascript program to find the sum of
    // the first N dodecagonal numbers
     
    // Function to find the N-th
    // dodecagonal number
    function Dodecagonal_num(n)
    {
 
        // Formula to calculate N-th
        // dodecagonal number
        return (5 * n * n - 4 * n);
    }
 
    // Function to find the sum of 
    // the first N dodecagonal numbers
    function sum_Dodecagonal_num(n)
    {
 
        // Variable to get the sum
        let summ = 0;
 
        // Iterating through the
        // first N numbers
        for(let i = 1; i < n + 1; i++)
        {
 
            // Compute the sum
            summ += Dodecagonal_num(i);
        }
        return summ;
    }
     
    let n = 5;
   
    // Display first Nth
    // centered_decagonal number
    document.write(sum_Dodecagonal_num(n));
 
</script>
Output: 
215

 

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 :