Find the sum of the first N Centered heptagonal number
Last Updated :
17 Aug, 2023
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:
- Initially, we need to create a function which will help us to calculate the Nth centered heptagonal number.
- Now, run a loop starting from 1 to N, to find ith centered heptagonal number.
- Add all the above calculated centered heptagonal numbers.
- Finally, display the sum of the first N centered heptagonal numbers.
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
int center_heptagonal_num( int n)
{
return (7 * n * n - 7 * n + 2) / 2;
}
int sum_center_heptagonal_num( int n)
{
int summ = 0;
for ( int i = 1; i < n + 1; i++)
{
summ += center_heptagonal_num(i);
}
return summ;
}
int main()
{
int n = 5;
cout << (sum_center_heptagonal_num(n));
return 0;
}
|
Java
class GFG{
public static int center_heptagonal_num( int n)
{
return ( 7 * n * n - 7 * n + 2 ) / 2 ;
}
public static int sum_center_heptagonal_num( int n)
{
int summ = 0 ;
for ( int i = 1 ; i < n + 1 ; i++)
{
summ += center_heptagonal_num(i);
}
return summ;
}
public static void main(String args[])
{
int n = 5 ;
System.out.print(sum_center_heptagonal_num(n));
}
}
|
Python3
def center_heptagonal_num(n):
return ( 7 * n * n - 7 * n + 2 ) / / 2
def sum_center_heptagonal_num(n) :
summ = 0
for i in range ( 1 , n + 1 ):
summ + = center_heptagonal_num(i)
return summ
if __name__ = = '__main__' :
n = 5
print (sum_center_heptagonal_num(n))
|
C#
using System;
class GFG{
public static int center_heptagonal_num( int n)
{
return (7 * n * n - 7 * n + 2) / 2;
}
public static int sum_center_heptagonal_num( int n)
{
int summ = 0;
for ( int i = 1; i < n + 1; i++)
{
summ += center_heptagonal_num(i);
}
return summ;
}
public static void Main()
{
int n = 5;
Console.Write(sum_center_heptagonal_num(n));
}
}
|
Javascript
<script>
function center_heptagonal_num(n)
{
return (7 * n * n - 7 * n + 2) / 2;
}
function sum_center_heptagonal_num(n)
{
let summ = 0;
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>
|
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++
#include <iostream>
using namespace std;
int sumOfCenteredHeptagonalNumbers( int N)
{
int sum = 0;
int n = 1;
for ( int i = 0; i < N; i++) {
int heptagonal = (7 * n * n - 7 * n + 2) / 2;
sum += heptagonal;
n++;
}
return sum;
}
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++) {
int heptagonal = ( 7 * n * n - 7 * n + 2 ) / 2 ;
sum += heptagonal;
n++;
}
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):
heptagonal = ( 7 * n * n - 7 * n + 2 ) / / 2
sum + = heptagonal
n + = 1
return sum
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++)
{
int heptagonal = (7 * n * n - 7 * n + 2) / 2;
sum += heptagonal;
n++;
}
return sum;
}
static void Main()
{
int N = 5;
int result = SumOfCenteredHeptagonalNumbers(N);
Console.WriteLine(result);
Console.ReadLine();
}
}
|
Javascript
function sumOfCenteredHeptagonalNumbers(N) {
let sum = 0;
let n = 1;
for (let i = 0; i < N; i++) {
let heptagonal = (7 * n * n - 7 * n + 2) / 2;
sum += heptagonal;
n++;
}
return sum;
}
let N = 5;
let result = sumOfCenteredHeptagonalNumbers(N);
console.log(result);
|
Time Complexity: O(N), where N is the input value.
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...