9e03717deb
FossilOrigin-Name: 088675e452ed86a712563c8b2597fe4d47da59bdea0e40becdd1e028a84c47b0
19 lines
418 B
Forth
19 lines
418 B
Forth
# Least Common Multiple
|
|
|
|
The least common multiple of two integers a and b, is the
|
|
smallest positive integer that is divisible by both a and b.
|
|
|
|
This implements a word to find this in RETRO.
|
|
|
|
It uses a formula that reduces the problem to computing the
|
|
greatest common divisor (gcd), as in:
|
|
|
|
lcm(a,b) = |a*b| / gcd(a,b)
|
|
|
|
~~~
|
|
:gcd (ab-n)
|
|
[ tuck mod dup ] while drop ;
|
|
|
|
:lcm (ab-n)
|
|
dup-pair gcd [ * ] dip / ;
|
|
~~~
|