How to resolve the algorithm Least common multiple step by step in the VBScript programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Least common multiple step by step in the VBScript programming language
Table of Contents
Problem Statement
Compute the least common multiple (LCM) of two integers. Given m and n, the least common multiple is the smallest positive integer that has both m and n as factors.
The least common multiple of 12 and 18 is 36, because:
As a special case, if either m or n is zero, then the least common multiple is zero.
One way to calculate the least common multiple is to iterate all the multiples of m, until you find one that is also a multiple of n. If you already have gcd for greatest common divisor, then this formula calculates lcm.
One can also find lcm by merging the prime decompositions of both m and n.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Least common multiple step by step in the VBScript programming language
Source code in the vbscript programming language
Function LCM(a,b)
LCM = POS((a * b)/GCD(a,b))
End Function
Function GCD(a,b)
Do
If a Mod b > 0 Then
c = a Mod b
a = b
b = c
Else
GCD = b
Exit Do
End If
Loop
End Function
Function POS(n)
If n < 0 Then
POS = n * -1
Else
POS = n
End If
End Function
i = WScript.Arguments(0)
j = WScript.Arguments(1)
WScript.StdOut.Write "The LCM of " & i & " and " & j & " is " & LCM(i,j) & "."
WScript.StdOut.WriteLine
You may also check:How to resolve the algorithm Loops/Continue step by step in the Go programming language
You may also check:How to resolve the algorithm A+B step by step in the Excel programming language
You may also check:How to resolve the algorithm Loops/While step by step in the TorqueScript programming language
You may also check:How to resolve the algorithm Idiomatically determine all the lowercase and uppercase letters step by step in the Mathematica/Wolfram Language programming language
You may also check:How to resolve the algorithm Textonyms step by step in the Delphi programming language