aboutsummaryrefslogtreecommitdiff
path: root/challenge-125/abigail/lua/ch-1.lua
blob: 1a0efb40c84b2ddfe94709914fc85118f43e02c0 (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 ../README.md
--

--
-- Run as: lua ch-1.lua < input-file
--

function introot (square)
    return (math . floor (.4 + math . sqrt (square)))
end


for line in io . lines () do
    local n = tonumber (line)
    if n <= 2 then
        print (-1)
        goto end_loop
    end

    local n_sq = n * n
    local c    = n + 1
    local c_sq = n_sq + 2 * n + 1
    while 2 * c - 1 <= n_sq do
        local b_sq = c_sq - n_sq
        local b    = introot (b_sq)

        if b_sq == b * b then
            print (n .. " " .. b .. " " .. c)
        end

        c_sq = c_sq + 2 * c + 1
        c    = c + 1
    end

    local max_a = math . floor (n / math . sqrt (2))
    for a = 3, max_a do
        local b_sq = n_sq - a * a
        local b    = introot (b_sq)
        if b_sq == b * b then
            print (a .. " " .. b .. " " .. n)
        end
    end

    ::end_loop::
end