Given a number **N**, the task is to find distinct **N** numbers such that their product is a perfect cube.

**Examples:**

Input:N = 3

Output:1, 8, 27

Explanation:

Product of the output numbers = 1 * 8 * 27 = 216, which is the perfect cube of 6 (6^{3}= 216)

Input:N = 2

Output:1 8

Explanation:

Product of the output numbers = 1 * 8 = 8, which is the perfect cube of 2 (2^{3}= 8)

**Approach:** The solution is based on the fact that

The product of the first ‘N’ Perfect Cube numbers is always a Perfect Cube.

So, the Perfect Cube of first N natural numbers will be printed as the output.

**For example:**

For N = 1 => [1] Product is 1 and cube root of 1 is also 1 For N = 2 => [1, 8] Product is 8 and cube root of 8 is 2 For N = 3 => [1, 8, 27] Product is 216 and cube root of 216 is 6 and so on

Below is the implementation of the above approach:

## C++

`// C++ program to find N numbers such that ` `// their product is a perfect cube ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find the N numbers such ` `//that their product is a perfect cube ` `void` `findNumbers(` `int` `N) ` `{ ` ` ` `int` `i = 1; ` ` ` ` ` `// Loop to traverse each ` `//number from 1 to N ` ` ` `while` `(i <= N) { ` ` ` `// Print the cube of i ` `//as the ith term of the output ` ` ` `cout << (i * i * i) ` ` ` `<< ` `" "` `; ` ` ` ` ` `i++; ` ` ` `} ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `N = 4; ` ` ` ` ` `// Function Call ` ` ` `findNumbers(N); ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find N numbers such that ` `// their product is a perfect cube ` `import` `java.util.*; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to find the N numbers such ` `//that their product is a perfect cube ` `static` `void` `findNumbers(` `int` `N) ` `{ ` ` ` `int` `i = ` `1` `; ` ` ` ` ` `// Loop to traverse each ` ` ` `//number from 1 to N ` ` ` `while` `(i <= N) { ` ` ` ` ` `// Print the cube of i ` ` ` `//as the ith term of the output ` ` ` `System.out.print( (i * i * i) ` ` ` `+ ` `" "` `); ` ` ` ` ` `i++; ` ` ` `} ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main (String []args) ` `{ ` ` ` `int` `N = ` `4` `; ` ` ` ` ` `// Function Call ` ` ` `findNumbers(N); ` `} ` `} ` ` ` `// This code is contributed by chitranayal ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to find N numbers such that ` `# their product is a perfect cube ` ` ` `# Function to find the N numbers such ` `# that their product is a perfect cube ` `def` `findNumbers(N): ` ` ` `i ` `=` `1` ` ` ` ` `# Loop to traverse each ` ` ` `# number from 1 to N ` ` ` `while` `(i <` `=` `N): ` ` ` ` ` `# Print the cube of i ` ` ` `# as the ith term of the output ` ` ` `print` `((i ` `*` `i ` `*` `i), end` `=` `" "` `) ` ` ` ` ` `i ` `+` `=` `1` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `N ` `=` `4` ` ` ` ` `# Function Call ` ` ` `findNumbers(N) ` ` ` `# This code is contributed by mohit kumar 29 ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find N numbers such that ` `// their product is a perfect cube ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to find the N numbers such ` `//that their product is a perfect cube ` `static` `void` `findNumbers(` `int` `N) ` `{ ` ` ` `int` `i = 1; ` ` ` ` ` `// Loop to traverse each ` ` ` `//number from 1 to N ` ` ` `while` `(i <= N) { ` ` ` ` ` `// Print the cube of i ` ` ` `//as the ith term of the output ` ` ` `Console.Write( (i * i * i) ` ` ` `+ ` `" "` `); ` ` ` ` ` `i++; ` ` ` `} ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main (` `string` `[]args) ` `{ ` ` ` `int` `N = 4; ` ` ` ` ` `// Function Call ` ` ` `findNumbers(N); ` `} ` `} ` ` ` `// This code is contributed by Yash_R ` |

*chevron_right*

*filter_none*

**Output:**

1 8 27 64

**Performance Analysis:**

**Time Complexity:**As in the above approach, we are finding the Perfect Cube of N numbers, therefore it will take**O(N)**time.**Auxiliary Space Complexity:**As in the above approach, there are no extra space used; therefore the Auxiliary Space complexity will be**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:

- Count numbers upto N which are both perfect square and perfect cube
- Construct an Array such that cube sum of all element is a perfect square
- Check if number formed by joining two Numbers is Perfect Cube
- Count of pairs in a given range with sum of their product and sum equal to their concatenated number
- Numbers less than N that are perfect cubes and the sum of their digits reduced to a single digit is 1
- Count all triplets whose sum is equal to a perfect cube
- Perfect cube greater than a given number
- Largest number in an array that is not a perfect cube
- Largest perfect cube number in an Array
- Smallest perfect Cube divisible by all elements of an array
- Smallest perfect cube in an array
- Number of times the largest Perfect Cube can be subtracted from N
- Least number to be added to or subtracted from N to make it a Perfect Cube
- Perfect Cube String
- Check whether N can be a Perfect Cube after adding or subtracting K
- Count of pairs in an Array whose sum is a Perfect Cube
- Perfect Cube
- Previous perfect square and cube number smaller than number N
- Construct an Array of size N whose sum of cube of all elements is a perfect square
- Count of subarrays having sum as a perfect cube

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.