From 563cb2f865dcb3e10e7fe0cd3d8ca60a3e02e407 Mon Sep 17 00:00:00 2001 From: Abigail Date: Thu, 4 Nov 2021 18:12:56 +0100 Subject: Bash, bc, C, Lua, Node.js, Python and Ruby solutions for week 133, part 1. --- challenge-133/abigail/lua/ch-1.lua | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 challenge-133/abigail/lua/ch-1.lua (limited to 'challenge-133/abigail/lua') diff --git a/challenge-133/abigail/lua/ch-1.lua b/challenge-133/abigail/lua/ch-1.lua new file mode 100644 index 0000000000..f846c84e91 --- /dev/null +++ b/challenge-133/abigail/lua/ch-1.lua @@ -0,0 +1,13 @@ +#!/opt/local/bin/lua + +-- +-- See ../README.md +-- + +-- +-- Run as: lua ch-1.lua < input-file +-- + +for num in io . lines () do + print (math . floor (math . exp (math . log (tonumber (num)) / 2))) +end -- cgit From a0298e354570ba3ebf90abaeaf9f2af3b58702e7 Mon Sep 17 00:00:00 2001 From: Abigail Date: Fri, 5 Nov 2021 20:58:51 +0100 Subject: AWK, Bash, Lua, Node.js, Python and Ruby solutions for week 133, part 2. --- challenge-133/abigail/lua/ch-2.lua | 53 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 challenge-133/abigail/lua/ch-2.lua (limited to 'challenge-133/abigail/lua') diff --git a/challenge-133/abigail/lua/ch-2.lua b/challenge-133/abigail/lua/ch-2.lua new file mode 100644 index 0000000000..45120a21b3 --- /dev/null +++ b/challenge-133/abigail/lua/ch-2.lua @@ -0,0 +1,53 @@ +#!/opt/local/bin/lua + +-- +-- See ../README.md +-- + +-- +-- Run as: lua ch-2.lua +-- + +local small_primes = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31} + +function factorize (num) + local out = {} + for _, prime in ipairs (small_primes) do + while num % prime == 0 do + table . insert (out, prime) + num = num / prime + end + end + if num > 1 then + table . insert (out, num) + end + return (out) +end + + +function digit_sum (numbers) + local sum = 0 + for _, number in pairs (numbers) do + while number > 0 do + sum = sum + number % 10 + number = math . floor (number / 10) + end + end + return (sum) +end + + +local number = 1 +local count = 0 + + +while count < 10 do + local factors = factorize (number) + if #factors > 1 then + if digit_sum ({number}) == digit_sum (factors) then + print (number) + count = count + 1 + end + end + number = number + 1 +end -- cgit