2. Declare and implement a class named Pythagorean. The class will contain the f
ID: 3935433 • Letter: 2
Question
2. Declare and implement a class named Pythagorean. The class will contain the following five methods:
• getTriples(int n) prints all Pythagorean triples (a, b, c) such that a2 + b2 = c2, 0 < a = b = c = n.
• getQuadruples(int n) prints all Pythagorean quadruples (a, b, c, d) such that a2 + b2 + c2 = d2, 0 < a = b = c = d = n.
• getQuintuples(int n) prints all Pythagorean quintuples (a, b, c, d, e) such that a2 + b2 + c2 + d2 = e2, 0 < a = b = c = d = e = n.
• getHextuples(int n) prints all Pythagorean hextuples (a, b, c, d, e, f) such that a2 + b2 + c2 + d2 + e2 = f2, 0 < a = b = c = d = e = f = n.
• getSeptuples(int n) prints all Pythagorean septuples (a, b, c, d, e, f, g) such that a2 + b2 + c2 + d2 + e2 + f2 = g2, 0 < a = b = c = d = e = f = g = n.
For example, for n = 10. These methods will print
triples:
3, 4, 5
6, 8, 10
quadruples:
1, 2, 2, 3
1, 4, 8, 9
2, 3, 6, 7
2, 4, 4, 6
3, 6, 6, 9
4, 4, 7, 9
quintuples:
1, 1, 1, 1, 2
1, 1, 3, 5, 6
1, 1, 7, 7, 10
1, 2, 2, 4, 5
1, 3, 3, 9, 10
1, 4, 4, 4, 7
1, 5, 5, 7, 10
2, 2, 2, 2, 4
2, 2, 3, 8, 9
2, 2, 4, 5, 7
2, 4, 4, 8, 10
2, 4, 5, 6, 9
3, 3, 3, 3, 6
4, 4, 4, 4, 8
5, 5, 5, 5, 10
hextuples:
1, 1, 1, 2, 3, 4
1, 1, 1, 4, 9, 10
1, 1, 1, 5, 6, 8
1, 1, 2, 3, 7, 8
1, 1, 3, 3, 4, 6
1, 1, 3, 5, 8, 10
1, 2, 2, 2, 6, 7
1, 2, 2, 6, 6, 9
1, 2, 3, 5, 5, 8
1, 3, 3, 3, 6, 8
1, 3, 4, 5, 7, 10
2, 2, 2, 2, 3, 5
2, 2, 2, 4, 6, 8
2, 2, 3, 4, 4, 7
2, 3, 4, 4, 6, 9
3, 3, 3, 3, 8, 10
3, 4, 5, 5, 5, 10
4, 4, 4, 4, 6, 10
septuples:
1, 1, 1, 1, 1, 2, 3
1, 1, 1, 1, 3, 6, 7
1, 1, 1, 1, 4, 4, 6
1, 1, 1, 2, 2, 5, 6
1, 1, 1, 2, 3, 3, 5
1, 1, 1, 2, 5, 7, 9
1, 1, 1, 3, 4, 6, 8
1, 1, 1, 5, 6, 6, 10
1, 1, 2, 2, 3, 9, 10
1, 1, 2, 3, 3, 5, 7
1, 1, 2, 3, 6, 7, 10
1, 1, 2, 5, 5, 5, 9
1, 1, 3, 3, 4, 8, 10
1, 1, 3, 3, 5, 6, 9
1, 2, 2, 2, 2, 8, 9
1, 2, 2, 3, 3, 3, 6
1, 2, 3, 3, 3, 7, 9
1, 2, 3, 3, 4, 5, 8
1, 2, 3, 5, 5, 6, 10
1, 3, 3, 3, 6, 6, 10
1, 3, 3, 4, 4, 7, 10
1, 4, 4, 4, 4, 4, 9
2, 2, 2, 2, 2, 4, 6
2, 2, 2, 2, 4, 7, 9
2, 2, 2, 4, 6, 6, 10
2, 2, 3, 3, 5, 7, 10
2, 2, 4, 4, 4, 5, 9
2, 3, 3, 3, 3, 3, 7
2, 3, 3, 3, 5, 5, 9
3, 3, 3, 3, 3, 6, 9
3, 3, 4, 4, 5, 5, 10
Value n must be read from the user in the main method
Explanation / Answer
#include <iostream>
#include <cmath>
using namespace std;
class pythagorean{
public:
void getTriples( int n ){
cout << "triples:" << endl;
for(int a= 1; a <= n; a++ ){
for(int b= a; b <= n; b++ ){
double c = sqrt( a*a + b*b );
if( c == int(c) && c<=n ){
cout << a << " " << b << " " << int(c) << endl;
}}}}
void getQuadruples( int n ){
cout << "quadruples:" << endl;
for(int a= 1; a <= n; a++ ){
for(int b= a; b <= n; b++ ){
for(int c= b; c <= n; c++ ){
double d = sqrt( a*a + b*b + c*c );
if( d == int(d) && d<=n ){
cout << a << " " << b << " " << c << " " << int(d) << endl;
}}}}}
void getQuintuples( int n ){
cout << "quintuples:" << endl;
for(int a= 1; a <= n; a++ ){
for(int b= a; b <= n; b++ ){
for(int c= b; c <= n; c++ ){
for( int d=c; d <= n; d++ ){
double e = sqrt( a*a + b*b + c*c + d*d );
if( e == int(e) && e <=n ){
cout << a << " " << b << " " << c << " " << d << " " << int(e) << endl;
}}}}}}
void getHextuples( int n ){
cout << "hextuples:" << endl;
for(int a= 1; a <= n; a++ ){
for(int b= a; b <= n; b++ ){
for(int c= b; c <= n; c++ ){
for( int d=c; d <= n; d++ ){
for(int e=d; e<=n; e++ ){
double f = sqrt( a*a + b*b + c*c + d*d + e*e );
if( f == int(f) && f <=n ){
cout << a << " " << b << " " << c << " " << d << " " << e << " " <<int(f) << endl;
}}}}}}}
void getSeptuples( int n ){
cout << "septuples:" << endl;
for(int a= 1; a <= n; a++ ){
for(int b= a; b <= n; b++ ){
for(int c= b; c <= n; c++ ){
for( int d=c; d <= n; d++ ){
for(int e=d; e<=n; e++ ){
for(int f=e; f<=n; f++){
double g = sqrt( a*a + b*b + c*c + d*d + e*e + f*f );
if( g == int(g) && g <=n ){
cout << a << " " << b << " " << c << " " << d << " " << e << " " << f << " " <<int(g) << endl;
}}}}}}}}
};
int main(){
int n;
cin >> n;
pythagorean p;
p.getTriples(n);
p.getQuadruples(n);
p.getQuintuples(n);
p.getHextuples(n);
p.getSeptuples(n);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.