summaryrefslogtreecommitdiff
path: root/gcd.lua
diff options
context:
space:
mode:
Diffstat (limited to 'gcd.lua')
-rwxr-xr-xgcd.lua25
1 files changed, 25 insertions, 0 deletions
diff --git a/gcd.lua b/gcd.lua
new file mode 100755
index 0000000..1e6352c
--- /dev/null
+++ b/gcd.lua
@@ -0,0 +1,25 @@
+#!/usr/bin/env lua
+
+-- SYNOSPSIS:
+-- # 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)
+ r = ns[1]
+ for i = 2, table.maxn(ns) do
+ r = gcd2(r, ns[i])
+ end
+ return r
+end
+
+print(gcdn(arg))
+