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







<< PreviousNext >>

Faster Version for H.C.F. (G.C.D.) Code in JavaScript




A fast JavaScript code to find H.C.F. (G.C.D.)

The H.C.F. code in JavaScript from the previous lesson could get a bit slow if we run into a prime number and this prime number becomes the loop range.

Let's see how we can fix this:

Step 1:

Do a numerical sort on the resulting set so its first member is the smallest in the set.

Step 2:

Find the factors of the first number in the set.

Step 3:

Iteratively check through the set of numbers with the factors from Step 2 to make sure it is common to all.

Step 4:

For each common factor, divide every member of the number set by the common factor.

Note: The HTML part is still the same as above;
All you need to do is to change the external JavaScript source name to reflect this one.










<< PreviousNext >>