Open In App

# Print N numbers such that their product is a Perfect Cube

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 (63 = 216)
Input: N = 2
Output: 1 8
Explanation:
Product of the output numbers = 1 * 8 = 8, which is the perfect cube of 2 (23 = 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 =>     Product is 1    and cube root of 1 is also 1For N = 2 => [1, 8]    Product is 8    and cube root of 8 is 2For N = 3 => [1, 8, 27]    Product is 216    and cube root of 216 is 6and 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 ``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);``}`

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

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

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

## Javascript

 ``

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)

Approach#2: Using math

This approach iterates through numbers starting from 1 and checks if their cube root is an integer. If it is, then the number is a perfect cube and is printed. The loop continues until n such perfect cubes have been printed.

#### Algorithm

1. Initialize the count to 0 and i to 1.
2. While count is less than n, do the following:
a. Check if the cube root of i is an integer by comparing the float value of the cube root with its integer value.
b. If the cube root is an integer, print the number i and increment the count.
c. Increment i by 1 after each iteration of the loop.
3. Print a newline character after all the perfect cube numbers have been printed.

## C++

 `#include ``#include ` `using` `namespace` `std;` `void` `print_cube_numbers(``int` `n) {``    ``int` `count = 0;``    ``int` `i = 1;``    ``while` `(count < n) {``        ``int` `cube_root = round(cbrt(i));``        ``if` `(cube_root * cube_root * cube_root == i) {``            ``cout << i << ``" "``;``            ``count++;``        ``}``        ``i++;``    ``}``    ``cout << endl;``}` `int` `main() {``    ``print_cube_numbers(3);``    ``print_cube_numbers(2);``    ``return` `0;``}`

## Java

 `import` `java.util.*;` `public` `class` `GFG {` `    ``// Function to print the first 'n' cube numbers``    ``// where cube_root * cube_root * cube_root = i``    ``public` `static` `void` `printCubeNumbers(``int` `n) {``        ``int` `count = ``0``;``        ``int` `i = ``1``;``        ``while` `(count < n) {``            ``// Calculate the cube root of 'i' using cbrt() method``           ``// from Math class``            ``int` `cubeRoot = (``int``) Math.round(Math.cbrt(i));``            ` `            ``// Check if cube_root * cube_root * cube_root == i``            ``if` `(cubeRoot * cubeRoot * cubeRoot == i) {``                ``System.out.print(i + ``" "``);``                ``count++;``            ``}``            ``i++;``        ``}``        ``System.out.println();``    ``}` `    ``public` `static` `void` `main(String[] args) {``        ``printCubeNumbers(``3``); ``// Print the first 3 cube numbers``        ``printCubeNumbers(``2``); ``// Print the first 2 cube numbers``    ``}``}`

## Python3

 `import` `math` `def` `print_cube_numbers(n):``    ``count ``=` `0``    ``i ``=` `1``    ``while` `count < n:``        ``if` `math.``pow``(i, ``1``/``3``) ``=``=` `int``(math.``pow``(i, ``1``/``3``)):``            ``print``(i, end``=``" "``)``            ``count ``+``=` `1``        ``i ``+``=` `1``    ``print``()`  `print_cube_numbers(``3``)``print_cube_numbers(``2``) `

## C#

 `using` `System;` `class` `Program``{  ``    ``// Function to print the first 'n' cube numbers``    ``// where cube_root * cube_root * cube_root = i``    ``static` `void` `PrintCubeNumbers(``int` `n)``    ``{``        ``int` `count = 0;``        ``int` `i = 1;``        ``while` `(count < n)``        ``{  ``            ``// Calculate the cube root of 'i' using cbrt() method``            ``// from Math class``            ``int` `cubeRoot = (``int``)Math.Round(Math.Cbrt(i));``            ` `            ``// Check if cube_root * cube_root * cube_root == i``            ``if` `(cubeRoot * cubeRoot * cubeRoot == i)``            ``{``                ``Console.Write(i + ``" "``);``                ``count++;``            ``}``            ``i++;``        ``}``        ``Console.WriteLine();``    ``}` `    ``static` `void` `Main()``    ``{``        ``PrintCubeNumbers(3);``        ``PrintCubeNumbers(2);``    ``}``}`

Output

```1 8 27
1 8

```

Time Complexity: O(N^2/3) because it iterates through numbers from 1 to N and checks the cube root of each number, which takes O(N^1/3) time. Therefore, the overall time complexity is O(N^2/3).

Auxiliary Space:  O(1) because it uses constant space to store the variables count and i, and no additional data structures are used.