Given an integer **N**, the task is to find all numbers up to **N**, which are both Pentagonal as well as Hexagonal.

**Example:**

Input:N = 1000

Output:1

Input:N = 100000

Output:1, 40755

**Approach:**

- To solve the problem, we are generating all pentagonal numbers up to
**N**and check if those are hexagonal numbers or not. - Formula to calculate
**i**:^{th}Pentagonal Number

**i * ( 3 * i – 1 ) / 2** -
To check if a pentagonal number, say
**pn**, is a hexagonal number or not:**( 1 + sqrt(8 * pn + 1 ) ) / 4**needs to be a Natural number

Below is the implementation of the above approach:

## C++

`// C++ Program of the above approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to print numbers upto N ` `// which are both pentagonal as well ` `// as hexagonal numbers ` `void` `pen_hex(` `long` `long` `n) ` `{ ` ` ` `long` `long` `pn = 1; ` ` ` `for` `(` `long` `long` `int` `i = 1;; i++) { ` ` ` ` ` `// Calculate i-th pentagonal number ` ` ` `pn = i * (3 * i - 1) / 2; ` ` ` ` ` `if` `(pn > n) ` ` ` `break` `; ` ` ` ` ` `// Check if the pentagonal number ` ` ` `// pn is hexagonal or not ` ` ` `long` `double` `seqNum ` ` ` `= (1 + ` `sqrt` `(8 * pn + 1)) / 4; ` ` ` ` ` `if` `(seqNum == ` `long` `(seqNum)) ` ` ` `cout << pn << ` `", "` `; ` ` ` `} ` `} ` ` ` `// Driver Program ` `int` `main() ` `{ ` ` ` `long` `long` `int` `N = 1000000; ` ` ` `pen_hex(N); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program of the above approach ` `import` `java.util.*; ` ` ` `class` `GFG{ ` ` ` `// Function to print numbers upto N ` `// which are both pentagonal as well ` `// as hexagonal numbers ` `static` `void` `pen_hex(` `long` `n) ` `{ ` ` ` `long` `pn = ` `1` `; ` ` ` `for` `(` `long` `i = ` `1` `; i < n; i++) ` ` ` `{ ` ` ` ` ` `// Calculate i-th pentagonal number ` ` ` `pn = i * (` `3` `* i - ` `1` `) / ` `2` `; ` ` ` ` ` `if` `(pn > n) ` ` ` `break` `; ` ` ` ` ` `// Check if the pentagonal number ` ` ` `// pn is hexagonal or not ` ` ` `double` `seqNum = (` `1` `+ Math.sqrt( ` ` ` `8` `* pn + ` `1` `)) / ` `4` `; ` ` ` ` ` `if` `(seqNum == (` `long` `)seqNum) ` ` ` `System.out.print(pn + ` `", "` `); ` ` ` `} ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `long` `N = ` `1000000` `; ` ` ` `pen_hex(N); ` `} ` `} ` ` ` `// This code is contributed by offbeat ` |

*chevron_right*

*filter_none*

**Output:**

1, 40755,

**Time Complexity:** O(N)

**Auxiliary space:** O(1)

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.

## Recommended Posts:

- Second Pentagonal numbers
- Second hexagonal numbers
- Concentric Hexagonal Numbers
- Find the sum of the first N Centered Pentagonal Number
- n'th Pentagonal Number
- Centered pentagonal number
- Pentagonal Pyramidal Number
- Hexagonal Number
- Program to check if N is a Pentagonal Number
- Centered hexagonal number
- Program to check if N is a Centered Pentagonal Number or not
- Program to check if N is a Hexagonal Number or not
- Surface Area and Volume of Hexagonal Prism
- Program to check if N is a Concentric Hexagonal Number
- Program to check if N is a Centered Hexagonal Number
- Find two numbers whose sum and GCD are given
- Given two numbers a and b find all x such that a % x = b
- Find three prime numbers with given sum
- To find sum of two numbers without using any operator
- Find k numbers which are powers of 2 and have sum N | Set 1

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.