lt-core/lt-stdlib/euclidean.lt

29 lines
488 B
Text
Raw Permalink Normal View History

/* Integer Math
*/
export {
/* Euclidean Algorithm to calculate greatest common divisor
*/
let gcd = λ{
a : ~ machine.Int64;
b : ~ machine.Int64;
} ↦ {
while( b ) {
let tmp = i% a b;
! a b;
! b tmp;
}
a;
};
/* least common multiple
*/
let lcm = λ{
a : ~ machine.Int64;
b : ~ machine.Int64;
} ↦ i* (int-abs a) (i/ (int-abs b) (gcd a b));
};