Our Lowest Common Multiple code in Javascript.
List Lowest Common Multiple Javascript File:
var all_factors = []; // factors to our set_of_numbers
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;
}
HTML File:
<!DOCTYPE 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>
Try it out!
Elegance (0.0)