Pentagonal Pyramidal Number
Last Updated :
23 Sep, 2022
Given a number n, find the nth pentagonal pyramidal number.
A Pentagonal Pyramidal Number belongs to the figurate number class. It is the number of objects in a pyramid with a pentagonal base. The nth pentagonal pyramidal number is equal to sum of first n pentagonal numbers.
Examples:
Input : n = 3
Output : 18
Input : n = 7
Output : 196
Method 1: (Naive Approach) :
This approach is simple. It says to add all the pentagonal numbers up to n (by running loop) to get nth Pentagonal pyramidal number.
Below is the implementation of this approach:
C++
#include <bits/stdc++.h>
using namespace std;
int pentagon_pyramidal( int n)
{
int sum = 0;
for ( int i = 1; i <= n; i++) {
int p = (3 * i * i - i) / 2;
sum = sum + p;
}
return sum;
}
int main()
{
int n = 4;
cout << pentagon_pyramidal(n) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int pentagon_pyramidal( int n)
{
int sum = 0 ;
for ( int i = 1 ; i <= n; i++)
{
int p = ( 3 * i * i - i) / 2 ;
sum = sum + p;
}
return sum;
}
public static void main (String[] args)
{
int n = 4 ;
System.out.println(pentagon_pyramidal(n));
}
}
|
Python3
def pentagon_pyramidal(n):
sum = 0
for i in range ( 1 , n + 1 ):
p = ( 3 * i * i - i ) / 2
sum = sum + p
return sum
n = 4
print ( int (pentagon_pyramidal(n)))
|
C#
using System;
class GFG
{
static int pentagon_pyramidal( int n)
{
int sum = 0;
for ( int i = 1; i <= n; i++)
{
int p = (3 * i *
i - i) / 2;
sum = sum + p;
}
return sum;
}
static public void Main ()
{
int n = 4;
Console.WriteLine(pentagon_pyramidal(n));
}
}
|
PHP
<?php
function pentagon_pyramidal( $n )
{
$sum = 0;
for ( $i = 1; $i <= $n ; $i ++)
{
$p = (3 * $i *
$i - $i ) / 2;
$sum = $sum + $p ;
}
return $sum ;
}
$n = 4;
echo pentagon_pyramidal( $n );
?>
|
Javascript
<script>
function pentagon_pyramidal(n)
{
var sum = 0;
for (i = 1; i <= n; i++)
{
var p = (3 * i * i - i) / 2;
sum = sum + p;
}
return sum;
}
var n = 4;
document.write(pentagon_pyramidal(n));
</script>
|
Output :
40
Time Complexity: O(n)
Auxiliary space: O(1)
Method 2: (Efficient Approach) :
In this approach, we use formula to get nth Pentagonal pyramidal number in O(1) time.
nth Pentagonal pyramidal number = n2 (n + 1) / 2
Below is the implementation of this approach:
C++
#include <bits/stdc++.h>
using namespace std;
int pentagon_pyramidal( int n)
{
return n * n * (n + 1) / 2;
}
int main()
{
int n = 4;
cout << pentagon_pyramidal(n) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int pentagon_pyramidal( int n)
{
return n * n *
(n + 1 ) / 2 ;
}
public static void main (String[] args)
{
int n = 4 ;
System.out.println(pentagon_pyramidal(n));
}
}
|
Python3
def pentagon_pyramidal(n):
return n * n * (n + 1 ) / 2
n = 4
print ( int (pentagon_pyramidal(n)))
|
C#
using System;
class GFG
{
static int pentagon_pyramidal( int n)
{
return n * n *
(n + 1) / 2;
}
static public void Main ()
{
int n = 4;
Console.WriteLine(
pentagon_pyramidal(n));
}
}
|
PHP
<?php
function pentagon_pyramidal( $n )
{
return $n * $n *
( $n + 1) / 2;
}
$n = 4;
echo pentagon_pyramidal( $n );
?>
|
Javascript
<script>
function pentagon_pyramidal(n)
{
return n * n *
(n + 1) / 2;
}
var n = 4;
document.write(pentagon_pyramidal(n));
</script>
|
Time Complexity: O(1)
Space complexity: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...