diff --git a/src/algorithms/math/euclidean-algorithm/euclideanAlgorithm.js b/src/algorithms/math/euclidean-algorithm/euclideanAlgorithm.js index fe3b57dd60..8a5f830683 100644 --- a/src/algorithms/math/euclidean-algorithm/euclideanAlgorithm.js +++ b/src/algorithms/math/euclidean-algorithm/euclideanAlgorithm.js @@ -3,9 +3,23 @@ * @param {number} originalB * @return {number} */ + +/*Method 1: A bit Complex to understand*/ export default function euclideanAlgorithm(originalA, originalB) { const a = Math.abs(originalA); const b = Math.abs(originalB); return (b === 0) ? a : euclideanAlgorithm(b, a % b); } + +/*Method 2: Easy to evaluate*/ +export default function euclideanAlgorithm2(originalA, originalB) { + const a = Math.abs(originalA); + const b = Math.abs(originalB); + + while(a != b){ + [a,b] = a>b : [a-b, b] : [a, b-a] + } + + return a || b; +}