Skip to content

Commit 67e1b47

Browse files
committed
LCM es6 code added
1 parent 101137e commit 67e1b47

File tree

4 files changed

+30
-0
lines changed

4 files changed

+30
-0
lines changed

Number Theory/LCM/es6/gcd.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// A recursive approach to GCD Implementation
2+
// Details of this code can be found at 'GCD' Directory
3+
const gcd = (a, b) => (b === 0) ? a : gcd(b, a%b);
4+
5+
module.exports = gcd

Number Theory/LCM/es6/lcm.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/* Least Commond Multiple (LCM) Finding in JavaScript */
2+
const gcd = require('./gcd');
3+
4+
/*
5+
The basic formula of Calculating LCM of 'a' and 'b' is:
6+
LCM(a,b) = (a * b)/gcd(a,b)
7+
As multiplying (a*b) can be a large number for some 'a' and 'b',
8+
we are dividing one of the numbers, 'a' here,
9+
by the gcd(a,b) before multiplication and reducing the possibility
10+
of 'integer overflow'
11+
*/
12+
module.exports.lcm = (a, b) => ((a / gcd(a,b)) * b);

Number Theory/LCM/es6/test.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const { lcm } = require('./lcm');
2+
3+
/************ Testing LCM ***************/
4+
console.log(lcm(5, 10));
5+
console.log(lcm(14, 35));

Number Theory/LCM/lcm.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
/* Least Commond Multiple (LCM) Finding in JavaScript */
22

3+
/*
4+
The basic formula of Calculating LCM of 'a' and 'b' is:
5+
LCM(a,b) = (a * b)/gcd(a,b)
6+
As multiplying (a*b) can be a large number for some 'a' and 'b',
7+
we are dividing one of the numbers, 'a' here,
8+
by the gcd(a,b) before multiplication and reducing the possibility
9+
of 'integer overflow'
10+
*/
311
function lcm(a, b) {
412
return (a / gcd(a,b)) * b;
513
}

0 commit comments

Comments
 (0)