From 2317033e756addc52f9bbaf021011fc21e410629 Mon Sep 17 00:00:00 2001 From: Camden Dixie O'Brien Date: Thu, 14 Dec 2023 12:42:30 +0000 Subject: [PATCH] Create prime factorisation exercise --- 17_prime_factors.lua | 50 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 17_prime_factors.lua diff --git a/17_prime_factors.lua b/17_prime_factors.lua new file mode 100644 index 0000000..46af5c5 --- /dev/null +++ b/17_prime_factors.lua @@ -0,0 +1,50 @@ +-- 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())