aboutsummaryrefslogtreecommitdiff
path: root/challenge-113/abigail/lua/ch-1.lua
diff options
context:
space:
mode:
author冯昶 <seaker@qq.com>2021-05-24 15:49:34 +0800
committer冯昶 <seaker@qq.com>2021-05-24 15:49:34 +0800
commitb876dd5b111bf1c267b2fb58cd64e6b655128e5c (patch)
tree95d82b58ea802af7a3cb3d20c72af0a7be502954 /challenge-113/abigail/lua/ch-1.lua
parentd848d3cf55a41587d2e8809838ffec2a84d4491d (diff)
parentc61ba475fd263546925b47f264825a44477622e0 (diff)
downloadperlweeklychallenge-club-b876dd5b111bf1c267b2fb58cd64e6b655128e5c.tar.gz
perlweeklychallenge-club-b876dd5b111bf1c267b2fb58cd64e6b655128e5c.tar.bz2
perlweeklychallenge-club-b876dd5b111bf1c267b2fb58cd64e6b655128e5c.zip
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'challenge-113/abigail/lua/ch-1.lua')
-rw-r--r--challenge-113/abigail/lua/ch-1.lua46
1 files changed, 46 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