! Using a LINGO Procedure for computing the
Least Common Multiple of a pair of numbers;
! Keywords: GCD, Greatest Common Divisor,
LCM, Least Common Multiple, Procedures in LINGO, Euclid's algorithm;
DATA:
X = 3.7;
Y = 15;
ENDDATA
PROCEDURE GETGCD:
! Inputs:
GCDPR = previous Greatest Common Divisor,
GCDNTR = new term to include in GCD calculation,
Outputs:
GCDNU = GCD( GCDPR, GCDNTR);
GCDTOLR = 10^(13); !Zero tolerance;
GCDX = GCDPR;
! Make GCDRM the smaller of the two;
@IFC( GCDX #LT# GCDNTR:
GCDNU = GCDNTR;
GCDRM = GCDX;
@ELSE
GCDNU = GCDX;
GCDRM = GCDNTR;
);
! Now use Euclid's algorithm. If GCDNU divides GCDX,
then the remainder is zero;
@WHILE( GCDRM #GT# GCDTOLR:
GCDX = GCDNU;
GCDNU = GCDRM;
! Get the remainder of GCDX/GCDNU;
GCDRM = @MOD( GCDX, GCDNU);
);
ENDPROCEDURE
PROCEDURE GETLCM:
! Inputs:
LCMPR = previous Greatest Common Divisor,
LCMNTR = new term to include in GCD calculation,
Outputs:
LCMNU = LCM( GCDPR, GCDNTR);
GCDPR = LCMPR;
GCDNTR = LCMNTR;
GETGCD;
LCMNU = @ABS( GCDPR* GCDNTR)/GCDNU;
ENDPROCEDURE
CALC:
! Calculate the LCM of X and Y;
@SET('TERSEO',2); ! Turn off default output;
LCMPR = X;
LCMNTR = Y;
GETLCM;
@WRITE(' Least Common Multiple of ', X,' and ', Y,' = ', @FORMAT(LCMNU,'20.10g'), @NEWLINE(1));
ENDCALC
