What is LCM? | Maths Explanation for JavaScript Kids
Akin to finding H.C.F., L.C.M. is commonly found by repeated factorization.
Only this time, the factors do not have to be common amongst the set of numbers.
If we have the set of numbers 8, 12 and
18 for example, their L.C.M. is found thus:
Hence, L.C.M. of 8, 12 and 18 = 2 X 2 X 2 x 3 x 3 = 72
Step-by-Step Guide to L.C.M. by Factorisation in JavaScript
We shall follow the steps below in writing our JavaScript LCM code.
Step 1:
Do a numerical reverse sort on the (resulting) set so its first member is the largest in the set.
Step 2:
Starting with 2, iteratively check through the set of numbers for individual factors.
Step 3:
For each individual factor, divide affected member(s) of the number set by the factor.
Step 4:
Repeat the above steps recursively until there are no more individual factors.
Create 2 new files; On Notepad++: File, New.
Call them LCM.html and LCM.js.
Type out the adjoining JavaScript code for finding Lowest Common Multiple (L.C.M.).
So! JavaScript Fun Practice Exercise - Find LCM
As a fun practice exercise, feel free to try out your own numbers, and see how the JavaScript code finds the LCM of those numbers.
JavaScript Code for LCM.html
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Find H.C.F.</title>
<script src="LCM.js"></script>
</head>
<body>
<h4>Lowest Common Multiple of a Set of Numbers.</h4>
<div id="myLCM"></div>
<script>/*
* 'set_of_numbers' holds the numbers whose LCM we are trying to find.
* Add as many of these numbers as you wish by extending the array.
*/
var set_of_numbers = [];
set_of_numbers[0] = 4;
set_of_numbers[1] = 5;
set_of_numbers[2] = 6;
var result = getLCM(set_of_numbers); // call to our function
document.getElementById("myLCM").innerHTML = "The L.C.M. of " +
set_of_numbers.join(", ") + " is: " + result;
</script>
</body>
</html>
LCM.js
var index = 0; // index into array all_factors
/* To avoid recording the same factor multiple times
count off as it is used on the different numbers. */
var state_check = false;
/* Pass of Array(according to javascript) is by reference;
so make a copy that will be passed instead. */
var arg_copy = [];
var i = 2; // start checking for factors from variable 2
/*
* Our function checks 'set_of_numbers';
* If it finds a factor, it records(once for each round) this factor;
* then divides 'set_of_numbers' by the factor found
* and makes this the new 'set_of_numbers'.
* It continues recursively until all factors are found.
*/
function getLCMFactors() {
// STEP 1:
// Sort in descending order
arg_copy.sort(
function (a, b) {
return b - a;
}
);
while (i <= arg_copy[0]) {
state_check = false;
// STEP 2:
for (var j = 0; j < arg_copy.length; j++) {
if ((arg_copy[j] % i) == 0) {
// STEP 3:
arg_copy[j] /= i;
if (state_check == false) {
all_factors[index++] = i;
}
state_check = true; //do not store the factor twice
}
}
// STEP 4:
if (state_check == true) {
return getLCMFactors();
}
i++;
}
return;
}
function getLCM(group) {
// Make a local copy
for (var j = 0; j < group.length; j++) {
arg_copy[j] = group[j];
}
getLCMFactors();
var LCM = 1;
for (var k = 0; k < all_factors.length; k++) {
LCM *= all_factors[k];
}
return LCM;
}