Encuentra otros dos lados de un triángulo rectángulo

Dado un lado del triángulo de ángulo recto, verifique si existe un triángulo de ángulo recto posible con otros dos lados del triángulo. Si es posible, imprima la longitud de los otros dos lados. 
Else print -1 
Nota: Todos los lados del triángulo deben ser números enteros positivos

Ejemplo 1: 

Input : a = 3
Output : b = 4, c = 5
Explanation : a = 3, b = 4 and c = 5 form right 
angle triangle because 
32 + 42 = 9 + 16 = 25 = 52 => 32 + 42 = 52

Ejemplo 2: 

Input : a = 11
Output : b = 60, c = 61
Explanation : a = 11, b = 60 and c = 61 form 
right angle triangle because
112 + 602 = 121 + 3600 = 3721 = 612 => 112 + 602 = 612

Para resolver este problema primero observamos la ecuación de Pitágoras. Si a y b son las longitudes de los catetos de un triángulo rectángulo y c es la longitud de la hipotenusa, entonces la suma de los cuadrados de las longitudes de los catetos es igual al cuadrado de la longitud de la hipotenusa. 

Esta relación está representada por la fórmula:  

a2 + b2 = c2

Right angle triangle

  • Caso 1: a es un número impar: dado a, encuentre b y c
c2 - b2 = a2
OR
c = (a2 + 1)/2;
b = (a2 - 1)/2;
  • La solución anterior funciona solo para el caso en que a es impar, porque 2 + 1 es divisible por 2 solo para impar a.
  • Caso 2: a es un número par: cuando cb es 2 y c+b es (a 2 )/2
c-b = 2 & c+b = (a2)/2
Hence,
c = (a2)/4 + 1;
b = (a2)/4 - 1;

Esto funciona cuando a es par. 

Esta solución no funciona para el caso de a = 1 y a = 2 porque no hay ningún triángulo rectángulo con lado 1 o 2 con todos los lados enteros.  

C++

// C++ program to print other two sides of right
// angle triangle given one side
#include <bits/stdc++.h>
using namespace std;
 
// Finds two sides of a right angle triangle
// if it exist.
void printOtherSides(int n)
{
    // if n is odd
    if (n & 1)
    {
        // case of n = 1 handled separately
        if (n == 1)
            cout << -1 << endl;
        else
        {
            int b = (n*n-1)/2;
            int c = (n*n+1)/2;
            cout << "b = " << b
                 << ", c = " << c << endl;
        }
    }
    else
    {
        // case of n = 2 handled separately
        if (n == 2)
            cout << -1 << endl;
        else
        {
            int b = n*n/4-1;
            int c = n*n/4+1;
            cout << "b = " << b
                 << ", c = " << c << endl;
        }
    }
}
 
// Driver program to test above function
int main()
{
    int a = 3;
    printOtherSides(a);
    return 0;
}

Java

// Java program to print other two
// sides of right angle triangle
// given one side
 
class GFG
{
    // Finds two sides of a right angle
    // triangle if it they exist.
    static void printOtherSides(int n)
    {
        // if n is odd
        if (n % 2 != 0)
        {
            // case of n = 1 handled separately
            if (n == 1)
                System.out.println("-1");
            else
            {
                int b = (n * n -1) / 2;
                int c = (n *n  +1) / 2;
                System.out.println("b = "+ b +
                                   ", c = "+c);
            }
        }
        else
        {
            // case of n = 2 handled separately
            if (n == 2)
                System.out.println("-1");
            else
            {
                int b = n * n / 4 - 1;
                int c = n * n / 4 + 1;
                System.out.println("b = "+ b +
                                   ", c = "+c);
            }
        }
    }
         
    // Driver code
    public static void main (String[] args)
    {
        int a = 3;
        printOtherSides(a);
    }
}
 
// This code is contributed by Anant Agarwal.

Python3

# Python program to print other
# two sides of right angle
# triangle given one side
 
# Finds two sides of a right angle
# triangle if it they exist.
def printOtherSides(n):
     
    # if n is odd
    if(n & 1):
         
        # case of n = 1 handled
        # separately
        if(n == 1):
            print(-1)
        else:
            b = (n * n - 1) // 2
            c = (n * n + 1) // 2
            print("b =", b, ", c =", c)
    else:
         
        # case of n = 2 handled
        # separately
        if(n == 2):
            print(-1)
        else:
            b = n * n // 4 - 1
            c = n * n // 4 + 1
            print("b =", b", c =", c)
 
# Driver Code
a = 3
printOtherSides(a)
 
# This code is contributed by
# Sanjit_Prasad

C#

// C# program to print other two
// sides of right angle triangle
// given one side
using System;
 
class GFG {
     
    // Finds two sides of a right angle
    // triangle if it they exist.
    static void printOtherSides(int n)
    {
         
        // if n is odd
        if (n % 2 != 0)
        {
             
            // case of n = 1 handled
            // separately
            if (n == 1)
            Console.WriteLine("-1");
            else
            {
                int b = (n * n - 1) / 2;
                int c = (n * n + 1) / 2;
                Console.Write("b = "+ b +
                              ", c = "+ c);
            }
        }
        else
        {
             
            // case of n = 2 handled
            // separately
            if (n == 2)
            Console.Write("-1");
            else
            {
                int b = n * n / 4 - 1;
                int c = n * n / 4 + 1;
                Console.Write("b = "+ b +
                              ", c = "+ c);
            }
        }
    }
         
    // Driver code
    public static void Main ()
    {
        int a = 3;
        printOtherSides(a);
    }
}
 
// This code is contributed by Nitin Mittal.

PHP

<?php
// PHP program to print other two
// sides of right angle triangle
// given one side
 
// Finds two sides of a right angle
// triangle if it they exist.
function printOtherSides($n)
{
     
    // if n is odd
    if ($n & 1)
    {
        // case of n = 1
        // handled separately
        if ($n == 1)
            echo -1 ;
        else
        {
            $b = ($n * $n - 1) / 2;
            $c = ($n * $n + 1) / 2;
            echo "b = " ,$b,", c = " ,$c ;
        }
    }
    else
    {
         
        // case of n = 2
        // handled separately
        if ($n == 2)
            echo -1 ;
        else
        {
            $b = $n * $n / 4 - 1;
            $c = $n * $n / 4 + 1;
            echo "b = " ,$b, ", c = ", $c ;
        }
    }
}
 
    // Driver Code
    $a = 3;
    printOtherSides($a);
    return 0;
 
// This code is contributed by nitin mittal.
?>

Javascript

<script>
 
// javascript program to print other two
// sides of right angle triangle
// given one side 
 
// Finds two sides of a right angle
// triangle if it they exist.
function printOtherSides(n)
{
    // if n is odd
    if (n % 2 != 0)
    {
        // case of n = 1 handled separately
        if (n == 1)
            document.write("-1");
        else
        {
            var b = (n * n -1) / 2;
            var c = (n *n  +1) / 2;
            document.write("b = "+ b +
                               ", c = "+c);
        }
    }
    else
    {
        // case of n = 2 handled separately
        if (n == 2)
            document.write("-1");
        else
        {
            var b = n * n / 4 - 1;
            var c = n * n / 4 + 1;
            document.write("b = "+ b +
                               ", c = "+c);
        }
    }
}
     
// Driver code
var a = 3;
printOtherSides(a);
 
 
// This code is contributed by 29AjayKumar
 
</script>

Producción: 

b = 4, c = 5
This article is contributed by Pratik Chhajer . If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Complejidad temporal: O(1)
Espacio auxiliar: O(1)

Publicación traducida automáticamente

Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *