aboutsummaryrefslogtreecommitdiff
path: root/challenge-123/abigail/lua/ch-1.lua
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-123/abigail/lua/ch-1.lua')
-rw-r--r--challenge-123/abigail/lua/ch-1.lua48
1 files changed, 48 insertions, 0 deletions
diff --git a/challenge-123/abigail/lua/ch-1.lua b/challenge-123/abigail/lua/ch-1.lua
new file mode 100644
index 0000000000..4feb237189
--- /dev/null
+++ b/challenge-123/abigail/lua/ch-1.lua
@@ -0,0 +1,48 @@
+#!/opt/local/bin/lua
+
+--
+-- See ../README.md
+--
+
+--
+-- Run as: lua ch-1.lua < input-file
+--
+
+local ugly = {}
+ugly [1] = 1
+local next_2 = 1
+local next_3 = 1
+local next_5 = 1
+local count = 1
+
+for n in io . lines () do
+ n = tonumber (n)
+ while count < n do
+ local c2 = 2 * ugly [next_2]
+ local c3 = 3 * ugly [next_3]
+ local c5 = 5 * ugly [next_5]
+
+ count = count + 1
+
+ if c2 <= c3 and c2 <= c5 then
+ ugly [count] = c2
+ end
+ if c3 <= c2 and c3 <= c5 then
+ ugly [count] = c3
+ end
+ if c5 <= c2 and c5 <= c3 then
+ ugly [count] = c5
+ end
+
+ if 2 * ugly [next_2] <= ugly [count] then
+ next_2 = next_2 + 1
+ end
+ if 3 * ugly [next_3] <= ugly [count] then
+ next_3 = next_3 + 1
+ end
+ if 5 * ugly [next_5] <= ugly [count] then
+ next_5 = next_5 + 1
+ end
+ end
+ print (ugly [n])
+end