What is HCF and GCD? | Maths Explanation for JavaScript Kids
Highest Common Factor or Greatest Common Divisor is the highest-valued-factor
or greatest-valued-divisor common to a set of numbers.
In this JavaScript math project, we'll explore how to find the Highest Common Factor (HCF) and Greatest Common Divisor (GCD) of numbers.
This tutorial is perfect for primary school students learning to code with JavaScript.
A common method for finding H.C.F. - G.C.D. is repeated factorization
using only common factors.
If we have the set of numbers 30, 48 and
54 for example, their H.C.F. or G.C.D. is found thus:
Hence, H.C.F. of 30, 48 and 54 = 2 X 3 = 6
How to find HCF of multiple numbers in JavaScript | Step-by-step Guide.
We shall follow the steps below in our JavaScript algorithm for finding HCF.
Step 1:
Do a numerical sort on the set so its first member is the smallest in the set.
Step 2:
Starting with 2, iteratively check through the set of numbers for a common factor.
Step 3:
For each common factor, divide every member of the number set by the common factor.
Step 4:
Repeat from step 2 recursively until there are no more common factors.
Create 2 new files; On Notepad++: File, New.
Call them HCF.html and HCF.js.
Type out the adjoining JavaScript code for finding Highest Common Factor (Greatest Common Divisor).
So! JavaScript Fun Practice Exercise - Find HCF
As a fun practice exercise, feel free to try out your own numbers, and see how the JavaScript code finds the HCF of those numbers.
JavaScript Code for HCF.html
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Find H.C.F.</title>
<script src="HCF.js"></script>
</head>
<body>
<h4>Highest Common Factors of a Group of Numbers.</h4>
<div id="myHCF"></div>
<script>
/*
* 'group_of_numbers' holds the numbers whose HCF we are trying to find.
* Add as many of these numbers as you wish by extending the array.
*/
var group_of_numbers = [];
group_of_numbers[0] = 14;
group_of_numbers[1] = 49;
group_of_numbers[2] = 21;
group_of_numbers[3] = 42;
var result = getHCF(group_of_numbers); // call to our function
document.getElementById("myHCF").innerHTML =
"The H.C.F. of " + group_of_numbers.join(", ") + " is: " + result;
</script>
</body>
</html>
JavaScript Code for HCF.js
var index = 0; // index into array common_factors
var state_all_round_factor = false; // variable to keep state
var i = 2; // start checking for factors from variable 2
/* Pass of Array(according to javascript) is by reference;
so make a copy that will be passed instead. */
var arg_copy = [];
/*
* Our function checks 'group_of_numbers';
* If it finds a factor common to all fo it, it records this factor;
* then divides 'group_of_numbers' by the common factor found
* and makes this the new 'group_of_numbers'.
* It continues recursively until all common factors are found.
*/
function getHCFFactors(operate_on) {
while (i <= operate_on[0]) {
state_all_round_factor = true;
// STEP 2:
for (var j = 0; j < operate_on.length; j++) {
if (!(state_all_round_factor == true && (operate_on[j] % i) == 0)) {
state_all_round_factor = false;
}
}
// STEP 3:
if (state_all_round_factor == true) {
for (var j = 0; j < operate_on.length; j++) {
operate_on[j] /= i;
}
common_factors[index++] = i;
// STEP 4:
return getHCFFactors(operate_on);
}
i++;
}
return;
}
function getHCF(group) {
// STEP 1:
// Sort in ascending order
group_of_numbers.sort(
function (a, b) {
return a - b;
}
);
for (var j = 0; j < group.length; j++) {
arg_copy[j] = group[j];
}
getHCFFactors(arg_copy);
var HCF = 1;
for (var k = 0; k < common_factors.length; k++) {
HCF *= common_factors[k];
}
return HCF;
}