Dados dos enteros positivos A y B que representan los lados de un triángulo , la tarea es verificar si los dos lados dados del triángulo son lados de un triángulo rectángulo válido o no. Si es cierto, escriba “ SÍ ”. De lo contrario, escriba “No” .
Ejemplos:
Entrada: A = 3, B = 4
Salida: Sí
Explicación: Es posible un triángulo rectángulo con longitudes de lado 3, 4 y 5.Entrada: A = 2, B = 5
Salida: No
Enfoque: siga los pasos a continuación para resolver el problema:
- Inicialice una variable, diga checkTriangle para verificar si los dos lados dados del triángulo no son lados del triángulo rectángulo.
- Comprueba si el valor de B 2 + A 2 es un cuadrado perfecto o no . Si se encuentra que es cierto, actualice checkTriangle = True. .
- De lo contrario, comprueba si el valor de B 2 – A 2 es un cuadrado perfecto o no . Si se encuentra que es cierto, actualice checkTriangle = True.
- De lo contrario, comprueba si el valor de A 2 – B 2 es un número cuadrado perfecto o no . Si se encuentra que es cierto, actualice checkTriangle = True.
- Finalmente, imprima «Sí» si checkTriangle es Verdadero . De lo contrario, escriba “No” .
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program to implement // the above approach #include <bits/stdc++.h> using namespace std; // Function to check if N is a // perfect square number or not int checkPerfectSquare(int N) { // If N is a non // positive integer if (N <= 0) { return 0; } // Stores square root // of N double sq = sqrt(N); // Check for perfect square if (floor(sq) == ceil(sq)) { return 1; } // If N is not a // perfect square number return 0; } // Function to check if given two sides of a // triangle forms a right-angled triangle bool checktwoSidesareRighTriangle(int A, int B) { bool checkTriangle = false; // If the value of (A * A + B * B) is a // perfect square number if (checkPerfectSquare(A * A + B * B)) { // Update checkTriangle checkTriangle = true; } // If the value of (A * A - B * B) is a // perfect square number if (checkPerfectSquare(A * A - B * B)) { // Update checkTriangle checkTriangle = true; } // If the value of (B * B - A * A) is a // perfect square number if (checkPerfectSquare(B * B - A * A)) { // Update checkTriangle checkTriangle = true; } return checkTriangle; } // Driver Code int main() { int A = 3, B = 4; // If the given two sides of a triangle // forms a right-angled triangle if (checktwoSidesareRighTriangle(A, B)) { cout << "Yes"; } // Otherwise else { cout << "No"; } return 0; }
Java
// Java program to implement // the above approach import java.io.*; import java.util.*; class GFG{ // Function to check if N is a // perfect square number or not static int checkPerfectSquare(int N) { // If N is a non // positive integer if (N <= 0) { return 0; } // Stores square root // of N double sq = Math.sqrt(N); // Check for perfect square if (Math.floor(sq) == Math.ceil(sq)) { return 1; } // If N is not a // perfect square number return 0; } // Function to check if given two sides of a // triangle forms a right-angled triangle static boolean checktwoSidesareRighTriangle(int A, int B) { boolean checkTriangle = false; // If the value of (A * A + B * B) is a // perfect square number if (checkPerfectSquare(A * A + B * B) != 0) { // Update checkTriangle checkTriangle = true; } // If the value of (A * A - B * B) is a // perfect square number if (checkPerfectSquare(A * A - B * B) != 0) { // Update checkTriangle checkTriangle = true; } // If the value of (B * B - A * A) is a // perfect square number if (checkPerfectSquare(B * B - A * A) != 0) { // Update checkTriangle checkTriangle = true; } return checkTriangle; } // Driver Code public static void main(String[] args) { int A = 3, B = 4; // If the given two sides of a triangle // forms a right-angled triangle if (checktwoSidesareRighTriangle(A, B)) { System.out.print("Yes"); } // Otherwise else { System.out.print("No"); } } } // This code is contributed by susmitakundugoaldanga
Python3
# Python3 program to implement # the above approach from math import sqrt, floor, ceil # Function to check if N is a # perfect square number or not def checkPerfectSquare(N): # If N is a non # positive integer if (N <= 0): return 0 # Stores square root # of N sq = sqrt(N) # Check for perfect square if (floor(sq) == ceil(sq)): return 1 # If N is not a # perfect square number return 0 # Function to check if given two sides of a # triangle forms a right-angled triangle def checktwoSidesareRighTriangle(A, B): checkTriangle = False # If the value of (A * A + B * B) is a # perfect square number if (checkPerfectSquare(A * A + B * B)): # Update checkTriangle checkTriangle = True # If the value of (A * A - B * B) is a # perfect square number if (checkPerfectSquare(A * A - B * B)): # Update checkTriangle checkTriangle = True # If the value of (B * B - A * A) is a # perfect square number if (checkPerfectSquare(B * B - A * A)): # Update checkTriangle checkTriangle = True return checkTriangle # Driver Code if __name__ == '__main__': A = 3 B = 4 # If the given two sides of a triangle # forms a right-angled triangle if (checktwoSidesareRighTriangle(A, B)): print("Yes") # Otherwise else: print("No") # This code is contributed by SURENDRA_GANGWAR
C#
// C# program to implement // the above approach using System; class GFG{ // Function to check if N is a // perfect square number or not static int checkPerfectSquare(int N) { // If N is a non // positive integer if (N <= 0) { return 0; } // Stores square root // of N double sq = Math.Sqrt(N); // Check for perfect square if (Math.Floor(sq) == Math.Ceiling(sq)) { return 1; } // If N is not a // perfect square number return 0; } // Function to check if given two sides of a // triangle forms a right-angled triangle static bool checktwoSidesareRighTriangle(int A, int B) { bool checkTriangle = false; // If the value of (A * A + B * B) is a // perfect square number if (checkPerfectSquare(A * A + B * B) != 0) { // Update checkTriangle checkTriangle = true; } // If the value of (A * A - B * B) is a // perfect square number if (checkPerfectSquare(A * A - B * B) != 0) { // Update checkTriangle checkTriangle = true; } // If the value of (B * B - A * A) is a // perfect square number if (checkPerfectSquare(B * B - A * A) != 0) { // Update checkTriangle checkTriangle = true; } return checkTriangle; } // Driver Code public static void Main() { int A = 3, B = 4; // If the given two sides of a triangle // forms a right-angled triangle if (checktwoSidesareRighTriangle(A, B)) { Console.Write("Yes"); } // Otherwise else { Console.Write("No"); } } } // This code is contributed by code_hunt
Javascript
<script> // Javascript program to implement // the above approach // Function to check if N is a // perfect square number or not function checkPerfectSquare( N) { // If N is a non // positive integer if (N <= 0) { return 0; } // Stores square root // of N let sq = Math.sqrt(N); // Check for perfect square if (Math.floor(sq) == Math.ceil(sq)) { return 1; } // If N is not a // perfect square number return 0; } // Function to check if given two sides of a // triangle forms a right-angled triangle function checktwoSidesareRighTriangle( A , B) { let checkTriangle = false; // If the value of (A * A + B * B) is a // perfect square number if (checkPerfectSquare(A * A + B * B) != 0) { // Update checkTriangle checkTriangle = true; } // If the value of (A * A - B * B) is a // perfect square number if (checkPerfectSquare(A * A - B * B) != 0) { // Update checkTriangle checkTriangle = true; } // If the value of (B * B - A * A) is a // perfect square number if (checkPerfectSquare(B * B - A * A) != 0) { // Update checkTriangle checkTriangle = true; } return checkTriangle; } // Driver Code let A = 3, B = 4; // If the given two sides of a triangle // forms a right-angled triangle if (checktwoSidesareRighTriangle(A, B)) { document.write("Yes"); } // Otherwise else { document.write("No"); } // This code is contributed by Rajput-Ji </script>
Yes
Complejidad de tiempo: O(log(max(A, B))
Espacio auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por ManikantaBandla y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA