How to resolve the algorithm Least common multiple step by step in the PL/I programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Least common multiple step by step in the PL/I 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 PL/I programming language
Source code in the pl/i programming language
/* Calculate the Least Common Multiple of two integers. */
LCM: procedure options (main); /* 16 October 2013 */
declare (m, n) fixed binary (31);
get (m, n);
put edit ('The LCM of ', m, ' and ', n, ' is', LCM(m, n)) (a, x(1));
LCM: procedure (m, n) returns (fixed binary (31));
declare (m, n) fixed binary (31) nonassignable;
if m = 0 | n = 0 then return (0);
return (abs(m*n) / GCD(m, n));
end LCM;
GCD: procedure (a, b) returns (fixed binary (31)) recursive;
declare (a, b) fixed binary (31);
if b = 0 then return (a);
return (GCD (b, mod(a, b)) );
end GCD;
end LCM;
You may also check:How to resolve the algorithm Angle difference between two bearings step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Zero to the zero power step by step in the D programming language
You may also check:How to resolve the algorithm HTTPS step by step in the Ada programming language
You may also check:How to resolve the algorithm Man or boy test step by step in the Erlang programming language
You may also check:How to resolve the algorithm MD5 step by step in the Crystal programming language