Skip to content

Commit 6960272

Browse files
committed
Create find_lcm.js
- Created find_lcm.js - Created maths folder
1 parent f37cac8 commit 6960272

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

maths/find_lcm.js

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
author: PatOnTheBack
3+
license: GPL-3.0 or later
4+
5+
Modified from:
6+
https://github.com/TheAlgorithms/Python/blob/master/maths/find_lcm.py
7+
8+
More about LCM:
9+
https://en.wikipedia.org/wiki/Least_common_multiple
10+
*/
11+
12+
// Find the LCM of two numbers.
13+
function find_lcm(num_1, num_2) {
14+
"use strict";
15+
var max_num,
16+
lcm;
17+
// Check to see whether num_1 or num_2 is larger.
18+
if (num_1 > num_2) {
19+
max_num = num_1;
20+
} else {
21+
max_num = num_2;
22+
}
23+
lcm = max_num;
24+
25+
while (true) {
26+
if ((lcm % num_1 === 0) && (lcm % num_2 === 0)) {
27+
break;
28+
}
29+
lcm += max_num;
30+
}
31+
return lcm;
32+
}
33+
34+
// Run `find_lcm` Function
35+
var num_1 = 12,
36+
num_2 = 76;
37+
console.log(find_lcm(num_1, num_2));

0 commit comments

Comments
 (0)