Dado un número N , la tarea es verificar si este número se puede representar como la diferencia de dos cuadrados perfectos o no.
Ejemplos:
Entrada: N = 3
Salida: Sí
Explicación:
2 2 – 1 1 = 3Entrada: N = 10
Salida: No
Enfoque: La idea es que todos los números se pueden representar como la diferencia de dos cuadrados, excepto los números que dan el resto de 2 cuando se dividen por 4.
Visualicemos esto tomando algunos ejemplos:
N = 4 => 22 - 02 N = 6 => Can't be expressed as 6 % 4 = 2 N = 8 => 32 - 12 N = 10 => Can't be expressed as 10 % 4 = 2 N = 11 => 62 - 52 N = 12 => 42 - 22 and so on...
Por lo tanto, la idea es simplemente comprobar el resto de 2 cuando el número dado se divide por 4.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program to check whether a number // can be represented by the difference // of two squares #include <bits/stdc++.h> using namespace std; // Function to check whether a number // can be represented by the difference // of two squares bool difSquare(int n) { // Checking if n % 4 = 2 or not if (n % 4 != 2) { return true; } return false; } // Driver code int main() { int n = 45; if (difSquare(n)) { cout << "Yes\n"; } else { cout << "No\n"; } return 0; }
Java
// Java program to check whether a number // can be represented by the difference // of two squares import java.util.*; class GFG{ // Function to check whether a number // can be represented by the difference // of two squares static boolean difSquare(int n) { // Checking if n % 4 = 2 or not if (n % 4 != 2) { return true; } return false; } // Driver code public static void main(String[] args) { int n = 45; if (difSquare(n)) { System.out.print("Yes\n"); } else { System.out.print("No\n"); } } } // This code is contributed by shivanisinghss2110
Python3
# Python3 program to check whether a number # can be represented by the difference # of two squares # Function to check whether a number # can be represented by the difference # of two squares def difSquare(n): # Checking if n % 4 = 2 or not if (n % 4 != 2): return True return False # Driver code if __name__ == '__main__': n = 45 if (difSquare(n)): print("Yes") else: print("No") # This code is contributed by mohit kumar 29
C#
// C# program to check whether a number // can be represented by the difference // of two squares using System; class GFG{ // Function to check whether a number // can be represented by the difference // of two squares static bool difSquare(int n) { // Checking if n % 4 = 2 or not if (n % 4 != 2) { return true; } return false; } // Driver code public static void Main() { int n = 45; if (difSquare(n)) { Console.Write("Yes\n"); } else { Console.Write("No\n"); } } } // This code is contributed by Nidhi_biet
Javascript
<script> // Javascript program to check whether a number // can be represented by the difference // of two squares // Function to check whether a number // can be represented by the difference // of two squares function difSquare(n) { // Checking if n % 4 = 2 or not if (n % 4 != 2) { return true; } return false; } // Driver code var n = 45; if (difSquare(n)) { document.write("Yes\n"); } else { document.write("No\n"); } // This code is contributed by Rajput-Ji </script>
Yes
Publicación traducida automáticamente
Artículo escrito por ranjeetkumar_chill y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA