From f62bdc5a7bcc925ee3ce4d309f7481d643624ab6 Mon Sep 17 00:00:00 2001 From: sahil9001 Date: Thu, 1 Oct 2020 14:27:25 +0530 Subject: [PATCH] algo:Added GCD algorithm of two numbers a and b Reference #47 --- js/sahil9001_gcdalgo.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 js/sahil9001_gcdalgo.js 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