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

May 20, 2008
Syracuse, NY, USA
Of all @EVAL's functions only (?) gcd() and lcm() require '%' on a variable name.
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)]

v:\> echo %@eval[lcm(%num 18)]
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.
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
echo GCD = %divisor


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
 *  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:
p:\mapm> calc 6 15 GCD

p:\mapm> calc 6 15 LCM
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