Dado un número N , la tarea es encontrar N números distintos tales que su producto sea un cubo perfecto .
Ejemplos:
Entrada: N = 3
Salida: 1, 8, 27
Explicación:
Producto de los números de salida = 1 * 8 * 27 = 216, que es el cubo perfecto de 6 (6 3 = 216)
Entrada: N = 2
Salida: 1 8
Explicación:
Producto de los números de salida = 1 * 8 = 8, que es el cubo perfecto de 2 (2 3 = 8)
Enfoque: La solución se basa en el hecho de que
El producto de los primeros números ‘N’ Perfect Cube es siempre un Perfect Cube.
Entonces, el cubo perfecto de los primeros N números naturales se imprimirá como salida.
Por ejemplo:
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
A continuación se muestra la implementación del enfoque anterior:
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); }
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
<script> // JavaScript 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 function findNumbers(N) { let 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 document.write((i * i * i) + " "); i++; } } // Driver Code let N = 4; // Function Call findNumbers(N); // This code is contributed by Manoj. </script>
Producción:
1 8 27 64
Análisis de rendimiento:
- Complejidad de tiempo: como en el enfoque anterior, estamos encontrando el cubo perfecto de N números, por lo tanto, tomará O (N) tiempo.
- Complejidad del espacio auxiliar: como en el enfoque anterior, no se utiliza espacio adicional; por lo tanto, la complejidad del Espacio Auxiliar será O(1) .