Dado un número N , la tarea es verificar si el número N se puede representar como la diferencia de dos cubos consecutivos o no. En caso afirmativo , imprima esos números; de lo contrario, imprima No.
Ejemplos:
Entrada: N = 19
Salida:
Sí
2 3
Explicación:
3 3 – 2 3 = 19Entrada: N = 10
Salida: No
Enfoque: La observación clave en el problema es que un número puede representarse como la diferencia de dos cubos consecutivos si y solo si:
=> N = (K+1) 3 – K 3
=> N = 3*K 2 + 3*K + 1
=> 12*N = 36*K 2 + 36*K + 12
=> 12*N = ( 6*K + 3) 2 + 3
=> 12*N – 3 = (6*K + 3) 2
lo que significa que (12*N – 3) debe ser un cuadrado perfecto para descomponer N en la diferencia de dos cubos consecutivos.
Por lo tanto, si la condición anterior es cierta, imprimiremos los números usando un ciclo for para verificar para qué valor de i si (i+1) 3 – i 3 = N e imprimiremos el número i y i + 1 .
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to print the two consecutive // numbers whose difference is N void print(int N) { // Iterate in the range [0, 10^5] for (int i = 0; i < 100000; i++) { if (pow(i + 1, 3) - pow(i, 3) == N) { cout << i << ' ' << i + 1; return; } } } // Function to check if N is a // perfect cube bool isPerfectSquare(long double x) { // Find floating point value of // square root of x. long double sr = sqrt(x); // If square root is an integer return ((sr - floor(sr)) == 0); } // Function to check whether a number // can be represented as difference // of two consecutive cubes bool diffCube(int N) { // Check if 12 * N - 3 is a // perfect square or not return isPerfectSquare(12 * N - 3); } // Driver Code int main() { // Given Number N int N = 19; if (diffCube(N)) { cout << "Yes\n"; print(N); } else { cout << "No\n"; } return 0; }
Java
// Java program for the above approach import java.util.*; class GFG{ // Function to print the two consecutive // numbers whose difference is N static void print(int N) { // Iterate in the range [0, 10^5] for (int i = 0; i < 100000; i++) { if (Math.pow(i + 1, 3) - Math.pow(i, 3) == N) { int j = i + 1; System.out.println(i + " " + j); return; } } } // Function to check if N is a // perfect cube static boolean isPerfectSquare(double x) { // Find floating point value of // square root of x. double sr = Math.sqrt(x); // If square root is an integer return ((sr - Math.floor(sr)) == 0); } // Function to check whether a number // can be represented as difference // of two consecutive cubes static boolean diffCube(int N) { // Check if 12 * N - 3 is a // perfect square or not return isPerfectSquare(12 * N - 3); } // Driver Code public static void main(String[] args) { // Given Number N int N = 19; if (diffCube(N)) { System.out.println("Yes"); print(N); } else { System.out.println("No"); } } } // This code is contributed by rock_cool
Python3
# Python3 program for the above approach import math # Function to print the two consecutive # numbers whose difference is N def printt(N): # Iterate in the range [0, 10^5] for i in range(100000): if (pow(i + 1, 3) - pow(i, 3) == N): print(i, '', i + 1) return # Function to check if N is a # perfect cube def isPerfectSquare(x): # Find floating povalue of # square root of x. sr = math.sqrt(x) # If square root is an integer return ((sr - math.floor(sr)) == 0) # Function to check whether a number # can be represented as difference # of two consecutive cubes def diffCube(N): # Check if 12 * N - 3 is a # perfect square or not return isPerfectSquare(12 * N - 3) # Driver Code # Given number N N = 19 if (diffCube(N)): print("Yes") printt(N) else: print("No") # This code is contributed by sanjoy_62
C#
// C# program for the above approach using System; class GFG{ // Function to print the two consecutive // numbers whose difference is N static void print(int N) { // Iterate in the range [0, 10^5] for (int i = 0; i < 100000; i++) { if (Math.Pow(i + 1, 3) - Math.Pow(i, 3) == N) { int j = i + 1; Console.WriteLine(i + " " + j); return; } } } // Function to check if N is a // perfect cube static bool isPerfectSquare(double x) { // Find floating point value of // square root of x. double sr = Math.Sqrt(x); // If square root is an integer return ((sr - Math.Floor(sr)) == 0); } // Function to check whether a number // can be represented as difference // of two consecutive cubes static bool diffCube(int N) { // Check if 12 * N - 3 is a // perfect square or not return isPerfectSquare(12 * N - 3); } // Driver Code public static void Main(String[] args) { // Given Number N int N = 19; if (diffCube(N)) { Console.WriteLine("Yes"); print(N); } else { Console.WriteLine("No"); } } } // This code is contributed by Rajput-Ji
Javascript
<script> // Javascript program for the above approach // Function to print the two consecutive // numbers whose difference is N function print(N) { // Iterate in the range [0, 10^5] for(let i = 0; i < 100000; i++) { if (parseInt(Math.pow(i + 1, 3), 10) - parseInt(Math.pow(i, 3), 10) == N) { document.write(i + " " + (i + 1)); return; } } } // Function to check if N is a // perfect cube function isPerfectSquare(x) { // Find floating point value of // square root of x. let sr = Math.sqrt(x); // If square root is an integer return ((sr - Math.floor(sr)) == 0); } // Function to check whether a number // can be represented as difference // of two consecutive cubes function diffCube(N) { // Check if 12 * N - 3 is a // perfect square or not return isPerfectSquare(12 * N - 3); } // Driver code // Given Number N let N = 19; if (diffCube(N) != 0) { document.write("Yes" + "</br>"); print(N); } else { document.write("No"); } // This code is contributed by divyeshrabadiya07 </script>
Yes 2 3
Complejidad de tiempo: O(N), donde N está en el rango 10 5
Espacio auxiliar: O(1)