Triplete pitagórico con suma dada usando bucle único

C++

// C++ program to find the Pythagorean
// Triplet with given sum
#include <bits/stdc++.h>
using namespace std;
 
// Function to calculate the
// Pythagorean triplet in O(n)
void PythagoreanTriplet(int n)
{
    int flag = 0;
 
    // Iterate a from 1 to N-1.
    for (int a = 1; a < n; a++)
    {
        // Calculate value of b
        int b = (n * n - 2 * n * a)
                / (2 * n - 2 * a);
 
        // The value of c = n - a - b
        int c = n - a - b;
 
        if (a * a + b * b == c * c
            && b > 0 && c > 0)
        {
            cout << a << " " << b << " " << c;
            flag = 1;
            break;
        }
    }
 
    if (flag == 0) {
        cout << "-1";
    }
 
    return;
}
 
// Driver Code
int main()
{
    int N = 12;
 
    // Function call
    PythagoreanTriplet(N);
 
    return 0;
}

Java

// Java program to find the Pythagorean
// Triplet with given sum
 
class GFG {
 
    // Function to calculate the
    // Pythagorean triplet in O(n)
    static void PythagoreanTriplet(int n)
    {
        int flag = 0;
 
        // Iterate a from 1 to N-1.
        for (int a = 1; a < n; a++)
        {
            // Calculate value of b
            int b = (n * n - 2 * n * a)
              / (2 * n - 2 * a);
 
            // The value of c = n - a - b
            int c = n - a - b;
 
            if (a * a + b * b == c * c
                && b > 0 && c > 0)
            {
                System.out
                  .print(a + " " + b + " " + c);
                flag = 1;
                break;
            }
        }
 
        if (flag == 0)
        {
            System.out.print("-1");
        }
 
        return;
    }
   
    // Driver Code
    public static void main(String[] args)
    {
        int N = 12;
 
        // Function call
        PythagoreanTriplet(N);
    }
}
 
// This code contributed by sapnasingh4991

Python3

# Python3 program to find the Pythagorean
# Triplet with a given sum
 
# Function to calculate the
# Pythagorean triplet in O(n)
 
 
def PythagoreanTriplet(n):
    flag = 0
 
    # Iterate a from 1 to N-1.
    for a in range(1, n, 1):
 
        # Calculate value of b
        b = (n * n - 2 * n * a) // (2 * n - 2 * a)
 
        # The value of c = n - a - b
        c = n - a - b
 
        if (a * a + b * b == c * c
            and b > 0 and c > 0):
            print(a, b, c)
            flag = 1
            break
 
    if(flag == 0):
        print("-1")
 
    return
 
 
# Driver code
if __name__ == '__main__':
    N = 12
 
    # Function call
    PythagoreanTriplet(N)
 
# This code is contributed by Bhupendra_Singh

C#

// C# program to find the Pythagorean
// Triplet with given sum
using System;
 
class GFG {
 
    // Function to calculate the
    // Pythagorean triplet in O(n)
    static void PythagoreanTriplet(int n)
    {
        int flag = 0;
 
        // Iterate a from 1 to N-1.
        for (int a = 1; a < n; a++)
        {
            // Calculate value of b
            int b = (n * n - 2 * n * a)
              / (2 * n - 2 * a);
 
            // The value of c = n - a - b
            int c = n - a - b;
            if (a * a + b * b == c * c
                && b > 0 && c > 0)
            {
                Console.Write(a + " " + b + " " + c);
                flag = 1;
                break;
            }
        }
 
        if (flag == 0) {
            Console.Write("-1");
        }
        return;
    }
 
    // Driver code
    public static void Main(String[] args)
    {
        int N = 12;
 
        // Function call
        PythagoreanTriplet(N);
    }
}
 
// This code is contributed by shivanisinghss2110

Javascript

<script>
    // Javascript program to find the Pythagorean
      // Triplet with given sum
     
    // Function to calculate the
    // Pythagorean triplet in O(n)
    function PythagoreanTriplet(n)
    {
        let flag = 0;
 
        // Iterate a from 1 to N-1.
        for (let a = 1; a < n; a++)
        {
            // Calculate value of b
            let b = (n * n - 2 * n * a)
                    / (2 * n - 2 * a);
 
            // The value of c = n - a - b
            let c = n - a - b;
 
            if (a * a + b * b == c * c
                && b > 0 && c > 0)
            {
                document.write(a + " " + b + " " + c);
                flag = 1;
                break;
            }
        }
 
        if (flag == 0) {
            document.write("-1");
        }
 
        return;
    }
     
    let N = 12;
  
    // Function call
    PythagoreanTriplet(N);
 
// This code is contributed by divyeshrabadiya
</script>

Un triplete pitagórico es un conjunto de números naturales tales que a < b < c, para los cuales 

a^2 + b^2 = c^2
 

Dado un número N, encuentre un triplete pitagórico con suma como dado N o devuelva -1.

Ejemplos:  

Input: 12  
Output: 3 4 5
Explanation:
As 32 + 42 = 52

Input: 82
Output: -1

Enfoque: La idea es encontrar el valor de b y c en términos de a e iterar a de 1 a N. Para encontrar el valor de b y c en términos de a tenemos que hacer lo siguiente:  

Tenemos dos ecuaciones,  

 a^2 + b^2 = c^2        [Tex] a + b + c = N        [/Tex]
 

Hallaremos el valor de c en términos de a y b. Luego colocamos este valor en la ecuación 1 para resolver b. 

De la ecuación 2, 

 c = N - b - a
 

Ahora, pon este valor en la ecuación 1.

 a^2 + b^2 = (N - b - a)^2
 

Después de resolver la ecuación anterior obtendremos, 

 b = (N * N - 2 * N * a) / (2 * N - 2 * a)        [Tex]c = N – b – a        [/Tex]
 

Ahora, itere a de 1 a N y calcule respectivamente el valor de b y c. Luego, verifique si

a^2 + b^2 = c^2
 

C++

// C++ program to find the Pythagorean
// Triplet with given sum
#include <bits/stdc++.h>
using namespace std;
 
// Function to calculate the
// Pythagorean triplet in O(n)
void PythagoreanTriplet(int n)
{
    int flag = 0;
 
    // Iterate a from 1 to N-1.
    for (int a = 1; a < n; a++)
    {
        // Calculate value of b
        int b = (n * n - 2 * n * a)
                / (2 * n - 2 * a);
 
        // The value of c = n - a - b
        int c = n - a - b;
 
        if (a * a + b * b == c * c
            && b > 0 && c > 0)
        {
            cout << a << " " << b << " " << c;
            flag = 1;
            break;
        }
    }
 
    if (flag == 0) {
        cout << "-1";
    }
 
    return;
}
 
// Driver Code
int main()
{
    int N = 12;
 
    // Function call
    PythagoreanTriplet(N);
 
    return 0;
}

Java

// Java program to find the Pythagorean
// Triplet with given sum
 
class GFG {
 
    // Function to calculate the
    // Pythagorean triplet in O(n)
    static void PythagoreanTriplet(int n)
    {
        int flag = 0;
 
        // Iterate a from 1 to N-1.
        for (int a = 1; a < n; a++)
        {
            // Calculate value of b
            int b = (n * n - 2 * n * a)
              / (2 * n - 2 * a);
 
            // The value of c = n - a - b
            int c = n - a - b;
 
            if (a * a + b * b == c * c
                && b > 0 && c > 0)
            {
                System.out
                  .print(a + " " + b + " " + c);
                flag = 1;
                break;
            }
        }
 
        if (flag == 0)
        {
            System.out.print("-1");
        }
 
        return;
    }
   
    // Driver Code
    public static void main(String[] args)
    {
        int N = 12;
 
        // Function call
        PythagoreanTriplet(N);
    }
}
 
// This code contributed by sapnasingh4991

Python3

# Python3 program to find the Pythagorean
# Triplet with a given sum
 
# Function to calculate the
# Pythagorean triplet in O(n)
 
 
def PythagoreanTriplet(n):
    flag = 0
 
    # Iterate a from 1 to N-1.
    for a in range(1, n, 1):
 
        # Calculate value of b
        b = (n * n - 2 * n * a) // (2 * n - 2 * a)
 
        # The value of c = n - a - b
        c = n - a - b
 
        if (a * a + b * b == c * c
            and b > 0 and c > 0):
            print(a, b, c)
            flag = 1
            break
 
    if(flag == 0):
        print("-1")
 
    return
 
 
# Driver code
if __name__ == '__main__':
    N = 12
 
    # Function call
    PythagoreanTriplet(N)
 
# This code is contributed by Bhupendra_Singh

C#

// C# program to find the Pythagorean
// Triplet with given sum
using System;
 
class GFG {
 
    // Function to calculate the
    // Pythagorean triplet in O(n)
    static void PythagoreanTriplet(int n)
    {
        int flag = 0;
 
        // Iterate a from 1 to N-1.
        for (int a = 1; a < n; a++)
        {
            // Calculate value of b
            int b = (n * n - 2 * n * a)
              / (2 * n - 2 * a);
 
            // The value of c = n - a - b
            int c = n - a - b;
            if (a * a + b * b == c * c
                && b > 0 && c > 0)
            {
                Console.Write(a + " " + b + " " + c);
                flag = 1;
                break;
            }
        }
 
        if (flag == 0) {
            Console.Write("-1");
        }
        return;
    }
 
    // Driver code
    public static void Main(String[] args)
    {
        int N = 12;
 
        // Function call
        PythagoreanTriplet(N);
    }
}
 
// This code is contributed by shivanisinghss2110

Javascript

<script>
    // Javascript program to find the Pythagorean
      // Triplet with given sum
     
    // Function to calculate the
    // Pythagorean triplet in O(n)
    function PythagoreanTriplet(n)
    {
        let flag = 0;
 
        // Iterate a from 1 to N-1.
        for (let a = 1; a < n; a++)
        {
            // Calculate value of b
            let b = (n * n - 2 * n * a)
                    / (2 * n - 2 * a);
 
            // The value of c = n - a - b
            let c = n - a - b;
 
            if (a * a + b * b == c * c
                && b > 0 && c > 0)
            {
                document.write(a + " " + b + " " + c);
                flag = 1;
                break;
            }
        }
 
        if (flag == 0) {
            document.write("-1");
        }
 
        return;
    }
     
    let N = 12;
  
    // Function call
    PythagoreanTriplet(N);
 
// This code is contributed by divyeshrabadiya
</script>
Producción

3 4 5

Complejidad de tiempo: O(N)

Espacio Auxiliar: O(1)
 

Publicación traducida automáticamente

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