aboutsummaryrefslogtreecommitdiff
path: root/challenge-003/abigail/lua/ch-1.lua
blob: 3f0bf5b9fee05f59a03e749b843d62ecee11d67f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/opt/local/bin/lua

--
-- See https://theweeklychallenge.org/blog/perl-weekly-challenge-003
--

--
-- 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