C++ Due 22 September 2017 The Pythagorean Theorem applies to right triangles whe
ID: 3886972 • Letter: C
Question
C++ Due 22 September 2017 The Pythagorean Theorem applies to right triangles where the square of the hypotenuse is equal to the sum of the squares of the other two sides. There are sets of three numbers known as Pythagorean triples and they satisfy the Pythagorean Theorem. They are positive integers. Examples of such sets are: 3, 4, 5 and 5, 12, 13. You are to write a function that will determine whether three integers form or don't form a Pythagorean triple. The output of the function is Boolean. Then using this function, determine all the Pythagorean triples starting with 3, 4, 5 and continuing through 100, 100, 100. Output should be to a file. Duplication needs to be minimized. For example 3, 4, 5 and 4, 3, 5 are the same Pythagorean triple.Explanation / Answer
c++ code:
#include<bits/stdc++.h>
using namespace std;
//function to check if this is a pythagorean triplet!
bool isPythagorean_trip(int s1,int s2,int s3)
{
bool flag;
s1 = s1*s1;
s2 = s2*s2;
s3 = s3*s3;
if( (s1 == s2 + s3) or (s2 == s1 + s3) or (s3 == s2 + s1) )
{
flag = true;
}
else
{
flag = false;
}
return flag;
}
int main()
{
vector< vector<int> > unique_triplets; // vector to store all pythagorean triplets
for (int i = 3; i <= 100; ++i)
{
for (int j = 3; j <= 100; ++j)
{
for (int k = 3; k <= 100; ++k)
{
if( isPythagorean_trip(i,j,k) )
{
vector<int> tmp; tmp.push_back(i);tmp.push_back(j);tmp.push_back(k);
sort(tmp.begin(), tmp.end());
bool isduplicate = false;
//check if this is a duplicate triplet
for (int x = 0; x < unique_triplets.size(); ++x)
{
vector<int> v = unique_triplets[x];
if(v[0] == tmp[0] and v[1] == tmp[1] and v[2] == tmp[2])
{
isduplicate = true;
false;
}
}
if(isduplicate != true)
{
unique_triplets.push_back(tmp);
cout << "( " << i << " , " << j << " , " << k << " ) is a Pythagorean triplet!" << endl;
}
}
}
}
}
return 0;
}
Sample Output:
( 3 , 4 , 5 ) is a Pythagorean triplet!
( 5 , 12 , 13 ) is a Pythagorean triplet!
( 6 , 8 , 10 ) is a Pythagorean triplet!
( 7 , 24 , 25 ) is a Pythagorean triplet!
( 8 , 15 , 17 ) is a Pythagorean triplet!
( 9 , 12 , 15 ) is a Pythagorean triplet!
( 9 , 40 , 41 ) is a Pythagorean triplet!
( 10 , 24 , 26 ) is a Pythagorean triplet!
( 11 , 60 , 61 ) is a Pythagorean triplet!
( 12 , 16 , 20 ) is a Pythagorean triplet!
( 12 , 35 , 37 ) is a Pythagorean triplet!
( 13 , 84 , 85 ) is a Pythagorean triplet!
( 14 , 48 , 50 ) is a Pythagorean triplet!
( 15 , 20 , 25 ) is a Pythagorean triplet!
( 15 , 36 , 39 ) is a Pythagorean triplet!
( 16 , 30 , 34 ) is a Pythagorean triplet!
( 16 , 63 , 65 ) is a Pythagorean triplet!
( 18 , 24 , 30 ) is a Pythagorean triplet!
( 18 , 80 , 82 ) is a Pythagorean triplet!
( 20 , 21 , 29 ) is a Pythagorean triplet!
( 20 , 48 , 52 ) is a Pythagorean triplet!
( 21 , 28 , 35 ) is a Pythagorean triplet!
( 21 , 72 , 75 ) is a Pythagorean triplet!
( 24 , 32 , 40 ) is a Pythagorean triplet!
( 24 , 45 , 51 ) is a Pythagorean triplet!
( 24 , 70 , 74 ) is a Pythagorean triplet!
( 25 , 60 , 65 ) is a Pythagorean triplet!
( 27 , 36 , 45 ) is a Pythagorean triplet!
( 28 , 45 , 53 ) is a Pythagorean triplet!
( 28 , 96 , 100 ) is a Pythagorean triplet!
( 30 , 40 , 50 ) is a Pythagorean triplet!
( 30 , 72 , 78 ) is a Pythagorean triplet!
( 32 , 60 , 68 ) is a Pythagorean triplet!
( 33 , 44 , 55 ) is a Pythagorean triplet!
( 33 , 56 , 65 ) is a Pythagorean triplet!
( 35 , 84 , 91 ) is a Pythagorean triplet!
( 36 , 48 , 60 ) is a Pythagorean triplet!
( 36 , 77 , 85 ) is a Pythagorean triplet!
( 39 , 52 , 65 ) is a Pythagorean triplet!
( 39 , 80 , 89 ) is a Pythagorean triplet!
( 40 , 42 , 58 ) is a Pythagorean triplet!
( 40 , 75 , 85 ) is a Pythagorean triplet!
( 42 , 56 , 70 ) is a Pythagorean triplet!
( 45 , 60 , 75 ) is a Pythagorean triplet!
( 48 , 55 , 73 ) is a Pythagorean triplet!
( 48 , 64 , 80 ) is a Pythagorean triplet!
( 51 , 68 , 85 ) is a Pythagorean triplet!
( 54 , 72 , 90 ) is a Pythagorean triplet!
( 57 , 76 , 95 ) is a Pythagorean triplet!
( 60 , 63 , 87 ) is a Pythagorean triplet!
( 60 , 80 , 100 ) is a Pythagorean triplet!
( 65 , 72 , 97 ) is a Pythagorean triplet!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.