From 918189ec209910828f1f6e4892443f7007039066 Mon Sep 17 00:00:00 2001 From: Ray Date: Sun, 16 Aug 2020 10:32:10 -0400 Subject: [PATCH] Add PythagoreanTriple.java --- Maths/PythagoreanTriple.java | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 Maths/PythagoreanTriple.java diff --git a/Maths/PythagoreanTriple.java b/Maths/PythagoreanTriple.java new file mode 100644 index 00000000..4eae9615 --- /dev/null +++ b/Maths/PythagoreanTriple.java @@ -0,0 +1,32 @@ +package Maths; + +/** + * https://en.wikipedia.org/wiki/Pythagorean_triple + * + */ +public class PythagoreanTriple { + public static void main(String[] args) { + assert isPythagTriple(3,4,5); + assert isPythagTriple(5,12,13); + assert isPythagTriple(6,8,10); + assert !isPythagTriple(10,20,30); + assert !isPythagTriple(6,8,100); + assert !isPythagTriple(-1,-1,1); + } + + /** + * Check if a,b,c are a Pythagorean Triple + * + * @param a x/y component length of a right triangle + * @param b y/x component length of a right triangle + * @param c hypotenuse length of a right triangle + * @return boolean true if a, b, c satisfy the Pythagorean theorem, otherwise false + */ + public static boolean isPythagTriple(int a, int b, int c) { + if(a <= 0 || b <= 0 || c <= 0) { + return false; + } else { + return (a * a) + (b * b) == (c * c); + } + } +}