Skip to content
Related Articles

Related Articles

Improve Article
Find the sum of the first N Centered heptagonal number
  • Last Updated : 17 Mar, 2021

Given a number N, the task is to find the sum of the first N Centered heptagonal numbers.
 

The first few Centered heptagonal number are 1, 8, 22, 43, 71, 106, 148, … 
 

Examples: 
 

Input: N = 3 
Output: 31 
Explanation: 
1, 8 and 22 are the first three centered heptagonal numbers.
Input: N = 5 
Output: 145 
 

 



Approach: 
 

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

Below is the implementation of the above approach: 
 

C++




// C++ program to find the sum of the
// first N centered heptagonal numbers
#include<bits/stdc++.h>
using namespace std;
 
// Function to find the N-th centered
// heptagonal number
int center_heptagonal_num(int n)
{
 
    // Formula to calculate
    // nth centered heptagonal
    // number
    return (7 * n * n - 7 * n + 2) / 2;
}
 
// Function to find the sum of the first
// N centered heptagonal numbers
int sum_center_heptagonal_num(int n)
{
 
    // Variable to store
    // the sum
    int summ = 0;
 
    // Iterating through the range
    // 1 to N
    for(int i = 1; i < n + 1; i++)
    {
       summ += center_heptagonal_num(i);
    }
    return summ;
}
 
// Driver Code
int main()
{
    int n = 5;
 
    cout << (sum_center_heptagonal_num(n));
    return 0;
}
 
// This code is contributed by PratikBasu

Java




// Java program to find the sum of the
// first N centered heptagonal numbers
class GFG{
     
// Function to find the N-th centered
// heptagonal number
public static int center_heptagonal_num(int n)
{
 
    // Formula to calculate
    // nth centered heptagonal
    // number
    return (7 * n * n - 7 * n + 2) / 2;
}
 
// Function to find the sum of the first
// N centered heptagonal numbers
public static int sum_center_heptagonal_num(int n)
{
 
    // Variable to store
    // the sum
    int summ = 0;
 
    // Iterating through the range
    // 1 to N
    for(int i = 1; i < n + 1; i++)
    {
        summ += center_heptagonal_num(i);
    }
    return summ;
}
 
// Driver Code
public static void main(String args[])
{
    int n = 5;
 
    System.out.print(sum_center_heptagonal_num(n));
}
}
 
// This code is contributed by Code_Mech

Python3




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

C#




// C# program to find the sum of the
// first N centered heptagonal numbers
using System;
 
class GFG{
     
// Function to find the N-th centered
// heptagonal number
public static int center_heptagonal_num(int n)
{
 
    // Formula to calculate
    // nth centered heptagonal
    // number
    return (7 * n * n - 7 * n + 2) / 2;
}
 
// Function to find the sum of the first
// N centered heptagonal numbers
public static int sum_center_heptagonal_num(int n)
{
 
    // Variable to store
    // the sum
    int summ = 0;
 
    // Iterating through the range
    // 1 to N
    for(int i = 1; i < n + 1; i++)
    {
       summ += center_heptagonal_num(i);
    }
    return summ;
}
 
// Driver Code
public static void Main()
{
    int n = 5;
 
    Console.Write(sum_center_heptagonal_num(n));
}
}
 
// This code is contributed by Akanksha_Rai

Javascript




<script>
 
    // Javascript program to find the sum of the 
    // first N centered heptagonal numbers
     
    // Function to find the N-th centered
    // heptagonal number 
    function center_heptagonal_num(n)
    {
 
        // Formula to calculate
        // nth centered heptagonal 
        // number
        return (7 * n * n - 7 * n + 2) / 2;
    }
 
    // Function to find the sum of the first
    // N centered heptagonal numbers
    function sum_center_heptagonal_num(n)
    {
 
        // Variable to store
        // the sum
        let summ = 0;
 
        // Iterating through the range
        // 1 to N
        for(let i = 1; i < n + 1; i++)
        {
           summ += center_heptagonal_num(i);
        }
        return summ;
    }
     
    let n = 5;
   
    document.write(sum_center_heptagonal_num(n));
 
</script>
 
// This code is contributed by divyeshrabadiya07.
Output: 
145

 

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 industry experts, please refer Geeks Classes Live




My Personal Notes arrow_drop_up
Recommended Articles
Page :