lt-core/lt-stdlib/euclidean.lt

31 lines
556 B
Text
Raw Normal View History

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