usingMaths.com
Demonstrating and showing pupils and students one application of Mathematics.







<< PreviousNext >>

Code for H.C.F. (G.C.D.) of a Set of Numbers in JavaScript



Finding H.C.F. - G.C.D. in 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:

How to find H.C.F.

Hence, H.C.F. of 30, 48 and 54 = 2 X 3 = 6


Simulating H.C.F. (G.C.D.) in JavaScript code

We shall follow the steps below in writing our code.

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).









<< PreviousNext >>