Find the sum of the first N Centered Pentagonal Number

Given a number N, the task is to find the sum of first N Centered Pentagonal Numbers.

The first few Centered Pentagonal Number are 1, 6, 16, 31, 51, 76, 106 …

Examples:

Input: N = 3
Output: 23
Explanation:
1, 6 and 16 are the first three
Centered Pentagonal number.

Input: N = 5
Output: 105



Approach: The idea is to first create a function which would help us to find the centered pentagonal number in a constant time. The implementation of this function has already been discussed in this article. The following steps are followed after creating this function:

  1. Run a loop starting from 1 to N, to find ith Centered Pentagonal number.
  2. Add all the above calculated Centered Pentagonal numbers.
  3. Then, display the sum of N Centered Pentagonalnumbers.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to find the sum of the 
// first N centered pentagonal numbers 
#include<bits/stdc++.h> 
using namespace std; 
  
// Function to find the 
// Centered_Pentagonal number 
int Centered_Pentagonal_num(int n)
{
  
    // Formula to calculate 
    // nth Centered_Pentagonal 
    // number & return it 
    // into main function. 
    return (5 * n * n - 5 * n + 2) / 2;
}
  
// Function to find the sum of the first
// N Centered_Pentagonal numbers 
int sum_Centered_Pentagonal_num(int n)
{
  
    // To get the sum
    int summ = 0;
  
    // Iterating through the range
    // 1 to N
    for(int i = 1; i < n + 1; i++)
    {
       summ += Centered_Pentagonal_num(i);
    }
    return summ;
}
  
// Driver Code
int main()
{
    int n = 5;
  
    // Display first Nth 
    // Centered_Pentagonal number 
    cout << (sum_Centered_Pentagonal_num(n));
    return 0;
}
  
// This code is contributed by PratikBasu

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find the sum of the 
// first N centered pentagonal numbers 
class GFG{ 
  
// Function to find the 
// Centered_Pentagonal number 
static int Centered_Pentagonal_num(int n)
{
  
    // Formula to calculate 
    // nth Centered_Pentagonal 
    // number & return it 
    // into main function. 
    return (5 * n * n - 5 * n + 2) / 2;
}
  
// Function to find the sum of the first
// N Centered_Pentagonal numbers 
static int sum_Centered_Pentagonal_num(int n)
{
  
    // To get the sum
    int summ = 0;
  
    // Iterating through the range
    // 1 to N
    for(int i = 1; i < n + 1; i++)
    {
        summ += Centered_Pentagonal_num(i);
    }
    return summ;
}
  
// Driver Code
public static void main(String[] args)
{
    int n = 5;
  
    // Display first Nth 
    // Centered_Pentagonal number 
    System.out.print((sum_Centered_Pentagonal_num(n)));
}
}
  
// This code is contributed by sapnasingh4991

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to find the sum of 
# the first N Centered
# Pentagonal number
  
# Function to find the 
# Centered_Pentagonal number 
def Centered_Pentagonal_num(n): 
  
    # Formula to calculate  
    # nth Centered_Pentagonal 
    # number & return it 
    # into main function. 
    return (5 * n * n - 
            5 * n + 2) // 2
      
    
# Function to find the 
# sum of the first N
# Centered_Pentagonal 
# numbers
def sum_Centered_Pentagonal_num(n) : 
      
    # To get the sum
    summ = 0
      
    for i in range(1, n + 1):
  
        # Function to get the 
        # Centered_Pentagonal_num 
        summ += Centered_Pentagonal_num(i)
      
    return summ
    
# Driver Code 
if __name__ == '__main__'
            
    n = 5
      
    # display first Nth 
    # Centered_Pentagonal number
    print(sum_Centered_Pentagonal_num(n)) 

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find the sum of the 
// first N centered pentagonal numbers 
using System;
  
class GFG{ 
  
// Function to find the 
// Centered_Pentagonal number 
static int Centered_Pentagonal_num(int n)
{
  
    // Formula to calculate 
    // nth Centered_Pentagonal 
    // number & return it 
    // into main function. 
    return (5 * n * n - 5 * n + 2) / 2;
}
  
// Function to find the sum of the first
// N Centered_Pentagonal numbers 
static int sum_Centered_Pentagonal_num(int n)
{
  
    // To get the sum
    int summ = 0;
  
    // Iterating through the range
    // 1 to N
    for(int i = 1; i < n + 1; i++)
    {
       summ += Centered_Pentagonal_num(i);
    }
    return summ;
}
  
// Driver code
public static void Main(String[] args)
{
    int n = 5;
  
    // Display first Nth 
    // Centered_Pentagonal number 
    Console.Write((sum_Centered_Pentagonal_num(n)));
}
}
  
// This code is contributed by amal kumar choubey

chevron_right


Output:

105

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.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.