lt-core/lt-stdlib/euclidean.lt
2024-12-24 12:51:36 +01:00

30 lines
556 B
Text
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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