aboutsummaryrefslogtreecommitdiff
path: root/challenge-147/abigail/lua/ch-1.lua
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2022-01-18 08:58:27 +0000
committerGitHub <noreply@github.com>2022-01-18 08:58:27 +0000
commit52f3516552615dad1ad7566fd5934fca2914faaf (patch)
treedd57748653cc360b3f483f12d6821994f92cf721 /challenge-147/abigail/lua/ch-1.lua
parent9c9cb3dcb25537525090902ca4b8d10ae17e8960 (diff)
parentd2faecf9b3a1517b6303c84d23b35a6c26789fda (diff)
downloadperlweeklychallenge-club-52f3516552615dad1ad7566fd5934fca2914faaf.tar.gz
perlweeklychallenge-club-52f3516552615dad1ad7566fd5934fca2914faaf.tar.bz2
perlweeklychallenge-club-52f3516552615dad1ad7566fd5934fca2914faaf.zip
Merge pull request #5533 from Abigail/abigail/week-147
Abigail/week 147
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")