Find the sum of the first Nth Heptadecagonal Number
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:
- Initially, we need to create a function which will help us to calculate the Nth heptadecagonal number.
- Now, run a loop starting from 1 to N, to find ith heptadecagonal number.
- Add all the above calculated heptadecagonal numbers.
- 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 numberint 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 numbersint 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 codeint 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 numbersclass GFG{ // Function to find the N-th// heptadecagonal numberpublic 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 numberspublic 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# numberdef 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 numbersdef 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 codeif __name__ == '__main__' : n = 5 print(sum_heptadecagonal_num(n)) |
C#
// C# program to find the sum of the// first N heptadecagonal numbersusing System;class GFG{ // Function to find the N-th// heptadecagonal numberpublic 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 numberspublic 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 codepublic 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> |
315
Time complexity: O(N).


