usingMaths.com
From Theory to Practice - Math You Can Use.







<< PreviousNext >>

L.C.M. of a Set of Numbers in JavaScript - Maths Programming for Kids



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:

How to find L.C.M. using prime factorization method in JavaScript.
Figure: Math steps on how to find L.C.M. using prime factorization method in JavaScript.

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.









<< PreviousNext >>