aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAbigail <abigail@abigail.freedom.nl>2022-01-15 19:47:06 +0100
committerAbigail <abigail@abigail.freedom.nl>2022-01-15 19:47:06 +0100
commit0b93e274674688f3ae6702e168b0e0391c4a5bf5 (patch)
tree1a4e433dafca8ee7473ec66b5c40988e49e3e496
parentf6b8071c5ec3c2470b8e1ba37908648e0dbededc (diff)
downloadperlweeklychallenge-club-0b93e274674688f3ae6702e168b0e0391c4a5bf5.tar.gz
perlweeklychallenge-club-0b93e274674688f3ae6702e168b0e0391c4a5bf5.tar.bz2
perlweeklychallenge-club-0b93e274674688f3ae6702e168b0e0391c4a5bf5.zip
Week 147: Lua solutions
-rw-r--r--challenge-147/abigail/lua/ch-1.lua57
-rw-r--r--challenge-147/abigail/lua/ch-2.lua29
2 files changed, 86 insertions, 0 deletions
diff --git a/challenge-147/abigail/lua/ch-1.lua b/challenge-147/abigail/lua/ch-1.lua
new file mode 100644
index 0000000000..0c86152ba1
--- /dev/null
+++ b/challenge-147/abigail/lua/ch-1.lua
@@ -0,0 +1,57 @@
+#!/opt/local/bin/lua
+
+--
+-- See https://theweeklychallenge.org/blog/perl-weekly-challenge-147
+--
+
+--
+-- Run as: lua ch-1.lua
+--
+
+function is_prime (p)
+ if p == 2 then
+ return true
+ end
+ if p % 2 == 0 then
+ return false
+ end
+ i = 3
+ while i * i <= p do
+ if p % i == 0 then
+ return false
+ end
+ i = i + 2
+ end
+ return true
+end
+
+todo = {2, 3, 5, 7}
+for i, p in ipairs (todo) do
+ io . write (p, " ")
+end
+
+count = 20 - #todo
+
+pow = 10
+while #todo > 0 do
+ new_todo = {}
+ for d = 1, 9 do
+ for i, p in ipairs (todo) do
+ candidate = d * pow + p
+ if is_prime (candidate) then
+ io . write (candidate, " ")
+ new_todo [#new_todo + 1] = candidate
+ count = count - 1
+ if count <= 0 then
+ goto end_of_while
+ end
+ end
+ end
+ end
+ todo = new_todo
+ pow = pow * 10
+end
+
+::end_of_while::
+
+io . write ("\n")
diff --git a/challenge-147/abigail/lua/ch-2.lua b/challenge-147/abigail/lua/ch-2.lua
new file mode 100644
index 0000000000..1d93585f24
--- /dev/null
+++ b/challenge-147/abigail/lua/ch-2.lua
@@ -0,0 +1,29 @@
+#!/opt/local/bin/lua
+
+--
+-- See https://theweeklychallenge.org/blog/perl-weekly-challenge-147
+--
+
+--
+-- Run as: lua ch-2.lua
+--
+
+pentagon = {}
+p = 0
+n = 0
+
+while true do
+ p = p + n + n + n + 1
+ n = n + 1
+ pentagon [p] = 1
+ for seen in pairs (pentagon) do
+ if seen + seen <= p and pentagon [p - seen] ~= nil
+ and pentagon [p - seen - seen] ~= nil then
+ print (seen .. " " .. (p - seen))
+ goto end_of_while
+ end
+ end
+end
+
+::end_of_while::
+