Skip to content

Commit 101137e

Browse files
committed
GCD es6 code added
1 parent d22738d commit 101137e

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

Number Theory/GCD/es6/gcd.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/* Greatest Common Divisor (GCD) Finding in JavaScript */
2+
3+
const gcd = (a, b) => {
4+
while (true) {
5+
var remainder = a%b;
6+
if (remainder === 0) return b;
7+
8+
a = b;
9+
b = remainder;
10+
}
11+
}
12+
13+
//A recursive approach to GCD Implementation
14+
const recursive_gcd = (a, b) => (b === 0) ? a : recursive_gcd(b, a%b);
15+
16+
module.exports = {
17+
gcd,
18+
recursive_gcd
19+
}

Number Theory/GCD/es6/test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const { gcd, recursive_gcd } = require('./gcd');
2+
3+
/************ Testing GCD ***************/
4+
console.log(gcd(5, 10));
5+
console.log(gcd(14, 35));
6+
7+
console.log(recursive_gcd(5, 13));
8+
console.log(recursive_gcd(24, 60));

0 commit comments

Comments
 (0)