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







<< PreviousNext >>

L.C.M. of a Set of Numbers in Java



Finding L.C.M. in Java

Akin to 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.

Hence, L.C.M. of 8, 12 and 18 = 2 X 2 X 2 x 3 x 3 = 72


Simulating L.C.M. in code

We shall follow the steps below in writing our 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 a new class file; File, New File.
Call it findLCM.
Type out the adjoining Java code for Lowest Common Multiple (L.C.M.)



Note: You can comment out the Java code for the main class from the previous lesson if you have been following.









<< PreviousNext >>