From 107dd357487a8f7980addbf6c1a178deb9ad5fdb Mon Sep 17 00:00:00 2001 From: Haminhquan3001 <57892010+Haminhquan3001@users.noreply.github.com> Date: Fri, 8 Dec 2023 04:29:10 -0500 Subject: [PATCH] Implement Pythagorean Algorithm --- src/algorithms/math/pythagorean-theorem/README.md | 6 ++++++ .../__test__/pythagoreanTheorem.test.js | 12 ++++++++++++ .../math/pythagorean-theorem/pythagoreanTheorem.js | 9 +++++++++ 3 files changed, 27 insertions(+) create mode 100644 src/algorithms/math/pythagorean-theorem/README.md create mode 100644 src/algorithms/math/pythagorean-theorem/__test__/pythagoreanTheorem.test.js create mode 100644 src/algorithms/math/pythagorean-theorem/pythagoreanTheorem.js diff --git a/src/algorithms/math/pythagorean-theorem/README.md b/src/algorithms/math/pythagorean-theorem/README.md new file mode 100644 index 00000000..5ecd0aea --- /dev/null +++ b/src/algorithms/math/pythagorean-theorem/README.md @@ -0,0 +1,6 @@ +Pythagorean theorem: the area of the square whose side is the hypotenuse +(the side opposite the right angle) is equal to the sum of +the areas of the squares on the other two sides. + +## References: +https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=&cad=rja&uact=8&ved=2ahUKEwjnuNvt9v6CAxXyv4kEHWYeCoAQFnoECC4QAQ&url=https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FPythagorean_theorem&usg=AOvVaw3T92yCCxl4W1aa8hM6ft__&opi=89978449 diff --git a/src/algorithms/math/pythagorean-theorem/__test__/pythagoreanTheorem.test.js b/src/algorithms/math/pythagorean-theorem/__test__/pythagoreanTheorem.test.js new file mode 100644 index 00000000..958cc8dd --- /dev/null +++ b/src/algorithms/math/pythagorean-theorem/__test__/pythagoreanTheorem.test.js @@ -0,0 +1,12 @@ +import isPythagoreanTriangle from '../pythagoreanTheorem'; + +describe('isPythagoreanTriangle', () => { + it('should return true if hyperparameters satisfied the pythagorean theorem, otherwise return false', () => { + expect(isPythagoreanTriangle(0, 0, 0)).toEqual(false); + expect(isPythagoreanTriangle(-1, 2, 5)).toEqual(false); + expect(isPythagoreanTriangle(3, 4, 5)).toEqual(true); + expect(isPythagoreanTriangle(3, 4, -5)).toEqual(false); + expect(isPythagoreanTriangle(-1, -1, 1)).toEqual(false); + expect(isPythagoreanTriangle(3, 4, 25)).toEqual(false); + }); +}); diff --git a/src/algorithms/math/pythagorean-theorem/pythagoreanTheorem.js b/src/algorithms/math/pythagorean-theorem/pythagoreanTheorem.js new file mode 100644 index 00000000..ed456ca4 --- /dev/null +++ b/src/algorithms/math/pythagorean-theorem/pythagoreanTheorem.js @@ -0,0 +1,9 @@ +/** + * @param {number} a + * @param {number} b + * @param {number} c + * @return {boolean} + */ +export default function isPythagoreanTriangle(a, b, c) { + return (a <= 0 || b <= 0 || c <= 0) ? false : ((a * a) + (b * b) === (c * c)); +}