Open In App

Find the sum of the first N Centered heptagonal number

Last Updated : 17 Aug, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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

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

Iterative Approach:

In this approach, iterate from 1 to N and calculate the centered heptagonal numbers iteratively. Keep adding each heptagonal 

number to the sum.

Below is the implementation of the above approach: 

C++




// C++ program of the above approach
 
#include <iostream>
using namespace std;
 
int sumOfCenteredHeptagonalNumbers(int N)
{
    int sum = 0;
    int n = 1;
 
    for (int i = 0; i < N; i++) {
        // Calculate the heptagonal number
        int heptagonal = (7 * n * n - 7 * n + 2) / 2;
        sum += heptagonal;
        // Increment n by 1 for the next iteration
        n++;
    }
    // Return the sum of the first N centered heptagonal
    // numbers
    return sum;
}
 
// Driver Code
int main()
{
    int N = 5;
    int result = sumOfCenteredHeptagonalNumbers(N);
    cout << result << endl;
 
    return 0;
}


Java




public class GFG {
        static int sumOfCenteredHeptagonalNumbers(int N) {
        int sum = 0;
        int n = 1;
 
        for (int i = 0; i < N; i++) {
            // Calculate the heptagonal number
            int heptagonal = (7 * n * n - 7 * n + 2) / 2;
            sum += heptagonal;
            // Increment n by 1 for the next iteration
            n++;
        }
        // Return the sum of the first N centered heptagonal numbers
        return sum;
    }
 
    public static void main(String[] args) {
        int N = 5;
        int result = sumOfCenteredHeptagonalNumbers(N);
        System.out.println(result);
    }
}


Python3




def sum_of_centered_heptagonal_numbers(N):
    sum = 0
    n = 1
 
    for i in range(N):
        # Calculate the heptagonal number
        heptagonal = (7 * n * n - 7 * n + 2) // 2
        sum += heptagonal
        # Increment n by 1 for the next iteration
        n += 1
     
    # Return the sum of the first N centered heptagonal numbers
    return sum
 
# Driver Code
N = 5
result = sum_of_centered_heptagonal_numbers(N)
print(result)


C#




using System;
 
class GFG
{
    static int SumOfCenteredHeptagonalNumbers(int N)
    {
        int sum = 0;
        int n = 1;
 
        for (int i = 0; i < N; i++)
        {
            // Calculate the heptagonal number
            int heptagonal = (7 * n * n - 7 * n + 2) / 2;
            sum += heptagonal;
            // Increment n by 1 for the next iteration
            n++;
        }
 
        // Return the sum of the first N centered heptagonal numbers
        return sum;
    }
 
    static void Main()
    {
        int N = 5;
        int result = SumOfCenteredHeptagonalNumbers(N);
        Console.WriteLine(result);
 
        // Keep the console window open
        Console.ReadLine();
    }
}


Javascript




function sumOfCenteredHeptagonalNumbers(N) {
    let sum = 0;
    let n = 1;
 
    for (let i = 0; i < N; i++) {
        // Calculate the heptagonal number
        let heptagonal = (7 * n * n - 7 * n + 2) / 2;
        sum += heptagonal;
        // Increment n by 1 for the next iteration
        n++;
    }
    // Return the sum of the first N centered heptagonal numbers
    return sum;
}
 
// Driver Code
    let N = 5;
    let result = sumOfCenteredHeptagonalNumbers(N);
    console.log(result);


Output

145









Time Complexity: O(N), where N is the input value. 
Auxiliary Space: O(1)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads