diff --git a/inc/math.h b/inc/math.h index 2b8255c..6033d4b 100644 --- a/inc/math.h +++ b/inc/math.h @@ -229,6 +229,14 @@ float powf(float b, float x); +uint64_t gcd(uint64_t a, uint64_t b); + +static inline uint64_t lcm(uint32_t a, uint64_t b) +{ + return (a * b) / gcd(a, b); +} + + #ifdef __cplusplus } #endif diff --git a/src/math.c b/src/math.c index 0c14e30..47f6a77 100644 --- a/src/math.c +++ b/src/math.c @@ -231,3 +231,23 @@ return exp(x * log(b)); } + + +uint64_t gcd(uint64_t a, uint64_t b) +{ + if (a == 0) + return b; + + if (b == 0) + return a; + + uint64_t r = a % b; + while (r) + { + a = b; + b = r; + r = a % b; + } + + return b; +}