Possible pairs forming a Pythagorean Triple with a given value

Given an integer C, the task is to find all possible pairs (A, B) in range [1, C) such that:

  1. A2 + B2 = C2
  2. A < B

Examples:

Input: C = 5
Output:(3, 4)
Explanation:
(3)2 + (4)2 = 9 + 16 = 25 = 52

Input: C = 25
Output:(15, 20), (7, 24)
Explanation: Both the pairs satisfy the necessary conditions.

Approach:



  1. Check all possible values of A and B in the range [1, C).
  2. Store all pair that satisfies the given conditions.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to compute
// all the possible
// pairs that forms a
// pythagorean triple
// with a given value
#include <bits/stdc++.h>
using namespace std;
  
// Function to generate all
// possible pairs
vector<pair<int, int> > Pairs(int C)
{
    // Vector to store all the
    // possible pairs
    vector<pair<int, int> > ans;
  
    // Checking all the possible
    // pair in the range of [1, c)
    for (int i = 1; i < C; i++) {
        for (int j = i + 1; j < C;
             j++) {
  
            // If the pair satisfies
            // the condition push it
            // in the vector
            if ((i * i) + (j * j) == (C * C)) {
                ans.push_back(make_pair(i, j));
            }
        }
    }
    return ans;
}
  
// Driver Program
int main()
{
    int C = 13;
    vector<pair<int, int> > ans
        = Pairs(C);
  
    // If no valid pair exist
    if (ans.size() == 0) {
        cout << "No valid pair exist"
             << endl;
        return 0;
    }
  
    // Print all valid pairs
    for (auto i = ans.begin();
         i != ans.end(); i++) {
        cout << "(" << i->first << ", "
             << i->second << ")" << endl;
    }
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to compute all 
// the possible pairs that forms
// a pythagorean triple with a 
// given value
import java.util.*;
  
class GFG{
static class pair
    int first, second; 
      
    public pair(int first, int second) 
    
        this.first = first; 
        this.second = second; 
    
  
// Function to generate all
// possible pairs
static Vector<pair> Pairs(int C)
{
      
    // Vector to store all the
    // possible pairs
    Vector<pair> ans = new Vector<pair>();
  
    // Checking all the possible
    // pair in the range of [1, c)
    for(int i = 1; i < C; i++) 
    {
       for(int j = i + 1; j < C; j++)
       {
             
          // If the pair satisfies
          // the condition push it
          // in the vector
          if ((i * i) + (j * j) == (C * C)) 
          {
              ans.add(new pair(i, j));
          }
       }
    }
    return ans;
}
  
// Driver code
public static void main(String[] args)
{
    int C = 13;
    Vector<pair> ans = Pairs(C);
  
    // If no valid pair exist
    if (ans.size() == 0
    {
        System.out.print("No valid pair " +
                         "exist" + "\n");
        return;
    }
  
    // Print all valid pairs
    for(pair i:ans)
    {
       System.out.print("(" + i.first + 
                       ", " + i.second + 
                        ")" + "\n");
    }
}
}
  
// This code is contributed by gauravrajput1

chevron_right


Output:

(5, 12)

Time Complexity: O(C2)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : GauravRajput1