aboutsummaryrefslogtreecommitdiff
path: root/challenge-147/abigail/lua/ch-1.lua
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-147/abigail/lua/ch-1.lua')
-rw-r--r--challenge-147/abigail/lua/ch-1.lua57
1 files changed, 57 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")