Skip to content
Related Articles

Related Articles

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

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++




// 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

Java




// 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

Python3




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

C#




// 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

Javascript




<script>
 
    // Javascript program to find the sum of the 
    // first N centered pentagonal numbers
     
    // Function to find the 
    // Centered_Pentagonal number 
    function 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 
    function sum_Centered_Pentagonal_num(n)
    {
 
        // To get the sum
        let summ = 0;
 
        // Iterating through the range
        // 1 to N
        for(let i = 1; i < n + 1; i++)
        {
           summ += Centered_Pentagonal_num(i);
        }
        return summ;
    }
 
    let n = 5;
   
    // Display first Nth 
    // Centered_Pentagonal number
    document.write(sum_Centered_Pentagonal_num(n));
     
</script>
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.  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 DSA Live Classes




My Personal Notes arrow_drop_up
Recommended Articles
Page :