home *** CD-ROM | disk | FTP | other *** search
- *REM This batch file is Freeware, free to use and redistribute unmodified *
- *REM Jouni Miettunen * jon@stekt.oulu.fi * Oulu * Finland * Europe * 1992 *
-
- REM Greatest Common Divisor and Least Common Multiplier
- REM Timestamp 15-Nov-1992
-
- REM Note: EVAL can calculate 16 digit integers, IF/IFF compare 9 digit integer.
- REM Note: calculating LCM *can* cause overflow (multiplication).
-
- setlocal^break on^unalias *^if %#==0 .or. "%1"=="-?" goto info^set dec=100
-
- REM Did the user give a legal decimal string? Nothing but numbers?
-
- :input
- iff "%1"=="%@int[%1]" then^set %dec=%1^set dec=%@eval[%dec+1]
- else^echo Error: "%1" NaN (Not a Number).^goto next^endiff
-
- REM At the moment we can calculate max 16 digit numbers.
- REM Note: hard to check for overflow without overflow, so I didn't even try.
- REM It's fast this way and you'll notice, when errors occur...
-
- iff %@len[%1] gt 16 then
- echo Error: "%1" too long for me.^unset %dec^set dec=%@eval[%dec-1]^endiff
-
- REM Is there more?
-
- :next
- shift^if "%1" != "" goto input
-
- REM Do we have something to calculate?
-
- if "%100"=="" goto info^set dec=100
- if "%[%dec]"=="" set %dec=1^set gcd=%[%dec]
- echos Calculating Greatest Common Divisor...
-
- REM Main loop for GCD calculation
-
- :gcd
- set dec=%@eval[%dec+1]^iff "%[%dec]" != "" then
- set a=%gcd^set b=%[%dec]^gosub gcdloop
- goto gcd
- endiff
- screen %_row 0 Greatest Common Divisor is %gcdn
- echos Calculating Least Common Multiplier...
- set dec=99^set lcm=1^goto lcm
-
- REM GCD for two numbers
-
- :gcdloop
- REM if %a is less than %b then do...
- iff %@ascii[%@eval[%a-%b]]==45 then^set c=%a^set a=%b^set b=%c^endiff
- set a=%@eval[%a %% %b]^if %a != 0 goto gcdloop^set gcd=%b^return
-
- REM Setup for LCM calculation
-
- :lcm
- set dec=%@eval[%dec+1]^iff "%[%dec]" != "" then
- set lcm=%@eval[%lcm * %[%dec]]
- goto lcm
- endiff
- set dec=100
-
- REM Calculate LCM
-
- :lcmloop
- set dec=%@eval[%dec+1]^iff "%[%dec]" != "" then
- set lcm=%@eval[%lcm / %gcd]
- goto lcmloop
- endiff
- screen %_row 0 Least Common Multiplier is %lcmn
- goto end
-
- :info
- echo Usage: %@lower[%@name[%0]] [number ...]
- text
-
- Calculate Greatest Common Divisor and Least Common Multiplier.
- Numbers have to be positive integers with max 16 digits in each.
- endtext
-
- :end
- break off^quit
-