Skip to content
This repository was archived by the owner on Jun 26, 2022. It is now read-only.

Commit 89150e4

Browse files
Merge pull request #158 from sahil9001/JS_Euclidean_algorithm
algo:Added GCD algorithm of two numbers a and b
2 parents b30df96 + f62bdc5 commit 89150e4

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

js/sahil9001_gcdalgo.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
let euclideanAlgorithm = function (A, B) {
2+
// Make input numbers positive.
3+
const a = Math.abs(A);
4+
const b = Math.abs(B);
5+
6+
// To make algorithm work faster instead of subtracting one number from the other
7+
// we may use modulo operation.
8+
return (b === 0) ? a : euclideanAlgorithm(b, a % b);
9+
}
10+
11+
12+
//Taking two numbers
13+
let a = 20, b = 10;
14+
console.log("GCD of " + a + " " + b + " is " + euclideanAlgorithm(a, b));

0 commit comments

Comments
 (0)