# Pentagonal Pyramidal Number

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

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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

 `// CPP Program to get nth Pentagonal ` `// pyramidal number. ` `#include ` `using` `namespace` `std; ` ` `  `// function to get nth Pentagonal ` `// pyramidal number. ` `int` `pentagon_pyramidal(``int` `n) ` `{ ` `    ``int` `sum = 0; ` ` `  `    ``// Running loop from 1 to n ` `    ``for` `(``int` `i = 1; i <= n; i++) { ` ` `  `        ``// get nth pentagonal number ` `        ``int` `p = (3 * i * i - i) / 2; ` ` `  `        ``// add to sum ` `        ``sum = sum + p; ` `    ``} ` `    ``return` `sum; ` `} ` ` `  `// Driver Program ` `int` `main() ` `{ ` `    ``int` `n = 4; ` `    ``cout << pentagon_pyramidal(n) << endl; ` `    ``return` `0; ` `} `

## Java

 `// Java Program to get nth  ` `// Pentagonal pyramidal number. ` `import` `java.io.*; ` ` `  `class` `GFG  ` `{ ` ` `  `// function to get nth  ` `// Pentagonal pyramidal number. ` `static` `int` `pentagon_pyramidal(``int` `n) ` `{ ` `    ``int` `sum = ``0``; ` ` `  `    ``// Running loop from 1 to n ` `    ``for` `(``int` `i = ``1``; i <= n; i++)  ` `    ``{ ` ` `  `        ``// get nth pentagonal number ` `        ``int` `p = (``3` `* i * i - i) / ``2``; ` ` `  `        ``// add to sum ` `        ``sum = sum + p; ` `    ``} ` `    ``return` `sum; ` `} ` ` `  `// Driver Code ` `public` `static` `void` `main (String[] args)  ` `{ ` `    ``int` `n = ``4``; ` `    ``System.out.println(pentagon_pyramidal(n)); ` `} ` `} ` ` `  `// This code is contributed by anuj_67. `

## Python3

 `# Python3 Program to get nth Pentagonal  ` `# pyramidal number. ` `   `  `# function to get nth Pentagonal  ` `# pyramidal number. ` `def` `pentagon_pyramidal(n): ` `    ``sum` `=` `0` ` `  `    ``# Running loop from 1 to n  ` `    ``for` `i ``in` `range``(``1``, n ``+` `1``): ` `   `  `        ``# get nth pentagonal number ` `        ``p ``=` `( ``3` `*` `i ``*` `i ``-` `i ) ``/` `2` ` `  `        ``# add to sum ` `        ``sum` `=` `sum` `+` `p        ` `  `  `    ``return` `sum` ` `  `   `  `# Driver Program ` `n ``=` `4` `print``(``int``(pentagon_pyramidal(n))) `

## C#

 `// C# Program to get nth  ` `// Pentagonal pyramidal number. ` `using` `System; ` ` `  `class` `GFG ` `{ ` `     `  `// function to get nth  ` `// Pentagonal pyramidal number. ` `static` `int` `pentagon_pyramidal(``int` `n) ` `{ ` `    ``int` `sum = 0; ` ` `  `    ``// Running loop from 1 to n ` `    ``for` `(``int` `i = 1; i <= n; i++)  ` `    ``{ ` ` `  `        ``// get nth pentagonal number ` `        ``int` `p = (3 * i *  ` `                 ``i - i) / 2; ` ` `  `        ``// add to sum ` `        ``sum = sum + p; ` `    ``} ` `    ``return` `sum; ` `} ` ` `  `// Driver Code ` `static` `public` `void` `Main () ` `{ ` `    ``int` `n = 4; ` `    ``Console.WriteLine(pentagon_pyramidal(n)); ` `} ` `} ` ` `  `// This code is contributed by ajit. `

## PHP

 ` `

Output :

```40
```

Time Complexity : O(n)

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

 `// CPP Program to get nth Pentagonal ` `// pyramidal number. ` `#include ` `using` `namespace` `std; ` ` `  `// function to get nth Pentagonal ` `// pyramidal number. ` `int` `pentagon_pyramidal(``int` `n) ` `{ ` `    ``return` `n * n * (n + 1) / 2; ` `} ` ` `  `// Driver Program ` `int` `main() ` `{ ` `    ``int` `n = 4; ` `    ``cout << pentagon_pyramidal(n) << endl; ` `    ``return` `0; ` `} `

## Java

 `// Java Program to get nth  ` `// Pentagonal pyramidal number. ` `import` `java.io.*; ` ` `  `class` `GFG  ` `{ ` `     `  `// function to get nth  ` `// Pentagonal pyramidal number. ` `static` `int` `pentagon_pyramidal(``int` `n) ` `{ ` `    ``return` `n * n *  ` `          ``(n + ``1``) / ``2``; ` `} ` ` `  `// Driver Code ` `public` `static` `void` `main (String[] args) ` `{ ` `    ``int` `n = ``4``; ` `    ``System.out.println(pentagon_pyramidal(n)); ` `} ` `} ` ` `  `// This code is contributed by ajit `

## Python3

 `# Python3 Program to get nth Pentagonal  ` `# pyramidal number. ` `   `  `# function to get nth Pentagonal  ` `# pyramidal number. ` `def` `pentagon_pyramidal(n):      ` `    ``return` `n ``*` `n ``*` `(n ``+` `1``) ``/` `2` ` `  `   `  `# Driver Program ` `n ``=` `4` `print``(``int``(pentagon_pyramidal(n))) `

## C#

 `// C# Program to get nth  ` `// Pentagonal pyramidal number. ` `using` `System; ` ` `  `class` `GFG ` `{ ` `     `  `// function to get nth  ` `// Pentagonal pyramidal number. ` `static` `int` `pentagon_pyramidal(``int` `n) ` `{ ` `    ``return` `n * n *  ` `          ``(n + 1) / 2; ` `} ` ` `  `// Driver Code ` `static` `public` `void` `Main () ` `{ ` `    ``int` `n = 4; ` `    ``Console.WriteLine( ` `            ``pentagon_pyramidal(n)); ` `} ` `} ` ` `  `// This code is contributed ` `// by ajit `

## PHP

 ` `

Output :

```40
```

Time Complexity : O(1)

My Personal Notes arrow_drop_up 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.

Improved By : vt_m, jit_t