Open In App

Find the sum of the first Nth Heptadecagonal Number

Improve
Improve
Like Article
Like
Save
Share
Report

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 … 
 

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).

Auxiliary space: O(1) since it is using constant space for variables
 



Last Updated : 19 Sep, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads