In: Computer Science
use c++
A right-angle triangle with integer sides a, b, c, such as 3, 4,
5, is called a Pythagorean triple. The condition is that hypotenuse
to power of 2 is equal to adding of square of the 2 other sides. In
this example 25 = 16 + 9. Use brute force approach to find all the
triples such that no side is bigger than 50.
For this you should first implement the following Triangle class
and its methods. Then use it in main() to find all pythagorean
triples and put then in a std::vector. Finally print them out as
triples, like
(3, 4, 5)
.....
Make sure your output contains a triple only once, and not any
reordered ones, this means (3, 4, 5) is the same as (4, 3, 5) and
so only the increasing order triple should be printed out. This
means only (3, 4, 5) shoud be printed.
You program should print out all possible pythogorean triples with
sides <= 50.
*/
class Triangle
{
public:
Triangle(int a, int b, int c) // constructor.
Implement this
{}
bool isPythagorean() const // implement this
properly
{
return false;
}
private: // what do you put in private section?
}
int main()
{
}
Program
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
/* Define the structure Triplet to store the values of the
three sides in the vector */
struct Triplet
{
int side1, side2, side3;
};
/* Definition of the class Triangle to generate and print
all possible pythogorean triples with sides <= 50 */
class Triangle
{
public:
// constructor to assign user-defined values to the
// private member variables
Triangle(int a, int b, int c)
{
x = a;
y = b;
z = c;
}
/* isPythagorean() method is used to check if three integer
values satisfy the rules of a pythogorean triple as
defined in the question */
bool isPythagorean() const
{
if(z*z == x*x + y*y)
return true; // the given values form a pythogorean
triple
else
return false; // the given values donot form pythogorean
triple
}
// Private member variables for three sides of a triangle.
private:
int x,y,z;
};
int main()
{
// range specifies the upper limit of any value in a pythogorean
triple
int range=50;
// pythogorean_triples is a vector of type Triplet
vector<Triplet> pythogorean_triples;
/* The below given nested for loop is to get all possible
combinations
of x, y, z with values 1 to 50 and check for possible
combinations
that result in pythogorean triple */
// start the outer loop with value 1 and upper limit as range
for(int side1=1; side1<=range; side1++){
// the range of side2 starts from side1 and ends with range
for(int side2=side1; side2<=range;
side2++){
// the range of side3 starts from side2 and ends with range
for(int side3=side2; side3<=range; side3++){
// Create an object of Triangle with the indices of the
// the three for loops getting passed as sides to the
constructor
Triangle t(side1, side2, side3);
// isPythagorean() with check if those three values satify
the
// condition and return true or false
if(t.isPythagorean())
// push_back method appends the pythogorean triplet
// to the end of the vector.
pythogorean_triples.push_back({side1, side2,
side3});
}
}
}
// size() returns the number of elements in the vector
int size = pythogorean_triples.size();
// repeat the printing process for each triplet in the vector
for (int i=0; i<size; i++)
{
cout << "( " << pythogorean_triples[i].side1 <<
", " << pythogorean_triples[i].side2
<< ", " << pythogorean_triples[i].side3 << " )"
<< endl;
}
}
Output: