2024-05-18 18:01:41 +02:00
|
|
|
|
|
|
|
|
|
/* Integer Math
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
export {
|
2024-12-24 12:51:36 +01:00
|
|
|
|
import "int.lt";
|
|
|
|
|
|
2024-05-18 18:01:41 +02:00
|
|
|
|
/* 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-05-18 18:01:41 +02:00
|
|
|
|
} ↦ {
|
2024-12-24 12:51:36 +01:00
|
|
|
|
while( int-gt b 0 ) {
|
2024-05-18 18:01:41 +02:00
|
|
|
|
let tmp = i% a b;
|
|
|
|
|
! a b;
|
|
|
|
|
! b tmp;
|
2024-12-24 12:51:36 +01:00
|
|
|
|
};
|
2024-05-18 18:01:41 +02: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;
|
2024-05-18 18:01:41 +02:00
|
|
|
|
} ↦ i* (int-abs a) (i/ (int-abs b) (gcd a b));
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|