Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

gcd() and lcm() ... need '%'

May
12,834
163
Of all @EVAL's functions only (?) gcd() and lcm() require '%' on a variable name.
Code:
v:\> set num=21

v:\> echo %@eval[gcd(num 18)]
TCC: Syntax error "gcd(num 18)"

v:\> echo %@eval[lcm(num 18)]
TCC: Syntax error "lcm(num 18)"

v:\> echo %@eval[gcd(%num 18)]
3

v:\> echo %@eval[lcm(%num 18)]
126
 
I think Gnu's version has CGD and LCM.

They're pretty easy to do anyway (and fast). This works in principle, but is subject to the limitations of @MAX and @MIN. There are other ways to figure out which is bigger.
Code:
::GCD.BTM
set dividend=%@MAX[%1,%2]
set divisor=%@MIN[%1,%2]
do forever
   set remainder=%@EVAL[dividend MOD divisor]
   if %remainder == 0 leave
   set dividend=%divisor
   set divisor=%remainder
enddo
echo GCD = %divisor

And

LCM(x,y)=x * y / GCD(x,y)
 
Are you sure your MASM doesn't have GCD and LCM? I have an old version that includes
Code:
 *  M_APM  -  mapm_gcd.c
 *
 *  Copyright (C) 2001 - 2007   Michael C. Ring
There's a version on GitHub that also has that file. https://github.com/LuaDist/mapm

In fact, I just built it for the first time (even though I have no idea how to use it). I did it with VC8 using a batch file written for VC6.
 
I built it again with VC10 (using the same BAT file). And with its CALC.EXE (which was also built) I get:
Code:
p:\mapm> calc 6 15 GCD
3.0

p:\mapm> calc 6 15 LCM
30.0
 
I built CALC before also. It's my go-to command line calculator. RPN FTW.
CALC reminds me of when hand-held calculators first hit the scene. HPs used reverse polish notation, which I was never (and still am not) any good at.

My goto at the command line is a plugin command, EVAL, which is just a wrapper for @EVAL. You can do the same with an alias.
 

Similar threads

Back
Top