diff --git a/js/sahil9001_gcdalgo.js b/js/sahil9001_gcdalgo.js new file mode 100644 index 00000000..17b79cf7 --- /dev/null +++ b/js/sahil9001_gcdalgo.js @@ -0,0 +1,14 @@ +let euclideanAlgorithm = function (A, B) { + // Make input numbers positive. + const a = Math.abs(A); + const b = Math.abs(B); + + // To make algorithm work faster instead of subtracting one number from the other + // we may use modulo operation. + return (b === 0) ? a : euclideanAlgorithm(b, a % b); +} + + +//Taking two numbers +let a = 20, b = 10; +console.log("GCD of " + a + " " + b + " is " + euclideanAlgorithm(a, b)); \ No newline at end of file