Skip to content

Commit fa2ca9d

Browse files
authored
refactor: improve PythagoreanTriple logic and add parameterized tests (#6350)
1 parent 2f5bc8c commit fa2ca9d

File tree

2 files changed

+39
-30
lines changed

2 files changed

+39
-30
lines changed
Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,43 @@
11
package com.thealgorithms.maths;
22

33
/**
4-
* https://en.wikipedia.org/wiki/Pythagorean_triple
4+
* Utility class to check if three integers form a Pythagorean triple.
5+
* A Pythagorean triple consists of three positive integers a, b, and c,
6+
* such that a² + b² = c².
7+
*
8+
* Common examples:
9+
* - (3, 4, 5)
10+
* - (5, 12, 13)
11+
*
12+
* Reference: https://en.wikipedia.org/wiki/Pythagorean_triple
513
*/
614
public final class PythagoreanTriple {
7-
private PythagoreanTriple() {
8-
}
915

10-
public static void main(String[] args) {
11-
assert isPythagTriple(3, 4, 5);
12-
assert isPythagTriple(5, 12, 13);
13-
assert isPythagTriple(6, 8, 10);
14-
assert !isPythagTriple(10, 20, 30);
15-
assert !isPythagTriple(6, 8, 100);
16-
assert !isPythagTriple(-1, -1, 1);
16+
private PythagoreanTriple() {
1717
}
1818

1919
/**
20-
* Check if a,b,c are a Pythagorean Triple
20+
* Checks whether three integers form a Pythagorean triple.
21+
* The order of parameters does not matter.
2122
*
22-
* @param a x/y component length of a right triangle
23-
* @param b y/x component length of a right triangle
24-
* @param c hypotenuse length of a right triangle
25-
* @return boolean <tt>true</tt> if a, b, c satisfy the Pythagorean theorem,
26-
* otherwise
27-
* <tt>false</tt>
23+
* @param a one side length
24+
* @param b another side length
25+
* @param c another side length
26+
* @return {@code true} if (a, b, c) can form a Pythagorean triple, otherwise {@code false}
2827
*/
2928
public static boolean isPythagTriple(int a, int b, int c) {
3029
if (a <= 0 || b <= 0 || c <= 0) {
3130
return false;
32-
} else {
33-
return (a * a) + (b * b) == (c * c);
3431
}
32+
33+
// Sort the sides so the largest is treated as hypotenuse
34+
int[] sides = {a, b, c};
35+
java.util.Arrays.sort(sides);
36+
37+
int x = sides[0];
38+
int y = sides[1];
39+
int hypotenuse = sides[2];
40+
41+
return x * x + y * y == hypotenuse * hypotenuse;
3542
}
3643
}
Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
11
package com.thealgorithms.maths;
22

3-
import static org.junit.jupiter.api.Assertions.assertFalse;
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
44
import static org.junit.jupiter.api.Assertions.assertTrue;
55

66
import org.junit.jupiter.api.Test;
7+
import org.junit.jupiter.params.ParameterizedTest;
8+
import org.junit.jupiter.params.provider.CsvSource;
79

810
public class PythagoreanTripleTest {
911

12+
@ParameterizedTest
13+
@CsvSource({"3, 4, 5, true", "6, 8, 10, true", "9, 12, 15, true", "12, 16, 20, true", "15, 20, 25, true", "18, 24, 30, true", "5, 20, 30, false", "6, 8, 100, false", "-2, -2, 2, false", "0, 0, 0, false", "5, 5, 5, false"})
14+
void testIsPythagoreanTriple(int a, int b, int c, boolean expected) {
15+
assertEquals(expected, PythagoreanTriple.isPythagTriple(a, b, c));
16+
}
17+
1018
@Test
11-
public void testPythagoreanTriple() {
12-
assertTrue(PythagoreanTriple.isPythagTriple(3, 4, 5));
13-
assertTrue(PythagoreanTriple.isPythagTriple(6, 8, 10));
14-
assertTrue(PythagoreanTriple.isPythagTriple(9, 12, 15));
15-
assertTrue(PythagoreanTriple.isPythagTriple(12, 16, 20));
16-
assertTrue(PythagoreanTriple.isPythagTriple(15, 20, 25));
17-
assertTrue(PythagoreanTriple.isPythagTriple(18, 24, 30));
18-
assertFalse(PythagoreanTriple.isPythagTriple(5, 20, 30));
19-
assertFalse(PythagoreanTriple.isPythagTriple(6, 8, 100));
20-
assertFalse(PythagoreanTriple.isPythagTriple(-2, -2, 2));
19+
void testUnorderedInputStillValid() {
20+
// Should still detect Pythagorean triples regardless of argument order
21+
assertTrue(PythagoreanTriple.isPythagTriple(5, 3, 4));
22+
assertTrue(PythagoreanTriple.isPythagTriple(13, 12, 5));
2123
}
2224
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy