From 610ff9605ec7b308051a8f04f9b21d6f4060a8cd Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 19 Apr 2021 20:16:47 +0200 Subject: Solutions in 29 languages for week 109, part 1. --- challenge-109/abigail/lua/ch-1.lua | 43 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 challenge-109/abigail/lua/ch-1.lua (limited to 'challenge-109/abigail/lua/ch-1.lua') diff --git a/challenge-109/abigail/lua/ch-1.lua b/challenge-109/abigail/lua/ch-1.lua new file mode 100644 index 0000000000..5eb2d477f9 --- /dev/null +++ b/challenge-109/abigail/lua/ch-1.lua @@ -0,0 +1,43 @@ +#!/opt/local/bin/lua + +-- +-- See ../README.md +-- + +-- +-- Run as: lua ch-1.lua [plain | compute] +-- + +local PLAIN = 0 +local COMPUTE = 1 + +local COUNT = 20 + +function divisor_sum (n) + local sum = 0 + for i = 2, n - 2 do + if n % i == 0 + then sum = sum + i + end + end + return (sum) +end + +local type = PLAIN +if #arg >= 1 and arg [1] == "compute" +then type = COMPUTE +end + +if type == PLAIN +then print ("0, 0, 0, 2, 0, 5, 0, 6, 3, 7, 0, 15, 0, 9, 8, 14, 0, 20, 0, 21") +end + +if type == COMPUTE +then for n = 1, COUNT do + if n > 1 + then io . write (", ") + end + io . write (divisor_sum (n)) + end + io . write ("\n") +end -- cgit From 7b4fbbe6505b63d2faa62ad34939b6e830ea692b Mon Sep 17 00:00:00 2001 From: Abigail Date: Tue, 20 Apr 2021 20:52:57 +0200 Subject: When looping over possible divisors, stop at n / 2. --- challenge-109/abigail/lua/ch-1.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'challenge-109/abigail/lua/ch-1.lua') diff --git a/challenge-109/abigail/lua/ch-1.lua b/challenge-109/abigail/lua/ch-1.lua index 5eb2d477f9..74c3b7d914 100644 --- a/challenge-109/abigail/lua/ch-1.lua +++ b/challenge-109/abigail/lua/ch-1.lua @@ -15,7 +15,7 @@ local COUNT = 20 function divisor_sum (n) local sum = 0 - for i = 2, n - 2 do + for i = 2, n / 2 do if n % i == 0 then sum = sum + i end -- cgit