summaryrefslogtreecommitdiff
path: root/gcd.lua
blob: fe7c893f3d7d823bf2984c7f79782bfa89c56831 (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
#!/usr/bin/env lua

-- SYNOPSIS:
-- # chmod +x gcd.lua; ./gcd.lua 121 22 33 44
-- # lua gcd.lua 121 33 22

-- http://www.lua.org/pil/6.3.html
function gcd2(a, b)
    if b == 0 then
        return a
    else
        return gcd2(b, a % b)
    end
end

function gcdn(ns)
    local r = ns[1]
    for i = 2, #ns do
        r = gcd2(r, ns[i])
    end
    return r
end

if #arg ~= 0 then
    print(gcdn(arg))
end