aboutsummaryrefslogtreecommitdiff
path: root/challenge-136/abigail/lua/ch-1.lua
blob: 182d18fba7d01674e6dcf6ac8f85659de3c2ae6b (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
#!/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)
    while b > 0 do
        a, b = b, a % b
    end
    return a
end

--
-- Precalculate all the relevant powers of 2. Note that in pre 5-3 lua
-- integers are doubles, and start losing precision at 2^53, so we go
-- up to 2^52.
--
local power_of_2 = {}
local power      = 1
for i = 1, 52 do
    power = power * 2
    power_of_2 [power] = 1
end


for line in io . lines () do
    local _, _, n, m = line : find ("([0-9]+)%s+([0-9]+)")
    if power_of_2 [gcd (tonumber (n), tonumber (m))] then
        print (1)
    else
        print (0)
    end
end