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







<< PreviousNext >>

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



What is LCM? | Maths Explanation for Java 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 Java.
Figure: Math steps on how to find L.C.M. using prime factorization method in Java.

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 Java

We shall follow the steps below in writing our Java 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 a new Java 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.



So! Java Fun Practice Exercise - Find LCM

As a fun practice exercise, feel free to try out your own numbers, and see how the Java code finds the LCM of those numbers.







Java Code for Find LCM class.

package arithmetic;

import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;
import java.util.Collections;

public class LCM {

    private final Integer[] set_of_numbers;
    private final Integer[] arg_copy; // Java passes arrays by reference; make a copy.
    private final List<Integer> all_factors = new ArrayList<>(); // factors common to our set_of_numbers

    private int index// index into array common_factors
    private boolean state_check// variable to keep state
    private int calc_result;

    public LCM(List<Integer> group) {
        set_of_numbers = new Integer[group.size()];
        arg_copy = new Integer[group.size()];
        index = 0;

        //iterate through and retrieve members
        for (int number : group) {
            set_of_numbers[index] = number;
            index++;
        }

        Arrays.sort(set_of_numbers, Collections.reverseOrder());

        state_check = false;
        calc_result = 1;
    }

    /**
     * Our function checks 'set_of_numbers'; If it finds a factor common to all
     * for it, it records this factor; then divides 'set_of_numbers' by the
     * common factor found and makes this the new 'set_of_numbers'. It continues
     * recursively until all common factors are found.
     */

    private int findLCMFactors() {
        System.arraycopy(set_of_numbers, 0, arg_copy, 0, set_of_numbers.length);
        Arrays.sort(arg_copy, Collections.reverseOrder());

        while (index <= arg_copy[0]) {
            state_check = false;
            for (int j = 0; j < set_of_numbers.length; j++) {
                if (set_of_numbers[j] != 1 && (set_of_numbers[j] % index) == 0) {
                    set_of_numbers[j] /= index;
                    if (state_check == false) {
                        all_factors.add(index);
                    }
                    state_check = true;
                }
            }
            if (state_check == true) {
                return findLCMFactors();
            }
            index++;
        }

        return 0;
    }

    /**
     * Just calls out and collects the prepared factors.
     * @return - string value;
     */

    public int getLCM() {
        index = 2;
        findLCMFactors();

        //iterate through and retrieve members
        all_factors.stream().forEach((factor) -> {
            calc_result *= factor;
        });

        return calc_result;
    }
}

Java Code for Find LCM - Main Class.

package arithmetic;

import java.util.ArrayList;
import java.util.List;

public class Arithmetic {

    public static void main(String[] args) {
        System.out.println("Welcome to our demonstration sequels");
        System.out.println("Hope you enjoy (and follow) the lessons.");
        System.out.println("");

        /*
        * Find LCM.
         */

        String render_result = "The LCM of ";
        LCM lcm;
        List<Integer> group;

        group = new ArrayList<>();
        group.add(40);
        group.add(50);
        group.add(60);

        for (int number : group) {
            render_result += number + "; ";
        }
        render_result += "is:  ";

        lcm = new LCM(group);
        System.out.println(render_result + lcm.getLCM());
    }
}



<< PreviousNext >>