Given a number N the task is to find the sum of the first N Centered Pentadecagonal Number.
The first few Centered Pentadecagonal Numbers are 1, 16, 46, 91, 151, 226, 316 …
Examples:
Input: N = 3
Output: 63
Explanation:
1, 16 and 46 are the first three centered pentadecagonal numbers.Input: N = 5
Output: 305
Approach:
- Initially, we need to create a function which will help us to calculate the Nth centered Pentadecagonal number.
- Now, run a loop starting from 1 to N, to find ith Centered Pentadecagonal number.
- Add all the above calculated Centered Pentadecagonal numbers.
- Finally, display the sum of 1st N Centered Pentadecagonal numbers.
Below is the implementation of the above approach:
C++
// C++ program to find the sum of the // first N centered pentadecagonal number #include<bits/stdc++.h> using namespace std;
// Function to find the centered // pentadecagonal number int Centered_Pentadecagonal_num( int n)
{ // Formula to calculate
// N-th centered pentadecagonal
// number
return (15 * n * n - 15 * n + 2) / 2;
} // Function to find the sum of // the first N centered // pentadecagonal numbers int sum_Centered_Pentadecagonal_num( int n)
{ // Variable to store
// the sum
int summ = 0;
for ( int i = 1; i < n + 1; i++)
{
summ += Centered_Pentadecagonal_num(i);
}
return summ;
} // Driver Code int main()
{ int n = 5;
cout << sum_Centered_Pentadecagonal_num(n);
return 0;
} // This code is contributed by Rajput-Ji |
Java
// Java program to find the sum of the // first N centered pentadecagonal number class GFG {
// Function to find the centered // pentadecagonal number static int Centered_Pentadecagonal_num( int n)
{ // Formula to calculate
// N-th centered pentadecagonal
// number
return ( 15 * n * n - 15 * n + 2 ) / 2 ;
} // Function to find the sum of // the first N centered // pentadecagonal numbers static int sum_Centered_Pentadecagonal_num( int n)
{ // Variable to store
// the sum
int summ = 0 ;
for ( int i = 1 ; i < n + 1 ; i++)
{
summ += Centered_Pentadecagonal_num(i);
}
return summ;
} // Driver Code public static void main(String[] args)
{ int n = 5 ;
System.out.println(sum_Centered_Pentadecagonal_num(n));
} } // This code is contributed by sapnasingh4991 |
Python3
# Python3 program to find the sum # of the first N centered # Pentadecagonal number # Function to find the # Centered_Pentadecagonal # number def Centered_Pentadecagonal_num(n):
# Formula to calculate
# N-th Centered_Pentadecagonal
# number
return ( 15 * n * n -
15 * n + 2 ) / / 2
# Function to find the # sum of the first N # Centered_Pentadecagonal # numbers def sum_Centered_Pentadecagonal_num(n) :
# Variable to store
# the sum
summ = 0
for i in range ( 1 , n + 1 ):
summ + = Centered_Pentadecagonal_num(i)
return summ
# Driver code if __name__ = = '__main__' :
n = 5
print (sum_Centered_Pentadecagonal_num(n))
|
C#
// C# program to find the sum of the // first N centered pentadecagonal number using System;
class GFG
{ // Function to find the centered // pentadecagonal number static int Centered_Pentadecagonal_num( int n)
{ // Formula to calculate
// N-th centered pentadecagonal
// number
return (15 * n * n - 15 * n + 2) / 2;
} // Function to find the sum of // the first N centered // pentadecagonal numbers static int sum_Centered_Pentadecagonal_num( int n)
{ // Variable to store
// the sum
int summ = 0;
for ( int i = 1; i < n + 1; i++)
{
summ += Centered_Pentadecagonal_num(i);
}
return summ;
} // Driver Code public static void Main(String[] args)
{ int n = 5;
Console.WriteLine(sum_Centered_Pentadecagonal_num(n));
} } // This code is contributed by sapnasingh4991 |
Javascript
<script> // Javascript program to find the sum of the
// first N centered pentadecagonal number
// Function to find the centered
// pentadecagonal number
function Centered_Pentadecagonal_num(n)
{
// Formula to calculate
// N-th centered pentadecagonal
// number
return (15 * n * n - 15 * n + 2) / 2;
}
// Function to find the sum of
// the first N centered
// pentadecagonal numbers
function sum_Centered_Pentadecagonal_num(n)
{
// Variable to store
// the sum
let summ = 0;
for (let i = 1; i < n + 1; i++)
{
summ += Centered_Pentadecagonal_num(i);
}
return summ;
}
let n = 5;
document.write(sum_Centered_Pentadecagonal_num(n));
</script> |
Output:
305
Time Complexity: O(N)
Auxiliary Space: O(1) as it is using constant space for variables
Article Tags :
Recommended Articles