Skip to content

Instantly share code, notes, and snippets.

@Xrayez
Last active April 16, 2023 12:53
Show Gist options
  • Save Xrayez/0b396c347d49a0ba791cd635326aca2b to your computer and use it in GitHub Desktop.
Save Xrayez/0b396c347d49a0ba791cd635326aca2b to your computer and use it in GitHub Desktop.
Greatest Common Divisor (GCD) and Least Common Multiple (LCM) implemented in Lua
-- Greatest Common Divisor (Euclidean algorithm).
function math.gcd(a, b)
local t
while b ~= 0 do
t = b
b = math.fmod(a, b)
a = t
end
return a
end
-- Least Common Multiple.
-- Assumes that both `a` and `b` are not equal to 0 at the same time.
function math.lcm(a, b)
return (a * b) / math.gcd(a, b)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment