Last active
April 16, 2023 12:53
-
-
Save Xrayez/0b396c347d49a0ba791cd635326aca2b to your computer and use it in GitHub Desktop.
Greatest Common Divisor (GCD) and Least Common Multiple (LCM) implemented in Lua
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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