lua-exercises/05_greatest_common_divisor.lua

29 lines
993 B
Lua

-- The greatest common divisor (GCD) of two numbers is, as the name
-- suggests, the largest number which evenly divides both. Implement
-- the function below to calculate this. It's recommended that you
-- use Euclid's algorithm but feel free to use another approach if you
-- want.
--
-- Solution --------------------------------------------------------------------
function greatest_common_divisor(x, y)
-- Your implementation here
end
-- Tests -----------------------------------------------------------------------
local luaunit = require("luaunit.luaunit")
function test_greatest_common_divisor_of_1386_and_3213_is_63()
luaunit.assertEquals(greatest_common_divisor(1386, 3213), 63)
end
function test_greatest_common_divisor_of_1470_and_3234_is_294()
luaunit.assertEquals(greatest_common_divisor(1470, 3234), 294)
end
function test_greatest_common_divisor_of_931_and_399_is_133()
luaunit.assertEquals(greatest_common_divisor(931, 399), 133)
end
os.exit(luaunit.LuaUnit.run())