summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIgor <pashev.igor@gmail.com>2011-02-20 18:50:55 +0300
committerIgor <pashev.igor@gmail.com>2011-02-20 18:50:55 +0300
commit80a2e9adbbbf71a0acbb81ca6d8a708b5b79e7bd (patch)
tree5f0e93e75231418a9d4abdf0e840ce83d6ac89b1
parent3166b45e610d6defacf66a67b850f978dd7f2634 (diff)
downloadgcd-80a2e9adbbbf71a0acbb81ca6d8a708b5b79e7bd.tar.gz
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))
+