Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 21.3k
Expand file tree
/
Copy pathPythagoreanTriple.java
More file actions
Latest commit
43 lines (37 loc) · 1.16 KB
/
Copy pathPythagoreanTriple.java
File metadata and controls
43 lines (37 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
packagecom.thealgorithms.maths;
/**
* Utility class to check if three integers form a Pythagorean triple.
* A Pythagorean triple consists of three positive integers a, b, and c,
* such that a² + b² = c².
*
* Common examples:
* - (3, 4, 5)
* - (5, 12, 13)
*
* Reference: https://en.wikipedia.org/wiki/Pythagorean_triple
*/
publicfinalclassPythagoreanTriple {
privatePythagoreanTriple() {
}
/**
* Checks whether three integers form a Pythagorean triple.
* The order of parameters does not matter.
*
* @param a one side length
* @param b another side length
* @param c another side length
* @return {@code true} if (a, b, c) can form a Pythagorean triple, otherwise {@code false}
*/
publicstaticbooleanisPythagTriple(inta, intb, intc) {
if (a <= 0 || b <= 0 || c <= 0) {
returnfalse;
}
// Sort the sides so the largest is treated as hypotenuse
int[] sides = {a, b, c};
java.util.Arrays.sort(sides);
intx = sides[0];
inty = sides[1];
inthypotenuse = sides[2];
returnx * x + y * y == hypotenuse * hypotenuse;
}
}