Declare and implement a class named Pythagorean . The class will contain the fol
ID: 3544978 • Letter: D
Question
Declare and implement a class named Pythagorean. The class will contain the following three 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.
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
Explanation / Answer
import java.util.Scanner;
public class Pythagorean {
public static void getTriples(int n){
System.out.println("triples:");
int a, b, c;
for(c = 1; c <= n; c++){
for(b = 1; b <= c; b++){
for(a = 1; a <= b; a++){
if(a * a + b * b == c * c){
System.out.println(a + " " + b + " " + c);
System.out.println();
}
}
}
}
System.out.println(" ");
}
public static void getQuadruples(int n){
System.out.println("quadruples:");
int a, b, c, d;
for(d = 1; d <= n; d++){
for(c = 1; c <= d; c++){
for(b = 1; b <= c; b++){
for(a = 1; a <= b; a++){
if(a * a + b * b + c * c == d * d){
System.out.println(a + " " + b + " " + c + " " + d);
System.out.println();
}
}
}
}
}
System.out.println(" ");
}
public static void getQuintuples(int n){
System.out.println("quintuples:");
int a, b, c, d, e;
for(e = 1; e <= n; e++){
for(d = 1; d <= e; d++){
for(c = 1; c <= d; c++){
for(b = 1; b <= c; b++){
for(a = 1; a <= b; a++){
if(a * a + b * b + c * c + d * d == e * e){
System.out.println(a + " " + b + " " + c + " " + d + " " + e);
System.out.println();
}
}
}
}
}
}
System.out.println(" ");
}
public static void main(String argd[]){
System.out.println("Enter n:");
Scanner in = new Scanner(System.in);
int n = in.nextInt();
getTriples(n);
getQuadruples(n);
getQuintuples(n);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.