Skip to content

Commit 918189e

Browse files
author
Ray
committed
Add PythagoreanTriple.java
1 parent f32a63e commit 918189e

1 file changed

Lines changed: 32 additions & 0 deletions

File tree

Maths/PythagoreanTriple.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package Maths;
2+
3+
/**
4+
* https://en.wikipedia.org/wiki/Pythagorean_triple
5+
*
6+
*/
7+
public class PythagoreanTriple {
8+
public static void main(String[] args) {
9+
assert isPythagTriple(3,4,5);
10+
assert isPythagTriple(5,12,13);
11+
assert isPythagTriple(6,8,10);
12+
assert !isPythagTriple(10,20,30);
13+
assert !isPythagTriple(6,8,100);
14+
assert !isPythagTriple(-1,-1,1);
15+
}
16+
17+
/**
18+
* Check if a,b,c are a Pythagorean Triple
19+
*
20+
* @param a x/y component length of a right triangle
21+
* @param b y/x component length of a right triangle
22+
* @param c hypotenuse length of a right triangle
23+
* @return boolean <tt>true</tt> if a, b, c satisfy the Pythagorean theorem, otherwise <tt>false</tt>
24+
*/
25+
public static boolean isPythagTriple(int a, int b, int c) {
26+
if(a <= 0 || b <= 0 || c <= 0) {
27+
return false;
28+
} else {
29+
return (a * a) + (b * b) == (c * c);
30+
}
31+
}
32+
}

0 commit comments

Comments
 (0)