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







<< PreviousNext >>

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



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

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 C#

We shall follow the steps below in writing our C# 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 C# class file; Project, Add Class.
Call it LCM.
Type out the adjoining C# code for finding Lowest Common Multiple (L.C.M.)


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



So! C# Fun Practice Exercise - Find LCM

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







C# Code for LCM class.

using System.Collections.Generic;

namespace Arithmetic
{
    class LCM
    {
        private List<int> set_of_numbers = new List<int>();
        private List<int> arg_copy = new List<int>(); // arrays are passed by reference; make a copy.
        private List<int> all_factors = new List<int>(); // factors common to our set_of_numbers

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

        public LCM(List<int> group)
        {
            //iterate through and retrieve members
            foreach (int number in group)
            {
                set_of_numbers.Add(number);
                arg_copy.Add(number);
            }

            set_of_numbers.Sort();
            set_of_numbers.Reverse();

            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()
        {
            for (int i = 0; i < set_of_numbers.Count; i++)
            {
                arg_copy[i] = set_of_numbers[i];
            }
            // STEP 1:
            arg_copy.Sort();
            arg_copy.Reverse();

            while (index <= arg_copy[0])
            {
                state_check = false;
                for (int j = 0; j < set_of_numbers.Count; j++)
                {
                    if (set_of_numbers[j] != 1 && (set_of_numbers[j] % index) == 0)
                    {
                        // STEP 3:
                        set_of_numbers[j] /= index;
                        if (state_check == false)
                        {
                            all_factors.Add(index);
                        }
                        state_check = true;
                    }
                }
                // STEP 4:
                if (state_check == true)
                {
                    return findLCMFactors();
                }
                index++;
            }

            return 0;
        }

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

        public int getLCM()
        {
            // STEP 2:
            index = 2;
            findLCMFactors();

            //iterate through and retrieve members
            foreach (int factor in all_factors)
            {
                calc_result *= factor;
            }

            return calc_result;
        }
    }
}

C# Code for LCM - Main Class.

using System;
using System.Collections.Generic;

namespace Arithmetic
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Welcome to our demonstration sequels");
            Console.WriteLine("Hope you enjoy (and follow) the lessons.");
            Console.WriteLine("\r\n");

            /*
            * Find LCM.
             */

            List<int> set = new List<int>();
            set.Add(12);
            set.Add(18);
            set.Add(24);

            LCM LCM = new LCM(set);

            Console.WriteLine("The L.C.M. of " + String.Join(", ", set) + " is " + LCM.getLCM());

        }
    }
}



<< PreviousNext >>