L.C.M. of a Set of Numbers in Visual Basic - Maths Programming for Kids
What is LCM? | Maths Explanation for VB.Net 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:
Figure: Math steps on how to find L.C.M. using prime factorization method in VB.Net.
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 VB.Net
We shall follow the steps below in writing our Visual Basic 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 VB.Net class file; Project, Add Class.
Call it findLCM.vb.
Optionally, Create a new VB.Net module file; Project, Add Module.
Call it FindLCMModule.vb.
Type out the adjoining Visual Basic (VB.Net) code for finding Lowest Common Multiple (L.C.M.)
Note: You can just reuse the VB.Net main module
from the previous lesson if you have been following.
So! Visual Basic Fun Practice Exercise - Find LCM
As a fun practice exercise, feel free to try out your own numbers,
and see how the Visual Basic code finds the LCM of those numbers.
VB.Net Code for Find LCM - Class File.
Public ClassLCM
Dim set_of_numbers AsNewList(OfInteger) ' will hold the the values to be sent in Dim all_factors AsNewList(OfInteger) ' for housing all common factors Dim state_check AsBoolean Dim calc_result AsInteger Dim index AsInteger
' A constructor PublicSub _init_(group AsList(OfInteger)) ForEach member In group
set_of_numbers.Add(member) Next ' Sort array in descending order
set_of_numbers.Sort()
set_of_numbers.Reverse()
index = 1
state_check = False
calc_result = 1 EndSub
PrivateFunction findLCMFactors() AsList(OfInteger) ' Copy 'set_of_numbers' into 'arg_copy' Dim arg_copy AsNewList(OfInteger) ForEach number In set_of_numbers
arg_copy.Add(number) Next ' STEP 1: ' sort in descending order
arg_copy.Sort()
arg_copy.Reverse()
DoWhile index <= arg_copy.Item(0)
state_check = False For count_off = 0 To set_of_numbers.Count - 1 If set_of_numbers.Item(count_off) Mod index = 0 Then ' STEP 3:
set_of_numbers.Item(count_off) = CInt(set_of_numbers.Item(count_off) / index) If state_check = FalseThen
all_factors.Add(index) EndIf
state_check = True' do Not store the factor twice EndIf Next count_off
' STEP 4: If state_check Then Return findLCMFactors() EndIf
index += 1 Loop
ReturnNothing EndFunction
' Returns an array reference Of the desired Set Of even numbers PublicFunction getLCM() AsInteger ' STEP 2:
index = 2
findLCMFactors()
' iterate through And retrieve members ForEach factor In all_factors
calc_result *= factor Next factor
Return calc_result EndFunction
EndClass
VB.Net Code for Find LCM - Main Module.
ModuleArithmetic_LCM
Sub Main()
' Use the LCM Class Dim group AsNewList(OfInteger)({20, 30, 40}) Dim lcm AsNewLCM
lcm._init_(group) Dim answer = lcm.getLCM()
Console.Write("The L.C.M. of " & String.Join(", ", group) & " is " & answer)