Skip to content
Related Articles

Related Articles

Improve Article

Find the sum of the first Nth Heptadecagonal Number

  • Last Updated : 18 Mar, 2021

Given a number N, the task is to find the sum of first N Heptadecagonal Numbers.
 

The first few heptadecagonal numbers are 1, 17, 48, 94, 155, 231 … 
 

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.

Examples: 
 



Input: N = 3 
Output: 66 
Explanation: 
1, 17 and 48 are the first three heptadecagonal numbers.
Input: N = 6 
Output: 546 
 

 

Approach: 
 

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

Below is the implementation of the above approach: 
 

C++




// C++ program to find the sum of the
// first N heptadecagonal numbers
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the N-th
// heptadecagonal number
int heptadecagonal_num(int n)
{
     
    // Formula to calculate nth
    // heptadecagonal number
    return ((15 * n * n) - 13 * n) / 2;
}
 
// Function to find the sum of the
// first N heptadecagonal numbers
int sum_heptadecagonal_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 += heptadecagonal_num(i);
    }
    return summ;
}
 
// Driver code
int main()
{
    int n = 5;
     
    cout << sum_heptadecagonal_num(n);
}
 
// This code is contributed by coder001

Java




// Java program to find the sum of the
// first N heptadecagonal numbers
class GFG{
     
// Function to find the N-th
// heptadecagonal number
public static int heptadecagonal_num(int n)
{
         
    // Formula to calculate nth
    // heptadecagonal number
    return ((15 * n * n) - 13 * n) / 2;
}
     
// Function to find the sum of the
// first N heptadecagonal numbers
public static int sum_heptadecagonal_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 += heptadecagonal_num(i);
    }
    return summ;
}
 
// Driver code    
public static void main(String[] args)
{
    int n = 5;
     
    System.out.println(sum_heptadecagonal_num(n));
}
}
 
// This code is contributed by divyeshrabadiya07

Python3




# Python3 program to find the sum
# of the first N 
# heptadecagonal numbers
 
# Function to find the
# N-th heptadecagonal
# number
def heptadecagonal_num(n):
 
    # Formula to calculate 
    # nth heptadecagonal
    # number
    return ((15 * n * n) - 13 * n) // 2
     
   
# Function to find the
# sum of the first N
# heptadecagonal numbers
def sum_heptadecagonal_num(n) :
     
    # Variable to store
    # the sum
    summ = 0
     
    # Iterate from 1 to N
    for i in range(1, n + 1):
 
        summ += heptadecagonal_num(i)
     
    return summ
   
# Driver code
if __name__ == '__main__' :
           
    n = 5
     
    print(sum_heptadecagonal_num(n))

C#




// C# program to find the sum of the
// first N heptadecagonal numbers
using System;
 
class GFG{
     
// Function to find the N-th
// heptadecagonal number
public static int heptadecagonal_num(int n)
{
         
    // Formula to calculate nth
    // heptadecagonal number
    return ((15 * n * n) - 13 * n) / 2;
}
     
// Function to find the sum of the
// first N heptadecagonal numbers
public static int sum_heptadecagonal_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 += heptadecagonal_num(i);
    }
    return summ;
}
 
// Driver code
public static void Main()
{
    int n = 5;
     
    Console.WriteLine(sum_heptadecagonal_num(n));
}
}
 
// This code is contributed by Code_Mech

Javascript




<script>
 
    // Javascript program to find the sum of the
    // first N heptadecagonal numbers
     
    // Function to find the N-th
    // heptadecagonal number 
    function heptadecagonal_num(n) 
    {
 
        // Formula to calculate nth
        // heptadecagonal number 
        return ((15 * n * n) - 13 * n) / 2;
    }
 
    // Function to find the sum of the
    // first N heptadecagonal numbers 
    function sum_heptadecagonal_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 += heptadecagonal_num(i);
        }
        return summ;
    }
     
    let n = 5;
    document.write(sum_heptadecagonal_num(n));
 
// This code is contributed by divyesh072019.
</script>
Output: 
315

 

Time complexity: O(N).
 




My Personal Notes arrow_drop_up
Recommended Articles
Page :