Genere un triplete de Pitágoras a partir de un solo entero

Dado un solo entero n  \in [1, 1000000000], genere un triplete de Pitágoras que incluya n como uno de sus lados si es posible.
Ejemplos: 
 

Input : 22
Output : Pythagoras Triplets exist i.e. 22 120 122

Input : 4
Output : Pythagoras Triplets exist i.e.  4 3 5

Input : 2
Output : No Pythagoras Triplet exists 

Explicación: 
Definición: Los “tripletes de Pitágoras” son soluciones enteras del Teorema de Pitágoras, es decir, satisfacen la ecuación  a^2 + b^2 = c^2
. Nuestra tarea es generar un triplete a partir de un valor integral. Esta puede ser una tarea confusa porque el lado que se nos da puede ser una hipotenusa o un lado sin hipotenusa.
Comenzando a calcular los tripletes poniéndolos en una fórmula, se puede deducir que solo para 1 y 2, no es posible ningún triplete.
Además, 
si n es par, nuestros tripletes se calculan mediante fórmula  (n^2/4 - 1)^2 + n^2 = (n^2/4 + 1)^2
si n es impar, nuestros tripletes se calculan mediante fórmula  (n^2-1)^2/2 + n^2 = (n^2+1)^2/2
Prueba:
el teorema de Pitágoras también se puede escribir como, por  c^2 - b^2 = a^2
ejemplo, a*a = (cb)(c+b)
a*ax 1 = a*a, por lo tanto  c = (a^2+1)/2 b = (a^2 - 1)/2 , esta solución funciona si n es impar.
Para una solución uniforme, c+b = n^2/2, c-b=2 , por lo tanto, obtenemos la fórmula anterior cuando n es par.
Código
 

C++

// CPP program to find Pythagoras triplet
// with one side as given number.
#include <bits/stdc++.h>
using namespace std;
 
// Function, to evaluate the Pythagoras triplet
// with includes 'n' if possible
void evaluate(long long int n)
{
 
    if (n == 1 || n == 2)
        printf("No Pythagoras Triplet exists");
 
    else if (n % 2 == 0) {
 
        // Calculating for even case
        long long int var = 1LL * n * n / 4;
        printf("Pythagoras Triplets exist i.e. ");
        printf("%lld %lld %lld", n, var - 1, var + 1);
    }
 
    else if (n % 2 != 0) {
 
        // Calculating for odd case
        long long int var = 1LL * n * n + 1;
        printf("Pythagoras Triplets exist i.e. ");
        printf("%lld %lld %lld", n, var / 2 - 1, var / 2);
    }
}
 
// Driver function
int main()
{
    long long int n = 22;
    evaluate(n);
    return 0;
}

Java

// Java program to find
// Pythagoras triplet
// with one side as
// given number.
import java.io.*;
 
class GFG
{
     
// Function, to evaluate
// the Pythagoras triplet
// with includes 'n' if
// possible
static void evaluate( int n)
{
    if (n == 1 || n == 2)
        System.out.println("No Pythagoras " +
                           "Triplet exists");
 
    else if (n % 2 == 0)
    {
 
        // Calculating for even case
        int var = 1 * n * n / 4;
        System.out.print("Pythagoras Triplets " +
                                  "exist i.e. ");
        System.out.print(n + " ");
        System.out.print(var - 1+ " ");
        System.out.println(var + 1 +" ");
    }
 
    else if (n % 2 != 0)
    {
 
        int var = 1 * n * n + 1;
        System.out.print("Pythagoras Triplets " +
                                  "exist i.e. ");
        System.out.print(n + " ");
        System.out.print(var / 2 - 1 + " ");
        System.out.println(var / 2 + " ");
    }
}
 
// Driver Code
public static void main(String[] args)
{
    int n = 22;
    evaluate(n);
}
}
 
// This code is contributed
// by ajit

Python3

# Python3 program to find
# Pythagoras triplet with
# one side as given number.
 
# Function, to evaluate the
# Pythagoras triplet with
# includes 'n' if possible
def evaluate(n):
    if (n == 1 or n == 2):
        print("No Pythagoras" +
            " Triplet exists");
    elif (n % 2 == 0):
         
        # Calculating for
        # even case
        var = n * n / 4;
        print("Pythagoras Triplets" +
             " exist i.e. ", end = "");
        print(int(n), " ", int(var - 1),
                      " ", int(var + 1));
    elif (n % 2 != 0):
         
        # Calculating for odd case
        var = n * n + 1;
        print("Pythagoras Triplets " +
             "exist i.e. ", end = "");
        print(int(n), " ", int(var / 2 - 1),
                         " ", int(var / 2));
 
# Driver Code
n = 22;
evaluate(n);
 
# This code is contributed by mits

C#

// C# program to find
// Pythagoras triplet
// with one side as
// given number.
using System;
 
class GFG
{
     
// Function, to evaluate
// the Pythagoras triplet
// with includes 'n' if
// possible
static void evaluate(int n)
{
    if (n == 1 || n == 2)
        Console.WriteLine("No Pythagoras " +
                          "Triplet exists");
     
    else if (n % 2 == 0)
    {
 
        // Calculating for even case
        int var = 1 * n * n / 4;
        Console.Write("Pythagoras Triplets " +
                               "exist i.e. ");
        Console.Write(n + " ");
        Console.Write(var - 1+ " ");
        Console.WriteLine(var + 1 +" ");
    }
 
    else if (n % 2 != 0)
    {
        int var = 1 * n * n + 1;
        Console.Write("Pythagoras Triplets " +
                               "exist i.e. ");
        Console.Write(n + " ");
        Console.Write(var / 2 - 1 + " ");
        Console.WriteLine(var / 2 + " ");
    }
}
 
// Driver Code
static public void Main ()
{
    int n = 22;
    evaluate(n);
}
}
 
// This code is contributed
// by ajit

PHP

<?php
// PHP program to find Pythagoras triplet
// with one side as given number.
 
// Function, to evaluate the
// Pythagoras triplet with
// includes 'n' if possible
function evaluate($n)
{
 
    if ($n == 1 || $n == 2)
        echo "No Pythagoras Triplet exists";
 
    else if ($n % 2 == 0) {
 
        // Calculating for even case
        $var = $n * $n / 4;
        echo "Pythagoras Triplets exist i.e. ";
        echo $n, " ", $var - 1, " ", $var + 1;
    }
 
    else if ($n % 2 != 0) {
 
        // Calculating for odd case
        $var = $n * $n + 1;
        echo "Pythagoras Triplets exist i.e. ";
        echo $n, " ", $var / 2 - 1, " ", $var / 2;
    }
}
 
    // Driver Code
    $n = 22;
    evaluate($n);
 
// This code is contributed by ajit
?>

Javascript

<script>
    // Javascript program to find
    // Pythagoras triplet
    // with one side as
    // given number.
     
    // Function, to evaluate
    // the Pythagoras triplet
    // with includes 'n' if
    // possible
    function evaluate(n)
    {
        if (n == 1 || n == 2)
            document.write("No Pythagoras Triplet exists");
 
        else if (n % 2 == 0)
        {
 
            // Calculating for even case
            let Var = 1 * n * n / 4;
            document.write("Pythagoras Triplets " +
                                   "exist i.e. ");
            document.write(n + " ");
            document.write(Var - 1+ " ");
            document.write(Var + 1 +" ");
        }
 
        else if (n % 2 != 0)
        {
            let Var = 1 * n * n + 1;
            document.write("Pythagoras Triplets " +
                                   "exist i.e. ");
            document.write(n + " ");
            document.write(parseInt(Var / 2, 10) - 1 + " ");
            document.write(parseInt(Var / 2, 10) + " ");
        }
    }
     
    let n = 22;
    evaluate(n);
         
</script>

Producción: 
 

Pythagoras Triplets exist i.e. 22 120 122

Publicación traducida automáticamente

Artículo escrito por parthendo 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 *