diff --git a/math/lcm/lcm.go b/math/lcm/lcm.go index e1de23e17..14a1a9633 100644 --- a/math/lcm/lcm.go +++ b/math/lcm/lcm.go @@ -1,12 +1,23 @@ package lcm import ( - "math" + "math/bits" "github.com/TheAlgorithms/Go/math/gcd" ) // Lcm returns the lcm of two numbers using the fact that lcm(a,b) * gcd(a,b) = | a * b | func Lcm(a, b int64) int64 { - return int64(math.Abs(float64(a*b)) / float64(gcd.Iterative(a, b))) + ua, ub := abs(a), abs(b) + h, l := bits.Mul64(ua, ub) + g := gcd.Iterative(a, b) + r, _ := bits.Div64(h, l, uint64(g)) + return int64(r) +} + +func abs(x int64) uint64 { + if x < 0 { + return uint64(-(x+1))+1 + } + return uint64(x) }