How to resolve the algorithm Least common multiple step by step in the BASIC programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Least common multiple step by step in the BASIC 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 BASIC programming language

Source code in the basic programming language

10 DEF FN MOD(A) = INT((A / B - INT(A / B)) * B + .05) * SGN(A / B)
20 INPUT"M=";M%
30 INPUT"N=";N%
40 GOSUB 100
50 PRINT R
60 END

100 REM LEAST COMMON MULTIPLE M% N%
110 R = 0
120 IF M% = 0 OR N% = 0 THEN RETURN
130 A% = M% : B% = N% : GOSUB 200"GCD
140 R = ABS(M%*N%)/R
150 RETURN

200 REM GCD ITERATIVE EUCLID A% B%
210 FOR B = B% TO 0 STEP 0
220     C% = A%
230     A% = B
240     B = FN MOD(C%)
250 NEXT B
260 R = ABS(A%)
270 RETURN

      DEF FN_LCM(M%,N%)
      IF M%=0 OR N%=0 THEN =0 ELSE =ABS(M%*N%)/FN_GCD_Iterative_Euclid(M%, N%)
      
      DEF FN_GCD_Iterative_Euclid(A%, B%)
      LOCAL C%
      WHILE B%
        C% = A%
        A% = B%
        B% = C% MOD B%
      ENDWHILE
      = ABS(A%)

100 DEF LCM(A,B)=(A*B)/GCD(A,B)
110 DEF GCD(A,B)
120   DO WHILE B>0
130     LET T=B:LET B=MOD(A,B):LET A=T
140   LOOP 
150   LET GCD=A
160 END DEF 
170 PRINT LCM(12,18)

10 PRINT "First number"
20 INPUT A
30 PRINT "Second number"
40 INPUT B
42 LET Q = A
44 LET R = B
50 IF Q<0 THEN LET Q=-Q
60 IF R<0 THEN LET R=-R
70 IF Q>R THEN GOTO 130
80 LET R = R - Q
90 IF Q=0 THEN GOTO 110
100 GOTO 50
110 LET U = (A*B)/R
111 IF U < 0 THEN LET U = - U
112 PRINT U
120 END
130 LET C=Q
140 LET Q=R
150 LET R=C
160 GOTO 70


  

You may also check:How to resolve the algorithm Subleq step by step in the Quackery programming language
You may also check:How to resolve the algorithm Integer comparison step by step in the OCaml programming language
You may also check:How to resolve the algorithm Execute Computer/Zero step by step in the ALGOL 68 programming language
You may also check:How to resolve the algorithm Include a file step by step in the Mathematica / Wolfram Language programming language
You may also check:How to resolve the algorithm Run-length encoding step by step in the Objective-C programming language