forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPythagoreanTriple.java
More file actions
43 lines (37 loc) · 1.16 KB
/
PythagoreanTriple.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
package com.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
*/
public final class PythagoreanTriple {
private PythagoreanTriple() {
}
/**
* 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}
*/
public static boolean isPythagTriple(int a, int b, int c) {
if (a <= 0 || b <= 0 || c <= 0) {
return false;
}
// Sort the sides so the largest is treated as hypotenuse
int[] sides = {a, b, c};
java.util.Arrays.sort(sides);
int x = sides[0];
int y = sides[1];
int hypotenuse = sides[2];
return x * x + y * y == hypotenuse * hypotenuse;
}
}