Dado un número entero N , la tarea es encontrar el cuadrado perfecto anterior o el cubo perfecto más pequeño que el número N.
Ejemplos :
Entrada: N = 6
Salida:
Cuadrado perfecto = 4
Cubo perfecto = 1
Entrada: N = 30
Salida:
Cuadrado perfecto = 25
Cubo perfecto = 27
Enfoque: el número cuadrado perfecto anterior menor que N se puede calcular de la siguiente manera:
- Encuentra la raíz cuadrada del número dado N .
- Calcule su valor mínimo utilizando la función de suelo del idioma respectivo.
- Luego réstale 1 si N ya es un cuadrado perfecto.
- Imprima el cuadrado de ese número.
El número de cubo perfecto anterior menor que N se puede calcular de la siguiente manera:
- Encuentre la raíz cúbica de N dada.
- Calcule su valor mínimo utilizando la función de suelo del idioma respectivo.
- Luego réstale 1 si N ya es un cubo perfecto.
- Imprime el cubo de ese número.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation to find the // previous perfect square and cube // smaller than the given number #include <cmath> #include <iostream> using namespace std; // Function to find the previous // perfect square of the number N int previousPerfectSquare(int N) { int prevN = floor(sqrt(N)); // If N is already a perfect square // decrease prevN by 1. if (prevN * prevN == N) prevN -= 1; return prevN * prevN; } // Function to find the // previous perfect cube int previousPerfectCube(int N) { int prevN = floor(cbrt(N)); // If N is already a perfect cube // decrease prevN by 1. if (prevN * prevN * prevN == N) prevN -= 1; return prevN * prevN * prevN; } // Driver Code int main() { int n = 30; cout << previousPerfectSquare(n) << "\n"; cout << previousPerfectCube(n) << "\n"; return 0; }
Java
// Java implementation to find the // previous perfect square and cube // smaller than the given number import java.util.*; class GFG{ // Function to find the previous // perfect square of the number N static int previousPerfectSquare(int N) { int prevN = (int)Math.floor(Math.sqrt(N)); // If N is already a perfect square // decrease prevN by 1. if (prevN * prevN == N) prevN -= 1; return prevN * prevN; } // Function to find the // previous perfect cube static int previousPerfectCube(int N) { int prevN = (int)Math.floor(Math.cbrt(N)); // If N is already a perfect cube // decrease prevN by 1. if (prevN * prevN * prevN == N) prevN -= 1; return prevN * prevN * prevN; } // Driver Code public static void main(String[] args) { int n = 30; System.out.println(previousPerfectSquare(n)); System.out.println(previousPerfectCube(n)); } } // This code is contributed by Rohit_ranjan
Python3
# Python3 implementation to find the # previous perfect square and cube # smaller than the given number import math import numpy as np # Function to find the previous # perfect square of the number N def previousPerfectSquare(N): prevN = math.floor(math.sqrt(N)); # If N is already a perfect square # decrease prevN by 1. if (prevN * prevN == N): prevN -= 1; return prevN * prevN; # Function to find the # previous perfect cube def previousPerfectCube(N): prevN = math.floor(np.cbrt(N)); # If N is already a perfect cube # decrease prevN by 1. if (prevN * prevN * prevN == N): prevN -= 1; return prevN * prevN * prevN; # Driver Code n = 30; print(previousPerfectSquare(n)); print(previousPerfectCube(n)); # This code is contributed by Code_Mech
C#
// C# implementation to find the // previous perfect square and cube // smaller than the given number using System; class GFG{ // Function to find the previous // perfect square of the number N static int previousPerfectSquare(int N) { int prevN = (int)Math.Floor(Math.Sqrt(N)); // If N is already a perfect square // decrease prevN by 1. if (prevN * prevN == N) prevN -= 1; return prevN * prevN; } // Function to find the // previous perfect cube static int previousPerfectCube(int N) { int prevN = (int)Math.Floor(Math.Cbrt(N)); // If N is already a perfect cube // decrease prevN by 1. if (prevN * prevN * prevN == N) prevN -= 1; return prevN * prevN * prevN; } // Driver Code public static void Main(String[] args) { int n = 30; Console.WriteLine(previousPerfectSquare(n)); Console.WriteLine(previousPerfectCube(n)); } } // This code is contributed by sapnasingh4991
Javascript
<script> // JavaScript implementation to find the // previous perfect square and cube // smaller than the given number // Function to find the previous // perfect square of the number N function previousPerfectSquare(N) { let prevN = Math.floor(Math.sqrt(N)); // If N is already a perfect square // decrease prevN by 1. if (prevN * prevN == N) prevN -= 1; return prevN * prevN; } // Function to find the // previous perfect cube function previousPerfectCube(N) { let prevN = Math.floor(Math.cbrt(N)); // If N is already a perfect cube // decrease prevN by 1. if (prevN * prevN * prevN == N) prevN -= 1; return prevN * prevN * prevN; } // Driver Code let n = 30; document.write(previousPerfectSquare(n) + "<br>"); document.write(previousPerfectCube(n) + "<br>"); // This code is contributed by Manoj. </script>
Producción:
25 27
Complejidad de tiempo: O(sqrt(n))
Espacio Auxiliar: O(1)