aboutsummaryrefslogtreecommitdiff
path: root/challenge-136/abigail/lua/ch-1.lua
blob: 369ee72246421521c90469e701bb0023297eccc9 (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
#!/opt/local/bin/lua

--
-- See ../README.md
--

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

--
-- Find the GCD, using Euclids algorithm
--    (https://en.wikipedia.org/wiki/Euclidean_algorithm#Implementations)
--
function gcd (a, b)
    if b >  a then return gcd (b, a) end
    if b == 0 then return a          end
                   return gcd (b, a % b)
end

function is_power_of_n (number, n)
    if number <  1    then return false end
    if number == 1    then return true  end
    if number % n > 1 then return false end
                      return (is_power_of_n (number / n, n))
end

function is_power_of_2 (number)
    return is_power_of_n (number, 2)
end

for line in io . lines () do
    local _, _, n, m = line : find ("([0-9]+)%s+([0-9]+)")
    n = tonumber (n)
    m = tonumber (m)
    if n % 2 == 1 or m % 2 == 1 then
        print (0)
        goto continue
    end
    local r = gcd (n, m)
    if r > 1 and is_power_of_2 (r) then
        print (1)
    else
        print (0)
    end
    ::continue::
end