aboutsummaryrefslogtreecommitdiff
path: root/challenge-113/abigail/lua
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-05-23 16:59:39 +0100
committerGitHub <noreply@github.com>2021-05-23 16:59:39 +0100
commit64d90deae0b1c6e8fe708937f775f0975f138a4f (patch)
treecf2877cafb333f34246a86a5ab81a0abd8d07899 /challenge-113/abigail/lua
parentd03259c86c349e8efbbd6622dfdfcaa3c5f527f2 (diff)
parent1ab38e5de2d0bbab588c411e7764653217a9fc84 (diff)
downloadperlweeklychallenge-club-64d90deae0b1c6e8fe708937f775f0975f138a4f.tar.gz
perlweeklychallenge-club-64d90deae0b1c6e8fe708937f775f0975f138a4f.tar.bz2
perlweeklychallenge-club-64d90deae0b1c6e8fe708937f775f0975f138a4f.zip
Merge pull request #4120 from Abigail/abigail/week-113
Abigail/week 113
Diffstat (limited to 'challenge-113/abigail/lua')
-rw-r--r--challenge-113/abigail/lua/ch-1.lua46
-rw-r--r--challenge-113/abigail/lua/ch-2.lua25
2 files changed, 71 insertions, 0 deletions
diff --git a/challenge-113/abigail/lua/ch-1.lua b/challenge-113/abigail/lua/ch-1.lua
new file mode 100644
index 0000000000..4de70d106c
--- /dev/null
+++ b/challenge-113/abigail/lua/ch-1.lua
@@ -0,0 +1,46 @@
+#!/opt/local/bin/lua
+
+--
+-- See ../README.md
+--
+
+--
+-- Run as: lua ch-1.lua < input-file
+--
+
+--
+-- For a description of the algorithm, and the proof why this is correct:
+-- https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-113-1.html
+--
+
+local gcds = {1, 2, 1, 2, 5, 2, 1, 2, 1}
+
+for line in io . lines () do
+ local _, _, N, D = line : find ("([0-9]+)%s+([0-9])")
+ N = tonumber (N)
+ D = tonumber (D)
+ if D == 0
+ then if N >= 100 or N % 10 == 0
+ then print (1)
+ else print (0)
+ end
+ goto end_loop
+ end
+
+ if N >= D * 10
+ then print (1)
+ goto end_loop
+ end
+
+ for i = 0, D / gcds [D] - 1
+ do local T = N - 10 * i - D
+ if T >= 0 and T % D == 0
+ then print (1)
+ goto end_loop
+ end
+ end
+
+ print (0)
+
+ ::end_loop::
+end
diff --git a/challenge-113/abigail/lua/ch-2.lua b/challenge-113/abigail/lua/ch-2.lua
new file mode 100644
index 0000000000..5ba4c04ed9
--- /dev/null
+++ b/challenge-113/abigail/lua/ch-2.lua
@@ -0,0 +1,25 @@
+#!/opt/local/bin/lua
+
+--
+-- See ../README.md
+--
+
+--
+-- Run as: lua ch-2.lua < input-file
+--
+
+for line in io . lines () do
+ local sum = 0
+ for n in line : gmatch ("-?[0-9]+")
+ do sum = sum + tonumber (n)
+ end
+ local c = 0
+ for n in line : gmatch ("-?[0-9]+")
+ do if c > 0
+ then io . write (" ")
+ end
+ c = c + 1
+ io . write (sum - tonumber (n))
+ end
+ io . write ("\n")
+end