File tree Expand file tree Collapse file tree 4 files changed +30
-0
lines changed Expand file tree Collapse file tree 4 files changed +30
-0
lines changed Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 ) ;
Original file line number Diff line number Diff line change
1
+ const { lcm } = require ( './lcm' ) ;
2
+
3
+ /************ Testing LCM ***************/
4
+ console . log ( lcm ( 5 , 10 ) ) ;
5
+ console . log ( lcm ( 14 , 35 ) ) ;
Original file line number Diff line number Diff line change 1
1
/* Least Commond Multiple (LCM) Finding in JavaScript */
2
2
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
+ */
3
11
function lcm ( a , b ) {
4
12
return ( a / gcd ( a , b ) ) * b ;
5
13
}
You can’t perform that action at this time.
0 commit comments