51 lines
1.4 KiB
Lua
51 lines
1.4 KiB
Lua
-- Write a function which determines the prime factors of a given
|
|
-- number, returning them in a list. You may assume that none of the
|
|
-- prime factors are above 10000
|
|
--
|
|
-- Solution --------------------------------------------------------------------
|
|
function prime_factors(n)
|
|
-- Your implementation here
|
|
end
|
|
|
|
-- Tests -----------------------------------------------------------------------
|
|
|
|
local luaunit = require("luaunit.luaunit")
|
|
|
|
function test_prime_factors_of_42()
|
|
luaunit.assertItemsEquals(prime_factors(42), {2, 3, 7})
|
|
end
|
|
|
|
function test_prime_factors_of_70()
|
|
luaunit.assertItemsEquals(prime_factors(70), {2, 5, 7})
|
|
end
|
|
|
|
function test_prime_factors_of_253()
|
|
luaunit.assertItemsEquals(prime_factors(253), {11, 23})
|
|
end
|
|
|
|
function test_prime_factors_of_245()
|
|
luaunit.assertItemsEquals(prime_factors(245), {5, 7, 7})
|
|
end
|
|
|
|
function test_prime_factors_of_36()
|
|
luaunit.assertItemsEquals(prime_factors(36), {2, 2, 3, 3})
|
|
end
|
|
|
|
function test_prime_factors_of_1337()
|
|
luaunit.assertItemsEquals(prime_factors(1337), {7, 191})
|
|
end
|
|
|
|
function test_prime_factors_of_4321()
|
|
luaunit.assertItemsEquals(prime_factors(4321), {29, 149})
|
|
end
|
|
|
|
function test_prime_factors_of_57812834()
|
|
luaunit.assertItemsEquals(prime_factors(57812834), {2, 29, 113, 8821})
|
|
end
|
|
|
|
function test_prime_factors_of_8894220()
|
|
luaunit.assertItemsEquals(prime_factors(8894220), {2, 2, 3, 5, 271, 547})
|
|
end
|
|
|
|
os.exit(luaunit.LuaUnit.run())
|